You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
webcrawler/core/src/main/java/core/thrift/ThriftClientDemo.java

61 lines
2.0 KiB

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;
}
}