webservice客戶端超時
阿新 • • 發佈:2019-02-09
使用axis2生成webserice客戶端時,在引用的jar包axis2-kernel下的org.apache.axis2.client.Options類中預設設定通訊超時時間為30s,jar包原始碼如下,可以看到預設時間為final常量:
為了在外部能夠設定超時時間,添加了一下程式碼【預設超時時間同jar包中原來30s同】:package org.apache.axis2.client; public class Options implements Externalizable,SafeSerializable{ public static final int DEFAULT_TIMEOUT_MILLISECONDS=30000; private long timeOutInMilliSeconds=-1L; public long getTimeOutInMilliSeconds(){ if((this.timeOutInMilliSeconds==-1L)&&(this.parent!=null)){ return this.parent.getTimeOutInMilliSeconds(); } return this.parent.getTimeOutInMilliSeconds()==-1L?30000L:this.timeOutInMilliSeconds; } }
在客戶端中手動設定超時時間,例如使用AXIS2生成的客戶端【比如ReqS500000001Stub類】private long TimeOut=30000L; public long getTimeOut(){ return TimeOut; } public void setTimeOut(long timeOut){ if(TimeOut>0L){ TimeOut=timeOut; }else{ TimeOut=30000L; } } public long getTimeOutInMilliSeconds(){ if((this.timeOutInMilliSeconds==-1L)&&(this.parent!=null)){ return this.parent.getTimeOutInMilliSeconds(); } return this.parent.getTimeOutInMilliSeconds()==-1L?getTimeOut():this.timeOutInMilliSeconds; }
在其構造方法中找到ServiceClient類的例項,有如下程式碼
_serviceClient=new org.apache.axis2.client.ServiceClient(configurationContext,_service);
然後手動新增超時時間程式碼【注意資料型別】,比如設定為60s:_serviceClient.getOptions().setTimeOut(60000L)
現在超時時間就能按照實際需求進行設定了。僅用交流,如果不妥,望指正