Configure thrift development environment in Windows

Source: Internet
Author: User

1) install thrift: Download The exefile to thrift.exe, and copy the file to the C: \ Windows directory. Then you can use it in the DOS environment.

For example, thrift-gen Java D: \ mywork \ javaproject \ thrifttest \ test. Thrift. The output Java file is output to the current directory by default. You can also use the-O parameter to specify the output path.

2) download the dependent package

2.1) libthrift. jar,: http://repo1.maven.org/maven2/org/apache/thrift/libthrift/0.9.0/

2.2) slf4j-api.jar

2.3) slf4j-simple.jar

3) Compile the thrift interface file

namespace cpp zam.thrift.testnamespace py thriftTestnamespace java com.zam.thrift.testnamespace php thriftTestservice Hello {      string helloString(1:string word)  }

4) Compile the interface implementation code

package com.zam.server;import org.apache.thrift.TException;import com.zam.thrift.test.Hello.Iface;public class HelloImpl implements Iface{private static int count = 0;@Overridepublic String helloString(String word) throws TException {// TODO Auto-generated method stubcount += 1;System.out.println("get " + word + " " +count); return "hello " + word + " " + count;}}

5) Write server code

package com.zam.server;import org.apache.thrift.protocol.TBinaryProtocol;  import org.apache.thrift.protocol.TBinaryProtocol.Factory;  import org.apache.thrift.server.TServer;  import org.apache.thrift.server.TThreadPoolServer;  import org.apache.thrift.server.TThreadPoolServer.Args;  import org.apache.thrift.transport.TServerSocket;  import org.apache.thrift.transport.TTransportException; import com.zam.thrift.test.Hello;import com.zam.thrift.test.Hello.Processor;public class Server {    public void startServer() {          try {              System.out.println("thrift server open port 1234");            TServerSocket serverTransport = new TServerSocket(1234);              Hello.Processor process = new Processor(new HelloImpl());              Factory portFactory = new TBinaryProtocol.Factory(true, true);              Args args = new Args(serverTransport);              args.processor(process);              args.protocolFactory(portFactory);              TServer server = new TThreadPoolServer(args);              server.serve();          } catch (TTransportException e) {              e.printStackTrace();          }      }            public static void main(String[] args) {      System.out.println("thrift server init");        Server server = new Server();          System.out.println("thrift server start");        server.startServer();          System.out.println("thrift server end");    }  }

6) write client code

package com.zam.server;import org.apache.thrift.TException;  import org.apache.thrift.protocol.TBinaryProtocol;  import org.apache.thrift.protocol.TProtocol;  import org.apache.thrift.transport.TSocket;  import org.apache.thrift.transport.TTransport;  import org.apache.thrift.transport.TTransportException; import com.zam.thrift.test.Hello;public class Client {    public void startClient() {          TTransport transport;          try {              System.out.println("thrift client connext server at 1234 port ");            transport = new TSocket("localhost", 1234);              TProtocol protocol = new TBinaryProtocol(transport);              Hello.Client client = new Hello.Client(protocol);              transport.open();              System.out.println(client.helloString("panguso"));              transport.close();              System.out.println("thrift client close connextion");        } catch (TTransportException e) {              e.printStackTrace();          } catch (TException e) {              e.printStackTrace();          }      }        public static void main(String[] args) {      System.out.println("thrift client init ");        Client client = new Client();          System.out.println("thrift client start ");        client.startClient();          System.out.println("thrift client end ");    }  }

8) run the server and client code

8.1) Start the server

thrift server initthrift server startthrift server open port 1234

8.2) Start the client

thrift client init thrift client start thrift client connext server at 1234 port hello panguso 1thrift client close connextionthrift client end 

9) sample code: http://download.csdn.net/detail/azhao_dn/5341602

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.