在用cxf 開發webservice應用時,用戶端老報
java.net.sockettimeoutexception: read timed out
原因為連接配接逾時,google 參考連結
(官方)
在spring+cxf的webservice環境下,用戶端有兩個時間屬性是可配置的,分别是connectiontimeout和receivetimeout.
connectiontimeout--webservice以tcp連接配接為基礎,這個屬性可以了解為tcp的握手時的時間設定,超過設定的時間長則認為是連接配接逾時.以毫秒為機關,預設是30000毫秒,即30秒.
receivetimeout -- 這個屬性是發送webservice的請求後等待響應的時間,超過設定的時長就認為是響應逾時.以毫秒為機關,預設是60000毫秒,即60秒.
一、在spring的配置檔案中進行設定(用戶端)。
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd ">
<http-conf:conduit name="{http://impl.service.product.super.com/}projectservice.http-conduit">
<http-conf:client connectiontimeout="10000" receivetimeout="20000"/>
</http-conf:conduit>
</beans>
這裡需要注意的有幾個地方:
需要指定http-conf名稱空間 xmlns:http-conf =http://cxf.apache.org/transports/http/configuration
指定模式位置: http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd
http-conf:conduit中的name屬性,指定設定生效的服務,如例子中,隻對服務名為{http://impl.service.product.sww.com/}projectservice的服務生效.
使用下面的設定則會對所有服務生效
< http-conf:conduit name = "*.http-conduit" >
</ http-conf:conduit >
二、通過java代碼進行設定。
client client = clientproxy.getclient(port);
httpconduit http = (httpconduit) client.getconduit();
httpclientpolicy httpclientpolicy = new httpclientpolicy();
httpclientpolicy.setconnectiontimeout(36000);
httpclientpolicy.setallowchunking(false);
httpclientpolicy.setreceivetimeout(32000);
http.setclient(httpclientpolicy);
另外,wsdl中的endpoint的位址不一定是有效的,為避免用戶端請求使用該位址,我們在請求前應通過以下方式強行設定為可用的服務位址。
((bindingprovider) port).getrequestcontext().put(bindingprovider.endpoint_address_property,serviceurl);