master
WuXianChaoPin 7 years ago
parent f46342bc9a
commit e290dce448
  1. 5
      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

@ -29,4 +29,9 @@ public class ConnectionPoolFactory extends BasePooledObjectFactory<TTransport>{
public PooledObject<TTransport> wrap(TTransport transport) { public PooledObject<TTransport> wrap(TTransport transport) {
return new DefaultPooledObject<>(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; private String ip;
@Value("${thrift.port}") @Value("${thrift.port}")
private int port; private Integer port;
@Value("${thrift.timeout}") @Value("${thrift.timeout}")
private int timeout; private Integer timeout;
private static GenericObjectPool<TTransport> pool; private static GenericObjectPool<TTransport> pool;
@PostConstruct @PostConstruct
public void init() { public void init() {
pool = new GenericObjectPool<>(new ConnectionPoolFactory(ip, port, timeout)); pool = new GenericObjectPool<>(new ConnectionPoolFactory(ip, port, timeout));
} }
public static void doExecute(Thrift thrift) throws Exception { public static void doExecute(Thrift thrift) throws Exception {

@ -106,10 +106,5 @@ public class SpringTest {
resultActions.andDo(MockMvcResultHandlers.print()).andReturn(); 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