|
|
|
@ -1,16 +1,21 @@ |
|
|
|
|
package core.thrift; |
|
|
|
|
|
|
|
|
|
import core.thrift.comment.QueryComment; |
|
|
|
|
import org.apache.commons.pool2.impl.GenericObjectPool; |
|
|
|
|
import org.apache.thrift.async.TAsyncClient; |
|
|
|
|
import org.apache.thrift.async.TAsyncClientManager; |
|
|
|
|
import org.apache.thrift.protocol.TBinaryProtocol; |
|
|
|
|
import org.apache.thrift.protocol.TProtocolFactory; |
|
|
|
|
import org.apache.thrift.transport.TNonblockingSocket; |
|
|
|
|
import org.apache.thrift.transport.TNonblockingTransport; |
|
|
|
|
import org.apache.thrift.transport.TTransport; |
|
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
|
|
import javax.annotation.PostConstruct; |
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.lang.reflect.Constructor; |
|
|
|
|
import java.lang.reflect.InvocationTargetException; |
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
@Service |
|
|
|
|
public class ThriftClientPool { |
|
|
|
@ -24,29 +29,37 @@ public class ThriftClientPool { |
|
|
|
|
@Value("${thrift.timeout}") |
|
|
|
|
private Integer timeout; |
|
|
|
|
|
|
|
|
|
private static GenericObjectPool<TTransport> pool; |
|
|
|
|
private static GenericObjectPool<TNonblockingSocket> pool; |
|
|
|
|
|
|
|
|
|
private static final Map<Class<? extends TAsyncClient>, Constructor> map = new HashMap<>(); |
|
|
|
|
|
|
|
|
|
@PostConstruct |
|
|
|
|
public void init() { |
|
|
|
|
pool = new GenericObjectPool<>(new ConnectionPoolFactory(ip, port, timeout)); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static void doExecute(Thrift thrift) throws Exception { |
|
|
|
|
public static <E extends TAsyncClient> void doExecute(Thrift<E> thrift,Class<E> c) throws Exception { |
|
|
|
|
//从池里获取一个Transport对象
|
|
|
|
|
TTransport transport = pool.borrowObject(); |
|
|
|
|
TNonblockingSocket socket = pool.borrowObject(); |
|
|
|
|
thrift.doExecute(client(c,socket)); |
|
|
|
|
//把一个Transport对象归还到池里
|
|
|
|
|
pool.returnObject(socket); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static <E extends TAsyncClient> E client(Class<E> c, TNonblockingSocket socket) throws NoSuchMethodException, IOException, IllegalAccessException, InvocationTargetException, InstantiationException { |
|
|
|
|
if (!map.containsKey(c)) { |
|
|
|
|
map.put(c, c.getConstructor(TProtocolFactory.class, TAsyncClientManager.class, TNonblockingTransport.class)); |
|
|
|
|
} |
|
|
|
|
TAsyncClientManager clientManager = new TAsyncClientManager(); |
|
|
|
|
TProtocolFactory protocol = new TBinaryProtocol.Factory(); |
|
|
|
|
QueryComment.AsyncClient asyncClient = new QueryComment.AsyncClient(protocol, clientManager, (TNonblockingTransport) transport); |
|
|
|
|
thrift.doExecute(asyncClient); |
|
|
|
|
//把一个Transport对象归还到池里
|
|
|
|
|
pool.returnObject(transport); |
|
|
|
|
return (E) map.get(c).newInstance(protocol,clientManager,socket); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public interface Thrift { |
|
|
|
|
void doExecute(QueryComment.AsyncClient client) throws Exception; |
|
|
|
|
public interface Thrift<E extends TAsyncClient> { |
|
|
|
|
void doExecute(E client) throws Exception; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|