【java】resteasy 使用

import java.util.HashMap;
import java.util.Map;

import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
import org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine;
import org.springframework.beans.factory.InitializingBean;
public class RestClientUtil implements InitializingBean{

	private HttpClient httpClient;
	private ResteasyWebTarget target;
	
	//访问地址。e.g http://127.0.0.1:8080
	private String targetAddress ; 
	
	
	
	public RestClientUtil(){
		try{
			ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager();
			connectionManager.setMaxTotal(2000);
			connectionManager.setDefaultMaxPerRoute(200);

			this.httpClient =new DefaultHttpClient(connectionManager);
			((DefaultHttpClient)this.httpClient).setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler());

		}catch(Exception e){
			throw new RuntimeException(e);
		}
	}
	
	[@Override](https://my.oschina.net/u/1162528)
	public void afterPropertiesSet() throws Exception {
		ResteasyClient client = new ResteasyClientBuilder().httpEngine(new ApacheHttpClient4Engine(httpClient)).build();
		target = client.target(targetAddress);
	}
	
	
	
	private Map<Class<?>,Object> proxyMap = new HashMap<Class<?>,Object>();
	public <T> T proxy(Class<T> cls){
		T obj = (T)proxyMap.get(cls);
		synchronized(this){
			if(obj == null){
				obj = (T)target.proxy(cls);
				proxyMap.put(cls, obj);
			}
		}
		return obj;
	}
	
	public String getTargetAddress() {
		return targetAddress;
	}

	public void setTargetAddress(String targetAddress) {
		this.targetAddress = targetAddress;
	}


}

转载于:https://my.oschina.net/v512345/blog/1536722

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值