我正在开发一个 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() {
@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();
}