Create Thrift server and thrift Client

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.