2012-12-20 94 views
15

我正在使用WebService客戶端,並且想要爲我的WebService調用設置超時。我嘗試了不同的方法,但仍無法做到這一點。我使用JAX-WS從WSDL進行代碼生成。我使用JBoss-eap-5.1作爲App Server和JDK1.6.0_27。我發現這些差異方法設置超時,但他們都沒有爲我工作。如何爲JAX-WS WebService調用設置超時

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."); 

現在我所做的是,我已經從一個執行者的內部調用我的web服務方法。我知道這不是一個好方法,但它爲我工作。夥計們請幫助我以正確的方式做到這一點。

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(); 
     } 

回答

18

你可以嘗試這些設置(它們是成對成對使用)

BindingProviderProperties.REQUEST_TIMEOUT 
BindingProviderProperties.CONNECT_TIMEOUT 

BindingProviderProperties應該從com.sun.xml.internal.WS.client

或者字符串for JBoss

javax.xml.ws.client.connectionTimeout 
javax.xml.ws.client.receiveTimeout 

所有物業將被放在getRequestContext() i n毫秒。

(BindingProvider)wsPort).getRequestContext().put(BindingProviderProperties.REQUEST_TIMEOUT, yourTimeoutInMillisec); 

對於JBoss而言,你可能想從org.jboss.ws.core.StubExt使用屬性StubExt.PROPERTY_CLIENT_TIMEOUT。詳情請參閱this thread

+1

StubExt.PROPERTY_CLIENT_TIMEOUT爲我工作,而是拋出異常'3 * timeoutMillisecond' 後,才針對上述領域的

值例如:如果'timeoutMillisecond = 3000'超過9000毫秒後拋出異常,但'Timeout after:3000ms'寫入日誌文件 – mariami

+0

Thanks @mariami它對我很有用。正在拋出異常,而不是在3 * timeoutMillisecond之後。儘管爲了使它工作,我不得不從與jax相關的jboss lib中刪除一些jar,否則它會給NoClassDefFoundError:javax/xml/ws/spi/Provider21。 –

2

升級將JBossWS本地庫,並使用StubExt.PROPERTY_CLIENT_TIMEOUT

升級將JBossWS本地的,按照這個link

* jbossws-native-3.4.0是Jboss 5.1.0GA的最新支持版本。你可以看到JBossWS - Supported Target Containers

這爲我工作

5

就像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-RT 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 
+0

請注意,給定的超時時間預計爲整數! – geld0r

2

˚F或者我設置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); 

參考link

1

設置下列選項爲我工作。我正在使用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";