@Override public void afterPropertiesSet() throws Exception { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); // 加载Iface接口 objectClass = classLoader.loadClass(serverAddressProvider.getService() + "$Iface"); // 加载Client.Factory类 Class<TServiceClientFactory<TServiceClient>> fi = (Class<TServiceClientFactory<TServiceClient>>) classLoader. loadClass(serverAddressProvider.getService() + "$Client$Factory"); TServiceClientFactory<TServiceClient> clientFactory = fi.newInstance(); ThriftClientPoolFactory clientPool = new ThriftClientPoolFactory(serverAddressProvider, clientFactory, callback); pool = new GenericObjectPool<TServiceClient>(clientPool, makePoolConfig()); // InvocationHandler handler = makeProxyHandler();//方式1 InvocationHandler handler = makeProxyHandler2();//方式2 proxyClient = Proxy.newProxyInstance(classLoader, new Class[] { objectClass }, handler); }
@Override public T getObject() throws Exception { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); Server server = serverManager.getService(innerClass.getName()); // 加载Iface接口 innerClass = classLoader.loadClass(server.getName()); String temp = server.getName().replace("$Iface", ""); // 加载Client.Factory类 Class<TServiceClientFactory<TServiceClient>> fi = (Class<TServiceClientFactory<TServiceClient>>) classLoader. loadClass(temp + "$Client$Factory"); TServiceClientFactory<TServiceClient> clientFactory = fi.newInstance(); ThriftClientPoolFactory clientPool = new ThriftClientPoolFactory(server, clientFactory, callback, proccessName); pool = new GenericObjectPool<TServiceClient>(clientPool, makePoolConfig()); ServiceProxy serviceProxy = new ServiceProxy(pool); return (T)Proxy.newProxyInstance(innerClass.getClassLoader(),new Class[]{innerClass}, serviceProxy); }
/** * Creates a client based on the TypeToken pointing to a Thrift interface class. * * This method uses black magic, based on the structure and names of generated * cc.ecl.action.thrift classes. If that changes, this here breaks. * * @throws TTransportException if transport creation fails (e.g. timeout) * @throws ThriftFactoryException if factory creation fails (probably bad ifaceType parameter, * or Thrift internals changed) */ @Override public <IFaceT> IFaceT initializeClient(String url, TypeToken<IFaceT> ifaceType) throws TTransportException, ThriftFactoryException { // assume same class loader ClassLoader classLoader = ifaceType.getRawType().getClassLoader(); String factoryName = ifaceType.toString().replace("Iface", "Client$Factory"); try { if (!clientFactoryMappings.containsKey(ifaceType)) { clientFactoryMappings.put(ifaceType, (TServiceClientFactory<?>) classLoader .loadClass(factoryName).newInstance()); } } catch (Exception e) { throw new RuntimeException("IllegalAccessException while initializing: " + factoryName, e); } return (IFaceT) clientFactoryMappings.get(ifaceType).getClient(requestTransport(url)); }
public ThriftClientPoolFactory(Server server, TServiceClientFactory<TServiceClient> clientFactory, PoolOperationCallBack callback, String proccessName) throws Exception { this.server = server; this.clientFactory = clientFactory; this.callback = callback; this.proccessName = proccessName; }
protected ThriftClientPoolFactory(ThriftServerAddressProvider addressProvider, TServiceClientFactory<TServiceClient> clientFactory) throws Exception { this.serverAddressProvider = addressProvider; this.clientFactory = clientFactory; }
protected ThriftClientPoolFactory(ThriftServerAddressProvider addressProvider, TServiceClientFactory<TServiceClient> clientFactory, PoolOperationCallBack callback) throws Exception { this.serverAddressProvider = addressProvider; this.clientFactory = clientFactory; this.callback = callback; }