如何为 JAX-WS WebService 调用设置超时 [英] How to Set Timeout for JAX-WS WebService Call
问题描述
我正在开发一个 WebService 客户端,我想为我的 WebService 调用设置超时.我尝试了不同的方法,但仍然无法实现这一目标.我正在使用 JAX-WS 从 WSDL 生成代码.我使用 JBoss-eap-5.1 作为应用服务器和 JDK1.6.0_27.我发现了这些设置超时的差异方法,但没有一个对我有用.
I'm working on a WebService Client and I want to set a Timeout for my WebService Call. I have tried different approaches but still I'm not able to achieve this. I'm using JAX-WS for code generation from WSDL. I'm using JBoss-eap-5.1 as App Server and JDK1.6.0_27. I found these diff approaches for setting timeout but none of them is working for me.
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 方法.我知道这不是一个好方法,但它对我有用.伙计们请帮助我以正确的方式做到这一点.
For now what I have done is, I have called my webservice method from inside an executor. I know its not a good approach, but its working for me. Guys please help me to do it in a proper way.
logger.log("Parameters set for createorUpdateContact call.
Going 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();
}
推荐答案
你可以试试这些设置(它们是成对使用的)
You could try these settings (they are paired to be used in pairs)
BindingProviderProperties.REQUEST_TIMEOUT
BindingProviderProperties.CONNECT_TIMEOUT
BindingProviderProperties
应该来自 com.sun.xml.internal.WS.client
或者字符串 对于 JBoss:
javax.xml.ws.client.connectionTimeout
javax.xml.ws.client.receiveTimeout
所有属性都以毫秒为单位放在 getRequestContext()
上.
All properties to be put on getRequestContext()
in milliseconds.
(BindingProvider)wsPort).getRequestContext().put(BindingProviderProperties.REQUEST_TIMEOUT, yourTimeoutInMillisec);
特别是对于 JBoss,您可能希望使用 org.jboss.ws.core.StubExt
中的属性 StubExt.PROPERTY_CLIENT_TIMEOUT
.有关详细信息,请参阅此主题.
For JBoss specifically, you might want to use the property StubExt.PROPERTY_CLIENT_TIMEOUT
from org.jboss.ws.core.StubExt
. See this thread for details.
这篇关于如何为 JAX-WS WebService 调用设置超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!