java webservice 调用超时_java – 如何为JAX-WS WebService调用设置超时

这篇博客讲述了在Java中使用JAX-WS进行WebService调用时如何设置超时的方法。作者在尝试通过设置各种连接和读取超时属性来解决超时问题,包括对URLConnection、BindingProvider请求上下文以及系统属性的设置。最终,通过使用ExecutorService来异步执行WebService调用并配合超时控制实现了预期效果。
摘要由CSDN通过智能技术生成

我正在使用WebService客户端,我想为我的WebService呼叫设置一个超时.我尝试了不同的方法,但仍然无法实现这一点.我正在使用JAX-WS来从WSDL生成代码.我使用JBoss-eap-5.1作为App Server和JDK1.6.0_27.我发现这些diff方法设置超时,但没有一个为我工作.

URL mbr_service_url = new URL(null,GlobalVars.MemberService_WSDL, new URLStreamHandler() {

@Override

protected URLConnection openConnection(URL url) throws IOException {

URL clone_url = new URL(url.toString());

HttpURLConnection clone_urlconnection = (HttpURLConnection) clone_url.openConnection();

// TimeOut settings

clone_urlconnection.setConnectTimeout(10000);

clone_urlconnection.setReadTimeout(10000);

return (clone_urlconnection);

}

});

MemberService service = new MemberService(mbr_service_url);

MemberPortType soap = service.getMemberPort();

ObjectFactory factory = new ObjectFactory();

MemberEligibilityWithEnrollmentSourceRequest request = factory.createMemberEligibilityWithEnrollmentSourceRequest();

request.setMemberId(GlobalVars.MemberId);

request.setEligibilityDate(value);

((BindingProvider) soap).getRequestContext().put(com.sun.xml.ws.client.BindingProviderProperties.REQUEST_TIMEOUT, 10000);

((BindingProvider) soap).getRequestContext().put(com.sun.xml.ws.client.BindingProviderProperties.CONNECT_TIMEOUT, 10000);

((BindingProvider) soap).getRequestContext().put(com.sun.xml.internal.ws.client.BindingProviderProperties.REQUEST_TIMEOUT, 10000);

((BindingProvider) soap).getRequestContext().put(com.sun.xml.internal.ws.client.BindingProviderProperties.CONNECT_TIMEOUT, 10000);

((BindingProvider) soap).getRequestContext().put(com.sun.xml.ws.developer.JAXWSProperties.REQUEST_TIMEOUT, 10000);

((BindingProvider) soap).getRequestContext().put(com.sun.xml.ws.developer.JAXWSProperties.CONNECT_TIMEOUT, 10000);

((BindingProvider) soap).getRequestContext().put(com.sun.xml.internal.ws.developer.JAXWSProperties.REQUEST_TIMEOUT, 10000);

((BindingProvider) soap).getRequestContext().put(com.sun.xml.internal.ws.developer.JAXWSProperties.CONNECT_TIMEOUT, 10000);

System.setProperty("sun.net.client.defaultConnectTimeout", "10000");

System.setProperty("sun.net.client.defaultReadTimeout", "10000");

MemberEligibilityWithEnrollmentSourceResponse response = soap.getMemberEligibilityWithEnrollmentSource(request);

logger.log("Call to member service finished.");

现在我所做的是,我从一个执行者的内部调用了我的webservice方法.我知道它不是一个好办法,但它为我工作.请各位帮忙我以正确的方式做到这一点.

logger.log("Parameters set for createorUpdateContact call.\nGoing in Executor Service.");

ExecutorService executorService = Executors.newSingleThreadExecutor();

executorService.execute(new Runnable() {

@Override

public void run() {

try {

response = soap.getMemberEligibilityWithEnrollmentSource(request);

} catch (MemberServiceException ex) {

logger.log("Exception in call to WebService", ex.fillInStackTrace());

}

}

});

executorService.shutdown();

try {

executorService.awaitTermination(GlobalVars.WSCallTimeOut, TimeUnit.SECONDS);

} catch (InterruptedException ex) {

logger.log("Thread Interrupted!", ex);

executorService.shutdownNow();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值