WebServiceUtil

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.rmi.RemoteException;

import javax.xml.rpc.ServiceException;

import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.transport.http.CommonsHttpMessageSender;
 
public class WebServiceUtil {
     /**
     * 静态调用Webservice方法
     * @param method
     * @param parms
     * @return
     * @throws ServiceException
     * @throws RemoteException
     * @throws Exception
    
    public static String callService(ServiceMethod method, Object[] parms)
            throws Exception {
     
         // throws ServiceException, RemoteException, Exception {
        String endPoint = StringUtils.removeEnd(
                method.getMethodUrl(), "?wsdl");
        String methodName = method.getMethodName();
        String nameSpace = method.getNamespace();
        String username = method.getUserName();
        String password = method.getPassword();
        String returnType = method.getReturnType();
        Service service = new Service();
        Call call = (Call)service.createCall();
    //  Call call = new Call(endPoint);
        // 设置远程webService 端点url
        call.setTargetEndpointAddress(endPoint);
        call.setTimeout(Integer.valueOf(120000));
        // 是否有普通的用户身份认证
        if (StringUtils.isNotEmpty(username)) {
            call.setUsername(username);
            call.setPassword(password);
        }
        // 是否设置命名空间(nameSpace)
        if (StringUtils.isEmpty(nameSpace))
            call.setOperationName(methodName);
        else {
            call.setOperationName(new QName(nameSpace, methodName));
        }
        // 设置方法需要的参数——参数名,类型,输入参数
        int count = 0;
        for (Object o : parms) {
            call.addParameter("arg" + count++, Constants.XSD_STRING,
                    ParameterMode.IN);
        }
        call.setReturnType(Constants.XSD_ANYSIMPLETYPE);
        // 通过返回值类型判断WebService的调用方式
        if (StringUtils.isNotEmpty(returnType)) {
            Object returnXml = call.invoke(parms);
            return (returnXml == null) ? null : returnXml.toString();
        }
        call.invokeOneWay(parms); 
        return null;
    }  */
    
    /***
     * 
     *@Description:获取web服务调用客户端
     *@Author:ouyangbiao
     *@Date:2016-9-29
     *@param callurl
     *@return
     *@throws Exception
     *@Modfiy:
     */
    public static Client getWebServiceClient(String callurl) throws Exception {
        URL url = new URL(callurl);
        HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
        httpConnection.setReadTimeout(30000);// 设置http连接的读超时,单位是毫秒
        httpConnection.setConnectTimeout(30000);
        httpConnection.connect();
        InputStream is = httpConnection.getInputStream();
        Client client = new Client(is, null);
        try {
            client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, String.valueOf(30000));// 设置发送的超时限制,单位是毫秒;
            // 解决Xfire/HttpMethod 造成Socket连接池满
            // 解决使用netstat -ano命令,发现服务器上有大量的CLOSE_WAIT连接,导致之后的WebService服务访问经常失败
            client.setProperty(CommonsHttpMessageSender.DISABLE_KEEP_ALIVE, "true");
            client.setProperty(CommonsHttpMessageSender.DISABLE_EXPECT_CONTINUE,"true");
            return client;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(is != null) {
                is.close();
            }
        }
        return null;
    }

    /***
     * 
     *@Description: 调用服务
     *@Author:ouyangbiao
     *@Date:2016-9-29
     *@param in
     *@param serviceCode
     *@param prssurl
     *@param opName
     *@return
     *@throws Exception
     *@Modfiy:
     */
    public static String callService(String url, String opName,Object... params)
            throws Exception {
        String callurl =  url;
        if(callurl.indexOf("?wsdl")<0){
            callurl=callurl+"?wsdl";
        }
        Client client = getWebServiceClient(callurl);
        Object[] result = client.invoke(opName, params);
        String rtn = result[0] == null ? "" : result[0].toString();
        return rtn;
    }
}

转载于:https://www.cnblogs.com/jakaBlog/p/10488352.html

在使用 WebService 进行远程调用时,由于网络不稳定等原因,可能会出现超时异常。为了防止这种情况,我们可以在代码中进行异常捕获并重新调用 WebService。 以下是一个示例代码: ```java public class WebServiceUtil { private static final int MAX_RETRY_TIMES = 3; // 最大重试次数 /** * 调用 WebService * * @param url WebService 地址 * @param method WebService 方法名 * @param timeout 超时时间(单位:毫秒) * @param params 参数列表 * @param <T> 返回类型 * @return 返回结果 * @throws Exception */ public static <T> T invokeWebService(String url, String method, int timeout, Object[] params, Class<T> resultType) throws Exception { T result = null; int retryTimes = 0; while (retryTimes < MAX_RETRY_TIMES) { try { // 创建服务调用工厂 ServiceFactory serviceFactory = ServiceFactory.newInstance(); Call call = (Call) serviceFactory.createCall(); // 设置 WebService 地址 call.setTargetEndpointAddress(new URL(url)); // 设置 WebService 方法名 call.setOperationName(method); // 设置 WebService 超时时间 call.setTimeout(timeout); // 设置 WebService 参数 call.setUseSOAPAction(true); call.addParameter(new QName(method), XMLType.XSD_STRING, ParameterMode.IN); call.setReturnType(XMLType.XSD_STRING); // 调用 WebService String resultStr = (String) call.invoke(params); // 解析 WebService 返回结果 JAXBContext jaxbContext = JAXBContext.newInstance(resultType); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); StringReader reader = new StringReader(resultStr); result = (T) unmarshaller.unmarshal(reader); break; } catch (Exception e) { retryTimes++; if (retryTimes == MAX_RETRY_TIMES) { throw e; } } } return result; } } ``` 在调用 WebService 的过程中,我们可以设置最大重试次数。当出现异常时,我们可以进行重试,直到达到最大重试次数或者成功调用 WebService。如果重试次数达到最大限制仍然失败,则抛出异常。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值