master
WuXianChaoPin 7 years ago
parent f46342bc9a
commit e290dce448
  1. 11
      core/src/main/java/core/thrift/ConnectionPoolFactory.java
  2. 5
      core/src/main/java/core/thrift/ThriftClientPool.java
  3. 7
      web/src/test/java/SpringTest.java
  4. 37
      web/src/test/java/ThriftTest.java

@ -7,12 +7,12 @@ import org.apache.thrift.transport.TNonblockingSocket;
import org.apache.thrift.transport.TTransport;
public class ConnectionPoolFactory extends BasePooledObjectFactory<TTransport>{
public class ConnectionPoolFactory extends BasePooledObjectFactory<TTransport> {
private String ip;
private int port;
private int socketTimeout;
public ConnectionPoolFactory(String ip,int port,int socketTimeout) {
public ConnectionPoolFactory(String ip, int port, int socketTimeout) {
this.ip = ip;
this.port = port;
this.socketTimeout = socketTimeout;
@ -21,7 +21,7 @@ public class ConnectionPoolFactory extends BasePooledObjectFactory<TTransport>{
@Override
//创建TTransport类型对象方法
public TTransport create() throws Exception {
return new TNonblockingSocket(ip, port,socketTimeout);
return new TNonblockingSocket(ip, port, socketTimeout);
}
//把TTransport对象打包成池管理的对象PooledObject<TTransport>
@ -29,4 +29,9 @@ public class ConnectionPoolFactory extends BasePooledObjectFactory<TTransport>{
public PooledObject<TTransport> wrap(TTransport transport) {
return new DefaultPooledObject<>(transport);
}
@Override
public boolean validateObject(PooledObject<TTransport> p) {
return p.getObject().isOpen();
}
}

@ -19,16 +19,17 @@ public class ThriftClientPool {
private String ip;
@Value("${thrift.port}")
private int port;
private Integer port;
@Value("${thrift.timeout}")
private int timeout;
private Integer timeout;
private static GenericObjectPool<TTransport> pool;
@PostConstruct
public void init() {
pool = new GenericObjectPool<>(new ConnectionPoolFactory(ip, port, timeout));
}
public static void doExecute(Thrift thrift) throws Exception {

@ -106,10 +106,5 @@ public class SpringTest {
resultActions.andDo(MockMvcResultHandlers.print()).andReturn();
}
@Test
public void thrift() throws Exception {
MyAsyncMethodCallback<Integer> call= new MyAsyncMethodCallback<>();
ThriftClientPool.doExecute(client -> client.commentSum(49052,call));
log.info(call.getResult());
}
}

@ -0,0 +1,37 @@
import core.thrift.MyAsyncMethodCallback;
import core.thrift.ThriftClientPool;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
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 web.config.SpringConfig;
import java.util.ArrayList;
//让测试运行于Spring环境
@RunWith(SpringJUnit4ClassRunner.class)
@SpringJUnitWebConfig
@ContextConfiguration(classes = {SpringConfig.class})
public class ThriftTest {
private static Logger log = LogManager.getLogger();
@Test
public void commentSum() throws Exception {
MyAsyncMethodCallback<Integer> call = new MyAsyncMethodCallback<>();
ThriftClientPool.doExecute(client -> client.commentSum(49052, call));
log.info(call.getResult());
}
@Test
public void download() throws Exception {
MyAsyncMethodCallback<String> call = new MyAsyncMethodCallback<>();
ThriftClientPool.doExecute(client ->client.download(new ArrayList<Integer>(){{
add(49052);
}},"test",call));
log.info(call.getResult());
}
}
Loading…
Cancel
Save