parent
341f0f7c2f
commit
f46342bc9a
@ -0,0 +1,32 @@ |
||||
package core.thrift; |
||||
|
||||
import org.apache.commons.pool2.BasePooledObjectFactory; |
||||
import org.apache.commons.pool2.PooledObject; |
||||
import org.apache.commons.pool2.impl.DefaultPooledObject; |
||||
import org.apache.thrift.transport.TNonblockingSocket; |
||||
import org.apache.thrift.transport.TTransport; |
||||
|
||||
|
||||
public class ConnectionPoolFactory extends BasePooledObjectFactory<TTransport>{ |
||||
private String ip; |
||||
private int port; |
||||
private int socketTimeout; |
||||
|
||||
public ConnectionPoolFactory(String ip,int port,int socketTimeout) { |
||||
this.ip = ip; |
||||
this.port = port; |
||||
this.socketTimeout = socketTimeout; |
||||
} |
||||
|
||||
@Override |
||||
//创建TTransport类型对象方法
|
||||
public TTransport create() throws Exception { |
||||
return new TNonblockingSocket(ip, port,socketTimeout); |
||||
} |
||||
|
||||
//把TTransport对象打包成池管理的对象PooledObject<TTransport>
|
||||
@Override |
||||
public PooledObject<TTransport> wrap(TTransport transport) { |
||||
return new DefaultPooledObject<>(transport); |
||||
} |
||||
} |
@ -0,0 +1,28 @@ |
||||
package core.thrift; |
||||
|
||||
import org.apache.thrift.async.AsyncMethodCallback; |
||||
|
||||
public class MyAsyncMethodCallback<E> implements AsyncMethodCallback<E> { |
||||
|
||||
private boolean onComplete=true; |
||||
|
||||
private E result; |
||||
|
||||
public E getResult() throws InterruptedException { |
||||
while (onComplete){ |
||||
Thread.sleep(100); |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
@Override |
||||
public void onComplete(E o) { |
||||
onComplete=false; |
||||
result=o; |
||||
} |
||||
|
||||
@Override |
||||
public void onError(Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
@ -1,61 +0,0 @@ |
||||
package core.thrift; |
||||
|
||||
import core.thrift.comment.QueryComment; |
||||
import org.apache.logging.log4j.LogManager; |
||||
import org.apache.logging.log4j.Logger; |
||||
import org.apache.thrift.protocol.TBinaryProtocol; |
||||
import org.apache.thrift.protocol.TProtocol; |
||||
import org.apache.thrift.transport.TFramedTransport; |
||||
import org.apache.thrift.transport.TSocket; |
||||
import org.apache.thrift.transport.TTransport; |
||||
|
||||
import java.util.List; |
||||
|
||||
public class ThriftClientDemo { |
||||
|
||||
private static final String host="127.0.0.1"; |
||||
private static final int port=2233; |
||||
private static Logger log=LogManager.getLogger(); |
||||
private static final TTransport tTransport = getTTransport(host, port, 5000); |
||||
|
||||
public static List<Integer> commentSumList(List<Integer> cids) throws Exception { |
||||
List<Integer> result= client().commentSumList(cids); |
||||
log.info("查询结果:"+result); |
||||
return result; |
||||
} |
||||
|
||||
public static int commentSum(int cid) throws Exception { |
||||
int result=client().commentSum(cid); |
||||
log.info("查询结果:"+result); |
||||
return result; |
||||
} |
||||
|
||||
public static String downloadXml(List<Integer> cids,String fileName) throws Exception { |
||||
String result=client().download(cids,fileName); |
||||
log.info("查询结果:"+result); |
||||
return result; |
||||
} |
||||
|
||||
private static QueryComment.Client client() throws Exception { |
||||
TTransport tTransport = getTTransport(); |
||||
TProtocol protocol = new TBinaryProtocol(tTransport); |
||||
return new QueryComment.Client(protocol); |
||||
} |
||||
|
||||
public static TTransport getTTransport() throws Exception{ |
||||
try{ |
||||
if(!tTransport.isOpen()){ |
||||
tTransport.open(); |
||||
} |
||||
return tTransport; |
||||
}catch(Exception e){ |
||||
e.printStackTrace(); |
||||
} |
||||
return null; |
||||
} |
||||
private static TTransport getTTransport(String host, int port, int timeout) { |
||||
final TSocket tSocket = new TSocket(host, port, timeout); |
||||
final TTransport transport = new TFramedTransport(tSocket); |
||||
return transport; |
||||
} |
||||
} |
@ -0,0 +1,53 @@ |
||||
package core.thrift; |
||||
|
||||
import core.thrift.comment.QueryComment; |
||||
import org.apache.commons.pool2.impl.GenericObjectPool; |
||||
import org.apache.thrift.async.TAsyncClientManager; |
||||
import org.apache.thrift.protocol.TBinaryProtocol; |
||||
import org.apache.thrift.protocol.TProtocolFactory; |
||||
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; |
||||
|
||||
@Service |
||||
public class ThriftClientPool { |
||||
|
||||
@Value("${thrift.ip}") |
||||
private String ip; |
||||
|
||||
@Value("${thrift.port}") |
||||
private int port; |
||||
|
||||
@Value("${thrift.timeout}") |
||||
private int 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 { |
||||
//从池里获取一个Transport对象
|
||||
TTransport transport = pool.borrowObject(); |
||||
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); |
||||
} |
||||
|
||||
public interface Thrift { |
||||
void doExecute(QueryComment.AsyncClient client) throws Exception; |
||||
} |
||||
|
||||
} |
||||
|
||||
|
||||
|
||||
|
@ -1,13 +0,0 @@ |
||||
|
||||
|
||||
# |
||||
# |
||||
##\u6570\u636E\u6E901 |
||||
#hibernate.connection.url1=jdbc:mysql://sukura.top:3306/bilibili?serverTimezone=UTC&useSSL=false |
||||
#hibernate.connection.username1=sukura |
||||
#hibernate.connection.password1=@ |
||||
# |
||||
##\u6570\u636E\u6E902 |
||||
#hibernate.connection.url2=jdbc:mysql://mysql.sukura.top:8635/bilibili?serverTimezone=UTC&useSSL=false |
||||
#hibernate.connection.username2=sukura |
||||
#hibernate.connection.password2=Luffy9412! |
Loading…
Reference in new issue