如何为JAX-WS WebService调用设置超时

我正在使用WebService客户端,我想为我的WebService调用设置一个Timeout。 我尝试了不同的方法,但我仍然无法做到这一点。 我正在使用JAX-WS从WSDL生成代码。 我使用JBoss-eap-5.1作为App Server和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(); } 

您可以尝试这些设置(它们配对成对使用)

 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 。 有关详情,请参阅此主题 。

像kolossus说你应该使用:

 com.sun.xml.internal.ws.client.BindingProviderProperties 

和String值是:

 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-RT 2.1.4 BindingProviderProperties:

 com.sun.xml.ws.client.BindingProviderProperties 

对于REQUEST_TIMEOUT和CONNECT_TIMEOUT,您将拥有不同的String值:

 com.sun.xml.ws.request.timeout com.sun.xml.ws.connect.timeout 

对我来说,设置javax.xml.ws.client.connectionTimeoutjavax.xml.ws.client.receiveTimeout解决了这个问题。

 ((BindingProvider)port).getRequestContext().put("javax.xml.ws.client.connectionTimeout", timeout); ((BindingProvider)port).getRequestContext().put("javax.xml.ws.client.receiveTimeout", timeout); 

参考链接

升级jbossws-native库并使用StubExt.PROPERTY_CLIENT_TIMEOUT

要升级jbossws-native,请点击此链接 。

* jbossws-native-3.4.0是Jboss 5.1.0GA的最新支持版本。 您可以看到JBossWS – 支持的目标容器

这对我有用

设置以下选项对我有用。 我正在使用Metro JAXWS实现。

 ((BindingProvider)portType).getRequestContext().put(JAXWSProperties.CONNECT_TIMEOUT, 10000); ((BindingProvider) portType).getRequestContext().put(JAXWSProperties.REQUEST_TIMEOUT, 50000); 

portType是Web Service端点接口。

com.sun.xml.internal.ws.developer.JAXWSProperties中上述字段的值

 public static final java.lang.String CONNECT_TIMEOUT = "com.sun.xml.internal.ws.connect.timeout"; public static final java.lang.String REQUEST_TIMEOUT = "com.sun.xml.internal.ws.request.timeout";