我正在处理一个WebService客户端,我想为我的WebService调用设置一个超时。我尝试过不同的方法,但我仍然无法做到这一点。我使用JAX从WSDL生成代码。我使用JBoss 5.1作为应用服务器和JDK1.6.0_27。我发现了这些不同的方法来设定超时,但是没有一个对我有用。
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();
}
发布于 2012-12-22 06:20:33
您可以尝试这些设置(它们成对成对使用)
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()
上的属性以毫秒为单位。
(BindingProvider)wsPort).getRequestContext().put(BindingProviderProperties.REQUEST_TIMEOUT, yourTimeoutInMillisec);
具体来说,对于JBoss,您可能希望使用来自org.jboss.ws.core.StubExt
的属性StubExt.PROPERTY_CLIENT_TIMEOUT
。详情请参见这条线。
发布于 2013-04-23 09:05:51
就像kolossus说的,你应该用:
com.sun.xml.internal.ws.client.BindingProviderProperties
字符串值是:
com.sun.xml.internal.ws.connect.timeout
com.sun.xml.internal.ws.request.timeout
虽然不应该使用内部包,但如果您使用默认的JDK6,这是唯一的方法。因此,在这种情况下,设置接收和连接超时应该使用:
bindingProvider.getRequestContext().put(BindingProviderProperties.REQUEST_TIMEOUT,requestTimeoutMs);
bindingProvider.getRequestContext().put(BindingProviderProperties.CONNECT_TIMEOUT,connectTimeoutMs);
但请注意,如果您使用其他JAXWS参考实现,即JAXWS 2.1.4 BindingProviderProperties:,则常量值是不同的。
com.sun.xml.ws.client.BindingProviderProperties
对于REQUEST_TIMEOUT和CONNECT_TIMEOUT,您将有不同的字符串值:
com.sun.xml.ws.request.timeout
com.sun.xml.ws.connect.timeout
发布于 2014-04-16 04:40:58
对我来说,设置javax.xml.ws.client.connectionTimeout
和javax.xml.ws.client.receiveTimeout
解决了这个问题。
((BindingProvider)port).getRequestContext().put("javax.xml.ws.client.connectionTimeout", timeout);
((BindingProvider)port).getRequestContext().put("javax.xml.ws.client.receiveTimeout", timeout);
参考链接
https://stackoverflow.com/questions/13967069
复制相似问题