一次項目組中需要控制超時時間,前期習慣用CXF實現,熟悉的才是最好的。所以這次依然想用CXF實現。
實現的方式代碼如下:
static{
String fvpWebserviceUrl = PropertyUtil.getConfig("fvpWebserviceUrl");
JaxWsProxyFactoryBean cxfFty = new JaxWsProxyFactoryBean();
cxfFty.setServiceClass(IWaybillQueryExtService.class);
if(StringUtils.isNotEmpty(fvpWebserviceUrl)){
cxfFty.setAddress(fvpWebserviceUrl);
service = (IWaybillQueryExtService) cxfFty.create();
setTimeoutTime(service);
}else{
logger.info("interface url"+fvpWebserviceUrl);
}
}
/**
* 設置web service 連接超時和讀取超時
* @param service
*/
private static void setTimeoutTime(Object service){
Client proxy = ClientProxy.getClient(service);
HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
HTTPClientPolicy policy = new HTTPClientPolicy();
try{
policy.setConnectionTimeout(Integer.parseInt(PropertyUtil.getConfig("connectionTimeout")));
policy.setReceiveTimeout(Integer.parseInt(PropertyUtil.getConfig("receiveTimeout")));
}catch(Exception e){
policy.setConnectionTimeout(3000);
policy.setReceiveTimeout(5000);
logger.error("",e);
}
conduit.setClient(policy);
}
需要用到的jar包為:
如果單獨運行超時機制和接口查詢都是沒有問題的,但放入項目組中就不行,后來才知是和spring的一些jar包沖突了。
CXF不行我就換了axis框架:通過下圖中的jar包程序生成了AXIS客戶端代碼,在網上下載的axis2的框架生成的代碼少文件,不知是否有用。單我是用下圖中做的。
運行命令:Java -Djava.ext.dirs=D:\axis org.apache.axis.wsdl.WSDL2Java http://xxxxx.com/ibs/services/realtime/addressClassification?wsdl -p com.my.module.ams.webservice -o e:\20150531
生成如下客戶端代碼文件:
設置超時的代碼處理,超時機制就能實現了。: