`
ruijf
  • 浏览: 69511 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

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

阅读更多
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.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://172.16.11.11:8080/webws/CalculatorPort?wsdl";
		//动态调用的客户端工厂类
		JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
		final Client client = factory.createClient(wsdlUrl);

		//设置超时单位为毫秒
		HTTPConduit http = (HTTPConduit) client.getConduit();      
		HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();      
		httpClientPolicy.setConnectionTimeout(3000);  //连接超时    
		httpClientPolicy.setAllowChunking(false);    //取消块编码 
		httpClientPolicy.setReceiveTimeout(3000);     //响应超时
		http.setClient(httpClientPolicy);
		
		//用线程池试了下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("adD", a,b);
						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

 

分享到:
评论
2 楼 wangyudong 2017-11-27  
由CXF实现的微服务需要有比较好的工具去测试RESTful API,很多REST Client是不支持自动化测试RESTful API,也不支持自动生成API文档.
之前习惯用一款名字为 WisdomTool REST Client,支持自动化测试RESTful API,输出精美的测试报告,并且自动生成精美的RESTful API文档。
轻量级的工具,功能却很精悍哦!

https://github.com/wisdomtool/rest-client

Most of REST Client tools do not support automated testing.

Once used a tool called WisdomTool REST Client supports automated testing, output exquisite report, and automatically generating RESTful API document.

Lightweight tool with very powerful features!

https://github.com/wisdomtool/rest-client
1 楼 jianhao84 2014-03-03  
在myeclipse中测试程序正常,发布之后客户端就很慢很慢,半天没反应

相关推荐

Global site tag (gtag.js) - Google Analytics