cxf动态调用webservice设置超时,测试线程安全

Java代码

import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.endpoint.dynamic.DynamicClientFactory;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;

public class WSClient {

    public static void main(String[] args) throws Exception {

        String wsdlUrl = "http://10.35.180.55:18090/services/OpenService?wsdl";
        // 动态调用的客户端工厂类
        DynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();  
        final Client client = dcf.createClient(wsdlUrl);  
        // 设置超时单位为毫秒
        HTTPConduit conduit = (HTTPConduit)client.getConduit();  
        HTTPClientPolicy policy = new HTTPClientPolicy();  
        policy.setConnectionTimeout(15000);
        policy.setAllowChunking(false);
        policy.setReceiveTimeout(30000);
        conduit.setClient(policy);
        
        final String param = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><DATA><OUTPUT><STATUS></STATUS><MESSAGE></MESSAGE></OUTPUT></DATA>";
        Object[] objects = client.invoke("openService", param);
        System.out.println(objects);

        // 用线程池试了下client对象线程安全性,发现是安全的
        ThreadPoolExecutor pool = new ThreadPoolExecutor(5, 50, 1000, TimeUnit.MICROSECONDS,
                new ArrayBlockingQueue<Runnable>(50));
        for (int i = 0; i < 100; i++) {
            pool.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        String threadName = Thread.currentThread().getName();
                        int a = new Random().nextInt(10);
                        int b = new Random().nextInt(10);
                        Object[] res = client.invoke("openService",param);
                        System.out.println(threadName + ":" + a + "+" + b + "=" + res[0]);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
    
}

结果:

pool-1-thread-37:0+5=5  
pool-1-thread-21:1+4=5  
pool-1-thread-33:6+3=9  
pool-1-thread-49:6+0=6  
pool-1-thread-42:7+1=8  
...  
...  
...  
pool-1-thread-7:7+5=12  
pool-1-thread-46:9+2=11  
pool-1-thread-17:6+2=8  
pool-1-thread-34:2+3=5  
pool-1-thread-36:8+3=11  
pool-1-thread-40:1+9=10  
pool-1-thread-26:4+4=8  
pool-1-thread-35:3+4=7 

 转载至 http://ruijf.iteye.com/blog/1186961

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值