1. Create Server
PackageCn.horace.thrift.server;ImportCn.horace.thrift.idl.IUserService;ImportCn.horace.thrift.rpc.IUserServiceImpl;ImportOrg.apache.thrift.protocol.TBinaryProtocol;Importorg.apache.thrift.protocol.TProtocolFactory;ImportOrg.apache.thrift.server.TServer;ImportOrg.apache.thrift.server.TThreadPoolServer;Importorg.apache.thrift.transport.*;Importjava.io.IOException;/*** Created by Horace on 15-4-10 pm 9:00am.*/ Public classsimpleserver{ Public Static voidMain (string[] args)throwsttransportexception, IOException {//Creating the ProcessorIuserservice.processor<iuserserviceimpl> Processor =NewIuserservice.processor<iuserserviceimpl> (NewIuserserviceimpl ()); //To create a transport objectTservertransport Servertransport =NewTserversocket (9090); //Create a transport factory, non-blockingTtransportfactory transportfactory =Newtframedtransport.factory (); //Create a protocol factoryTprotocolfactory protocolfactory =Newtbinaryprotocol.factory (); //Setting Server ParametersTthreadpoolserver.args Serverargs =NewTthreadpoolserver.args (Servertransport); //setting up the processorServerargs.processor (processor); //set the protocol usedserverargs.protocolfactory (protocolfactory); //set the transport object usedserverargs.transportfactory (transportfactory); //Creating a serverTserver Server =NewTthreadpoolserver (Serverargs); System.out.println ("Starting the simple server ..."); Server.serve (); }}
2. Create Client
Packagecn.horace.thrift.client;ImportCn.horace.thrift.idl.IUserService;Importorg.apache.thrift.TException;ImportOrg.apache.thrift.protocol.TBinaryProtocol;ImportOrg.apache.thrift.protocol.TProtocol;ImportOrg.apache.thrift.transport.TFramedTransport;ImportOrg.apache.thrift.transport.TSocket;ImportOrg.apache.thrift.transport.TTransport;/*** Created by Horace on 15-4-10 pm 9:00am.*/ Public classsimpleclient{ Public Static voidMain (string[] args)throwstexception, interruptedexception {//To create a transport objectTsocket Basetransport =NewTsocket ("127.0.0.1", 9090); //Create a Transport object, non-blockingTtransport transport =Newtframedtransport.factory (). Gettransport (Basetransport); //Open Connection ChannelTransport.open (); //Create a Protocol objectTprotocol protocol =NewTbinaryprotocol (transport); //Creating Client ObjectsIuserservice.client Client =Newiuserservice.client (protocol); Client.findall (); Transport.close (); System.out.println ("FindAll ..."); }}
Create Thrift server and thrift Client