webservice axis2客户端调用设置超时时间

axis2版本 :1.6.2

import java.rmi.RemoteException;

import nc.pubitf.para.SysInitQuery;
import nc.vo.pub.BusinessException;

import org.apache.axis2.AxisFault;
import org.apache.axis2.client.Options;
import org.apache.axis2.transport.http.HTTPConstants;
import org.tempuri.HQSWcfServiceCallHQSWcfServiceCallSoapStub;

import com.yonyou.yh.nhis.bd.util.SysConfigParamUtil;
import com.yonyouhealth.uaph.framework.comm.util.StringUtils;


public class ChinaShineUtils {

    private static HQSWcfServiceCallHQSWcfServiceCallSoapStub stub;
    private static HQSWcfServiceCallHQSWcfServiceCallSoapStub getServiceStub() throws AxisFault, BusinessException{
        try {
            stub = null==stub? new HQSWcfServiceCallHQSWcfServiceCallSoapStub() : stub;
            
            Options options = stub. _getServiceClient().getOptions();
            options.setProperty(HTTPConstants.CONNECTION_TIMEOUT, 2 * 1000);//设置服务端方法超时时间(单位是毫秒)
            options.setProperty(HTTPConstants.SO_TIMEOUT, 3 * 1000);//设置等待客户连接的超时时间(单位是毫秒)
            
            stub._getServiceClient().setOptions(options);//设置

            
        } catch (AxisFault e) {
            throw new AxisFault("接口调用超时,接口返回信息:"+ e.getMessage());
        }catch(Exception e){
            throw new BusinessException("接口调用失败,接口返回信息:"+ e.getMessage());
        }
        return stub;
    }
    
    private static HQSWcfServiceCallHQSWcfServiceCallSoapStub getServiceStub(String pk_group) throws AxisFault, BusinessException{
        try {
            if(StringUtils.hasText(pk_group)){
                String url = SysInitQuery.getParaString(pk_group, "NHISPV014");
                if(StringUtils.hasText(url) ){
                    stub = null==stub? new HQSWcfServiceCallHQSWcfServiceCallSoapStub(url) : stub;
                    
                    String conn_timeout_str = new SysConfigParamUtil().getPubSysInitByGroup(pk_group, "NHISPV022");
                    Integer connection_timeout = StringUtils.hasText(conn_timeout_str)? Integer.valueOf( conn_timeout_str) : 2000 ;
                    
                    String so_timeout_str = new SysConfigParamUtil().getPubSysInitByGroup(pk_group, "NHISPV023");
                    Integer so_timeout = StringUtils.hasText(so_timeout_str)?  Integer.valueOf(so_timeout_str) : 3000;
                    
                    Options options = stub._getServiceClient().getOptions();
                    options.setProperty(HTTPConstants.CONNECTION_TIMEOUT, connection_timeout);//设置服务端方法超时时间(单位是毫秒)
                    options.setProperty(HTTPConstants.SO_TIMEOUT, so_timeout);//等待客户连接的超时时间(单位是毫秒)
                    
                    stub._getServiceClient().setOptions(options);//设置

                }else{
                    stub = getServiceStub();
                }
            }else{
                stub = getServiceStub();
            }
        } catch (AxisFault e) {
            throw new AxisFault("接口调用超时,接口返回信息:"+ e.getMessage());
        }catch(Exception e){
            throw new BusinessException("接口调用失败,接口返回信息:"+ e.getMessage());
        }
        return stub;
    }

猜你喜欢

转载自blog.csdn.net/m0_37488170/article/details/93461840