Java tutorial
package com.soapsnake.thrift.license.rpc; import org.apache.commons.pool.impl.GenericObjectPool; import org.apache.thrift.TServiceClient; import org.apache.thrift.TServiceClientFactory; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.cglib.proxy.Proxy; /** * Created by soapsnake on 2017/6/4. */ public class ThriftServiceClientProxyFactory implements FactoryBean, InitializingBean { private String service; private String serverAddress; // private ThriftServerAddressProvider addressProvider; private Integer maxActive; private Integer idleTime; private Object proxyClient; private Class objectClass; private GenericObjectPool<TServiceClient> pool; private PoolOperationCallback callback = new PoolOperationCallback() { @Override public void destory(TServiceClient client) { System.out.println("destory"); } @Override public void make(TServiceClient client) { System.out.println("create"); } }; public String getService() { return service; } public void setService(String service) { this.service = service; } public String getServerAddress() { return serverAddress; } public void setServerAddress(String serverAddress) { this.serverAddress = serverAddress; } public Integer getMaxActive() { return maxActive; } public void setMaxActive(Integer maxActive) { this.maxActive = maxActive; } public Integer getIdleTime() { return idleTime; } public void setIdleTime(Integer idleTime) { this.idleTime = idleTime; } public Object getProxyClient() { return proxyClient; } public void setProxyClient(Object proxyClient) { this.proxyClient = proxyClient; } @Override public Object getObject() throws Exception { return proxyClient; } @Override public Class<?> getObjectType() { return objectClass; } @Override public boolean isSingleton() { return true; } @Override public void afterPropertiesSet() throws Exception { // zookeeper???,? // if (serverAddress != null){ // addressProvider = new FixedAddressProvider(serverAddress); // } ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); //?thrift serviceclass,,??class objectClass = classLoader.loadClass(service + "$Iface"); //?? Class<TServiceClientFactory<TServiceClient>> fi = (Class<TServiceClientFactory<TServiceClient>>) classLoader .loadClass(service + "$Client$Factory"); //TServiceClientFactory TServiceClientFactory<TServiceClient> clientFactory = fi.newInstance(); // ThriftClientPoolFactory clientPool = new ThriftClientPoolFactory(serverAddress, clientFactory, callback); // GenericObjectPool.Config poolConfig = new GenericObjectPool.Config(); poolConfig.maxActive = maxActive; poolConfig.minIdle = 0; poolConfig.minEvictableIdleTimeMillis = idleTime; poolConfig.timeBetweenEvictionRunsMillis = idleTime / 2L; //:ThriftClientPoolFactory,config pool = new GenericObjectPool<>(clientPool, poolConfig); proxyClient = Proxy.newProxyInstance(classLoader, new Class[] { objectClass }, (o, method, objects) -> { //pool?client TServiceClient client = pool.borrowObject(); try { return method.invoke(client, objects); } catch (Exception e) { throw e; } finally { pool.returnObject(client); } }); } static interface PoolOperationCallback { void destory(TServiceClient client); void make(TServiceClient client); } }