如何在客户端控制webservice调用的timeout

 我来答
huanglenzhi
推荐于2018-04-05 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
采纳数:117538 获赞数:517198
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。

向TA提问 私信TA
展开全部
  1,在CXF生成的stub的客户端,方式如下(CXF2.1.1):
  SimpleServiceService srv = new SimpleServiceService();
  SimpleService servicePort = srv.getSimpleServicePort();

  //timeout setting
  Client cl = ClientProxy.getClient(servicePort);

  HTTPConduit http = (HTTPConduit) cl.getConduit();

  HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
  httpClientPolicy.setConnectionTimeout(10000);
  httpClientPolicy.setReceiveTimeout(1000);
  http.setClient(httpClientPolicy);

  servicePort.concatenate("111", "2222");
  其中SimpleServiceService是自动生成的stub类.

  2,在Xfire生成的stub的客户端,方式如下(Xfire1.26):
  SimpleServiceServiceClient serviceClient= new SimpleServiceServiceClient();
  SimpleService servicePort = serviceClient.getSimpleServicePort();

  HttpClientParams params = new HttpClientParams();
  params.setParameter(HttpClientParams.USE_EXPECT_CONTINUE, Boolean.FALSE);
  // set connection timeout
  params.setParameter(HttpClientParams.CONNECTION_MANAGER_TIMEOUT, 2L);
  //set response timeout
  params.setIntParameter(HttpClientParams.SO_TIMEOUT,2);

  Client client = Client.getInstance(servicePort);
  client.setProperty(CommonsHttpMessageSender.HTTP_CLIENT_PARAMS, params);

  System.out.println(servicePort.concatenate("xfire1", "xfire2"));
  其中SimpleServiceServiceClient 是自动生成的stub类.

  上面两种方式都是通过控制底层的http 连接来达到timeout控制,如果超时会抛出 java.net.SocketTimeoutException: Read timed out.

  3,jax-ws的客户端:同样利用CXF的ClientProxy控制http连接:
  URL wsdlURL = new URL("file:///D:/JAVAWorkspace/Repository/prjCXFWS/src/wsdl/prjCXFWS.wsdl");

  QName serviceQName = new QName("test.cxfws.com/", "SimpleServiceService");

  QName portQName = new QName("test.cxfws.com/", "SimpleServicePort");

  Service service = Service.create(wsdlURL, serviceQName);

  //set handler
  service.setHandlerResolver(new RequestOrderHandlerResolver());

  SimpleService port = (SimpleService) service.getPort(portQName, SimpleService.class);

  //timeout setting
  Client cl = ClientProxy.getClient(port);

  HTTPConduit http = (HTTPConduit) cl.getConduit();

  HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
  httpClientPolicy.setConnectionTimeout(10000);
  httpClientPolicy.setReceiveTimeout(1000);
  http.setClient(httpClientPolicy);
  System.out.println(port.concatenate("srt1", "srt2"));

  4,jax-Dispatch,在网上搜索到有人说用以下方式:试了一下不好用.
  dispatch.getRequestContext().put("com.sun.xml.ws.request.timeout", 1000);
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式