Java 如何设置JAX-WSWebService调用的超时
我正在处理一个WebService客户端,我想为我的WebService调用设置一个超时。我尝试过不同的方法,但仍然无法做到这一点。我使用JAX-WS从WSDL生成代码。我使用JBoss-eap-5.1作为应用服务器,使用JDK1.6.0_27。我找到了这些设置超时的不同方法,但没有一种适合我Java 如何设置JAX-WSWebService调用的超时,java,web-services,jakarta-ee,jboss,jax-ws,Java,Web Services,Jakarta Ee,Jboss,Jax Ws,我正在处理一个WebService客户端,我想为我的WebService调用设置一个超时。我尝试过不同的方法,但仍然无法做到这一点。我使用JAX-WS从WSDL生成代码。我使用JBoss-eap-5.1作为应用服务器,使用JDK1.6.0_27。我找到了这些设置超时的不同方法,但没有一种适合我 URL mbr_service_url = new URL(null,GlobalVars.MemberService_WSDL, new URLStreamHandler() {
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
应该来自com.sun.xml.internal.WS.client
或字符串:
要放置在getRequestContext()
上的所有属性(毫秒)
(BindingProvider)wsPort).getRequestContext().put(BindingProviderProperties.REQUEST_TIMEOUT, yourTimeoutInMillisec);
特别是对于JBoss,您可能希望使用org.JBoss.ws.core.StubExt中的属性
StubExt.property\u CLIENT\u TIMEOUT
。有关详细信息,请参阅。升级jbossws本机库并使用StubExt.PROPERTY\u CLIENT\u超时
要升级jbossws-native,请遵循以下步骤
*jbossws-native-3.4.0是Jboss 5.1.0GA支持的最新版本。你可以看到
这对我很有用,就像科洛索斯说的那样,你应该使用:
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-RT 2.1.4 BindingProviderProperties,则常量值是不同的:
com.sun.xml.ws.client.BindingProviderProperties
对于请求超时和连接超时,您将有不同的字符串值:
com.sun.xml.ws.request.timeout
com.sun.xml.ws.connect.timeout
对我来说,设置
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);
请参阅设置以下选项对我有效。我正在使用Metro JAXWS实现
((BindingProvider)portType).getRequestContext().put(JAXWSProperties.CONNECT_TIMEOUT, 10000);
((BindingProvider) portType).getRequestContext().put(JAXWSProperties.REQUEST_TIMEOUT, 50000);
portType是Web服务端点接口
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";
我有一个具有以下环境的旧安装运行时: Jdk-1.5、Jboss-4.2.3.GA和WSClient是由JAX-WS规范2.0创建的 要激活Soap请求超时,我使用以下代码
((BindingProvider)port.getRequestContext().put(org.jboss.ws.core.StubExt.PROPERTY_CLIENT_TIMEOUT,String.valueOf(readTimeout))代码>
jarjbossws client.jar
复制到jboss-4.2.3.GA\server\default\lib\
StubExt.PROPERTY\u client\u TIMEOUT对我有效,但异常只有在3*timeoutmillis秒
之后才会抛出,例如:如果timeoutmillis秒=3000
则异常在9000毫秒之后抛出,但是超时时间:3000ms
写在日志文件中感谢@mariami它对我有用。异常是在适当的时间抛出的,而不是在3*TimeOut毫秒之后。尽管为了让它正常工作,我不得不从jboss库中删除一些与jax相关的JAR,否则它会给出NoClassDefFoundError:javax/xml/ws/spi/Provider21。请注意,给定的超时应该是整数!
((BindingProvider)portType).getRequestContext().put(JAXWSProperties.CONNECT_TIMEOUT, 10000);
((BindingProvider) portType).getRequestContext().put(JAXWSProperties.REQUEST_TIMEOUT, 50000);
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";