import java.lang.Thread.State;
import java.util.Iterator;
import java.util.List;
import org.apache.thrift.TException;
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 org.apache.thrift.transport.TTransportException;
import tdbservice.ErrorReturn;
import tdbservice.TDB_ReqTLine;
import tdbservice.TDB_TLine;
import tdbservice.TdbService;
public class Client {
private TTransport transport ;
private TdbService.Client client;
private String result = new String();
private String url;
private int port;
public Client(String url,int port) throws TTransportException{
this.url = url;
this.port = port;
// transport = new TSocket(url, port);
transport = new TFramedTransport(new TSocket(this.url, this.port));
//以上兩行為兩種不同transport,書寫方法主要取決於服務端的書寫格式
if(!transport.isOpen()){
TProtocol protocol = new TBinaryProtocol(transport);
client = new TdbService.Client(protocol);//此處的TdbService的調用由服務端來決定
transport.open();
}
State state = Thread.currentThread().getState();
String threadName = Thread.currentThread().getName();
System.out.println(threadName +"--------"+ state.toString());
}
public TTransport getTTransport(){
return transport;
}
public String testGetAllStock(){
try {
result = client.get_allstock();
} catch (TTransportException e) {
e.printStackTrace();
} catch (TException e) {
e.printStackTrace();
} finally{
transport.close();
}
return result;
}
private List<TDB_TLine> TLineResult;
private StringBuffer sbResult = new StringBuffer();
public String testTLine(TDB_ReqTLine req){//TDB_ReqTLine對象由服務端決定,這里的就是我要測試的接口get_tline的一個參數
try {
TLineResult = client.get_tline(req);//此處執行測試接口的調用
Iterator<TDB_TLine> it = TLineResult.iterator();
while(it.hasNext()){
TDB_TLine t = it.next();
sbResult.append(t.toString());
}
} catch (ErrorReturn e) {
e.printStackTrace();
} catch (TException e) {
e.printStackTrace();
}
return sbResult.toString();
}
}