parent
ab19a7e43c
commit
14e3171722
@ -0,0 +1,72 @@ |
||||
package core.thrift; |
||||
|
||||
import org.apache.commons.pool2.impl.GenericObjectPool; |
||||
import org.apache.thrift.TServiceClient; |
||||
import org.apache.thrift.async.TAsyncClient; |
||||
import org.apache.thrift.async.TAsyncClientManager; |
||||
import org.apache.thrift.protocol.TBinaryProtocol; |
||||
import org.apache.thrift.protocol.TProtocol; |
||||
import org.apache.thrift.protocol.TProtocolFactory; |
||||
import org.apache.thrift.transport.TNonblockingSocket; |
||||
import org.apache.thrift.transport.TNonblockingTransport; |
||||
import org.apache.thrift.transport.TSocket; |
||||
import org.apache.thrift.transport.TTransport; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import java.lang.reflect.Constructor; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
@Component |
||||
public class FactoryPool { |
||||
private GenericObjectPool<TNonblockingSocket> tNonblockingSocketPool; |
||||
|
||||
private GenericObjectPool<TSocket> tSocketPool; |
||||
|
||||
|
||||
public FactoryPool(TNonblockingSocketPool pool1, TSocketPool pool2) { |
||||
this.tNonblockingSocketPool = new GenericObjectPool<>(pool1); |
||||
this.tSocketPool = new GenericObjectPool<>(pool2); |
||||
} |
||||
|
||||
private final Map<Class, Constructor> clientMap = new HashMap<>(); |
||||
|
||||
public <C extends TAsyncClient> void doExecute(Class<C> c, AsyncClient<C> thrift) throws Exception { |
||||
//从池里获取一个Transport对象
|
||||
TNonblockingSocket trans = tNonblockingSocketPool.borrowObject(); |
||||
thrift.doExecute(client(c, trans)); |
||||
//把一个Transport对象归还到池里
|
||||
tNonblockingSocketPool.returnObject(trans); |
||||
} |
||||
|
||||
public <C extends TServiceClient> void doExecute(Class<C> c, ServiceClient<C> thrift) throws Exception{ |
||||
TSocket trans=tSocketPool.borrowObject(); |
||||
thrift.doExecute(client(c,trans)); |
||||
tSocketPool.returnObject(trans); |
||||
} |
||||
|
||||
private interface Thrift<E> { |
||||
void doExecute(E client) throws Exception; |
||||
} |
||||
|
||||
public interface AsyncClient<E extends TAsyncClient> extends Thrift<E>{} |
||||
|
||||
public interface ServiceClient<E extends TServiceClient> extends Thrift<E>{} |
||||
|
||||
public <T extends TTransport,C> C client(Class<C> c, T trans) throws Exception { |
||||
if (trans instanceof TNonblockingTransport) { |
||||
if (!clientMap.containsKey(c)) { |
||||
clientMap.put(c, c.getConstructor(TProtocolFactory.class, TAsyncClientManager.class, TNonblockingTransport.class)); |
||||
} |
||||
TProtocolFactory protocol = new TBinaryProtocol.Factory(); |
||||
TAsyncClientManager clientManager = new TAsyncClientManager(); |
||||
return (C) clientMap.get(c).newInstance(protocol, clientManager, trans); |
||||
} else { |
||||
if (!clientMap.containsKey(c)) { |
||||
clientMap.put(c, c.getConstructor(TProtocol.class)); |
||||
} |
||||
TBinaryProtocol protocol = new TBinaryProtocol(trans); |
||||
return (C) clientMap.get(c).newInstance(protocol); |
||||
} |
||||
} |
||||
} |
@ -1,85 +1,17 @@ |
||||
package core.thrift; |
||||
|
||||
import org.apache.commons.pool2.impl.GenericObjectPool; |
||||
import org.apache.thrift.TServiceClient; |
||||
import org.apache.thrift.async.TAsyncClient; |
||||
import org.apache.thrift.async.TAsyncClientManager; |
||||
import org.apache.thrift.protocol.TBinaryProtocol; |
||||
import org.apache.thrift.protocol.TProtocol; |
||||
import org.apache.thrift.protocol.TProtocolFactory; |
||||
import org.apache.thrift.transport.TNonblockingSocket; |
||||
import org.apache.thrift.transport.TNonblockingTransport; |
||||
import org.apache.thrift.transport.TSocket; |
||||
import org.apache.thrift.transport.TTransport; |
||||
import org.springframework.beans.factory.annotation.Value; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import javax.annotation.PostConstruct; |
||||
import java.lang.reflect.Constructor; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
@Component |
||||
public class SocketConfig { |
||||
@Value("${thrift.ip}") |
||||
private String ip; |
||||
public String ip; |
||||
|
||||
@Value("${thrift.port}") |
||||
private Integer port; |
||||
public Integer port; |
||||
|
||||
@Value("${thrift.timeout}") |
||||
private Integer timeout; |
||||
|
||||
|
||||
private GenericObjectPool<TNonblockingSocket> tNonblockingSocketPool; |
||||
|
||||
private GenericObjectPool<TSocket> tSocketPool; |
||||
|
||||
|
||||
private final Map<Class, Constructor> clientMap = new HashMap<>(); |
||||
|
||||
@PostConstruct |
||||
public void init(){ |
||||
tNonblockingSocketPool=new GenericObjectPool<>(new TNonblockingSocketPool(ip,port,timeout)); |
||||
tSocketPool=new GenericObjectPool<>(new TSocketPool(ip,port,timeout)); |
||||
} |
||||
|
||||
public <C extends TAsyncClient> void doExecute(Class<C> c, AsyncClient<C> thrift) throws Exception { |
||||
//从池里获取一个Transport对象
|
||||
TNonblockingSocket trans = tNonblockingSocketPool.borrowObject(); |
||||
thrift.doExecute(client(c, trans)); |
||||
//把一个Transport对象归还到池里
|
||||
tNonblockingSocketPool.returnObject(trans); |
||||
} |
||||
|
||||
public <C extends TServiceClient> void doExecute(Class<C> c,ServiceClient<C> thrift) throws Exception{ |
||||
TSocket trans=tSocketPool.borrowObject(); |
||||
thrift.doExecute(client(c,trans)); |
||||
tSocketPool.returnObject(trans); |
||||
} |
||||
|
||||
private interface Thrift<E> { |
||||
void doExecute(E client) throws Exception; |
||||
} |
||||
|
||||
public interface AsyncClient<E extends TAsyncClient> extends Thrift<E>{} |
||||
|
||||
public interface ServiceClient<E extends TServiceClient> extends Thrift<E>{} |
||||
public Integer timeout; |
||||
|
||||
public <T extends TTransport,C> C client(Class<C> c, T trans) throws Exception { |
||||
if (trans instanceof TNonblockingTransport) { |
||||
if (!clientMap.containsKey(c)) { |
||||
clientMap.put(c, c.getConstructor(TProtocolFactory.class, TAsyncClientManager.class, TNonblockingTransport.class)); |
||||
} |
||||
TProtocolFactory protocol = new TBinaryProtocol.Factory(); |
||||
TAsyncClientManager clientManager = new TAsyncClientManager(); |
||||
return (C) clientMap.get(c).newInstance(protocol, clientManager, trans); |
||||
} else { |
||||
if (!clientMap.containsKey(c)) { |
||||
clientMap.put(c, c.getConstructor(TProtocol.class)); |
||||
} |
||||
TBinaryProtocol protocol = new TBinaryProtocol(trans); |
||||
return (C) clientMap.get(c).newInstance(protocol); |
||||
} |
||||
} |
||||
} |
||||
|
@ -0,0 +1,12 @@ |
||||
package spring.methodreplace; |
||||
|
||||
import org.springframework.stereotype.Component; |
||||
|
||||
@Component |
||||
public class Add { |
||||
|
||||
|
||||
public void add(){ |
||||
System.out.println("Add"); |
||||
} |
||||
} |
@ -0,0 +1,25 @@ |
||||
package spring.methodreplace; |
||||
|
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.springframework.test.context.ContextConfiguration; |
||||
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig; |
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
||||
|
||||
import javax.annotation.Resource; |
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class) |
||||
@SpringJUnitWebConfig |
||||
@ContextConfiguration() |
||||
public class SpringTest { |
||||
|
||||
@Resource |
||||
private Add add; |
||||
|
||||
@Test |
||||
public void test1(){ |
||||
add.add(); |
||||
} |
||||
|
||||
} |
||||
|
Loading…
Reference in new issue