1.同步阻塞
CaculatorService.Processor<CaculatorServiceHandler> processor = new CaculatorService.Processor<CaculatorServiceHandler>(
new CaculatorServiceHandler());
TServerTransport serverTransport = new TServerSocket(8888);
Args targs = new Args(serverTransport);
targs.processor(processor);
targs.transportFactory(new TFramedTransport.Factory());
targs.protocolFactory(new TCompactProtocol.Factory());
TServer server = new TSimpleServer(targs);
System.out.println("Starting the TSimpleServer...");
server.serve();
CaculatorService.Processor<CaculatorServiceHandler> processor = new CaculatorService.Processor<CaculatorServiceHandler>(
new CaculatorServiceHandler());
TServerTransport serverTransport = new TServerSocket(8888);
org.apache.thrift.server.TThreadPoolServer.Args targs = new org.apache.thrift.server.TThreadPoolServer.Args(serverTransport);
targs.processor(processor);
targs.transportFactory(new TFramedTransport.Factory());
targs.protocolFactory(new TCompactProtocol.Factory());
TThreadPoolServer server = new TThreadPoolServer(targs);
System.out.println("Starting the TThreadPoolServer...");
server.serve();
TTransport transport = new TFramedTransport(new TSocket("localhost", 8888));
transport.open();
TProtocol protocol = new TCompactProtocol(transport);
CaculatorService.Client client = new CaculatorService.Client(protocol);
transport.close();
2.同步非阻塞
CaculatorService.Processor<CaculatorServiceHandler> processor = new CaculatorService.Processor<CaculatorServiceHandler>(
new CaculatorServiceHandler());
TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(8888);
org.apache.thrift.server.TNonblockingServer.Args targs = new org.apache.thrift.server.TNonblockingServer.Args(serverTransport);
targs.processor(processor);
targs.transportFactory(new TFramedTransport.Factory());
targs.protocolFactory(new TCompactProtocol.Factory());
TNonblockingServer server = new TNonblockingServer(targs);
System.out.println("Starting the TNonblockingServer...");
server.serve();
CaculatorService.Processor<CaculatorServiceHandler> processor = new CaculatorService.Processor<CaculatorServiceHandler>(
new CaculatorServiceHandler());
TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(8888);
org.apache.thrift.server.THsHaServer.Args targs = new org.apache.thrift.server.THsHaServer.Args(serverTransport);
targs.processor(processor);
targs.transportFactory(new TFramedTransport.Factory());
targs.protocolFactory(new TCompactProtocol.Factory());
TServer server = new THsHaServer(targs);
System.out.println("Starting the THsHaServer...");
server.serve();
CaculatorService.Processor<CaculatorServiceHandler> processor = new CaculatorService.Processor<CaculatorServiceHandler>(
new CaculatorServiceHandler());
TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(8888);
org.apache.thrift.server.TThreadedSelectorServer.Args targs = new org.apache.thrift.server.TThreadedSelectorServer.Args(
serverTransport);
targs.processor(processor);
targs.transportFactory(new TFramedTransport.Factory());
targs.protocolFactory(new TCompactProtocol.Factory());
TServer server = new TThreadedSelectorServer(targs);
System.out.println("Starting the TThreadedSelectorServer...");
server.serve();
TTransport transport = new TFramedTransport(new TSocket("localhost", 8888));
transport.open();
TProtocol protocol = new TCompactProtocol(transport);
CaculatorService.Client client = new CaculatorService.Client(protocol);
transport.close();
3.异步,注册回调
try {
TProtocolFactory protocolFactory = new TCompactProtocol.Factory();
TAsyncClientManager clientManager = new TAsyncClientManager();
TNonblockingSocket transport = new TNonblockingSocket("localhost", 8888);
CaculatorService.AsyncClient client = new CaculatorService.AsyncClient(protocolFactory, clientManager, transport);
Random random = new Random();
for (int i = 0; i < 10; ++i) {
int x = random.nextInt(1000), y = random.nextInt(1000);
System.out.println("" + x + " + " + y + " = ?");
try {
client.add(x, y, new MyCallback());
Thread.sleep(100);
} catch (TException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
transport.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
class MyCallback implements AsyncMethodCallback<CaculatorService.AsyncClient.add_call> {
@Override
public void onComplete(add_call arg0) {
// TODO Auto-generated method stub
try {
System.out.println(arg0.getResult());
} catch (TException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onError(Exception arg0) {
// TODO Auto-generated method stub
System.out.println(arg0.toString());
}
}