Thrift implementation C # detailed description of calling Java development steps

Source: Internet
Author: User
Tags gz file

Thrift implementation C # detailed description of calling Java development steps
Apache Thrift is an efficient remote service calling framework implemented by Facebook that supports multiple programming languages. Similar cross-language RPC frameworks include ICE, Hessian, Protocol Buffer, and Avro. 1 download Thrift

: Http://thrift.apache.org/download

Thrift-0.9.3.exe tool for compiling Thrift intermediate files to generate corresponding language code

Thrift-0.9.3.tar.gz contains the source code library of various Thrift languages, as well as some test program code, etc.

2 compile and generate. NET library (DLL) and Java Library (Jar)

Decompress the thrift-0.9.3.tar.gz file.

(1) generate a. NET Library

Open the project: E: \ Thrift \ thrift-0.9.3 \ lib \ csharp \ src \ Thrift. sln compilation to generate Thrift. dll

My environments are VS2010 and. NET 4.0.

(2) generate a Java Library

The Java library is built with Ant and requires Ant installation. The installation steps are not described here.

Open the command line CD to the java library code path: E: \ Thrift \ thrift-0.9.3 \ lib \ java (including build. xml)

Then you can directly execute the ant command to find that the corresponding jar file is generated under the build directory.

3. Compile the thrift intermediate file
namespace java testnamespace csharp testservice Hello {      string helloString(1:string word)}
4. Generate Java and C # interface files

Thrift-0.9.3.exe-gen java test. thrift

Thrift-0.9.3.exe-gen csharp test. thrift

The generated code is displayed in the current directory.

5. Write Java server code

Create a common Java project and compile the relevant jar files (libthrift-0.9.3.jar and all jar files under the E: \ Thrift \ thrift-0.9.3 \ lib \ java \ build \ lib directory) compiled by ant) add tobuild path; then the generated Hello. java is also added to the project. Pay attention to the package name.

(1) first write the interface implementation class:

 

package test;import org.apache.thrift.TException;import test.Hello.Iface; public classHelloImpl implementsIface{    privatestaticintcount= 0;         @Override    publicString helloString(String word)throwsTException   {        count += 1;         System.out.println("get " + word + " " +count);          return "hello " + word + " " + count;    }  }

 

(2) write the boarding code, start and listen on the specified port:

package test;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 test.Hello.Processor; public classServer {    @SuppressWarnings({"rawtypes", "unchecked" })    public void startServer() {           try {               System.out.println("thrift server host on port 8899");             TServerSocket serverTransport = new TServerSocket(8899);           Hello.Processorprocess = newProcessor(newHelloImpl());               Factory portFactory = newTBinaryProtocol.Factory(true, true);               Args args = newArgs(serverTransport);               args.processor(process);               args.protocolFactory(portFactory);               TServer server = newTThreadPoolServer(args);               server.serve();           } catch (TTransportException e) {               e.printStackTrace();           }      }             publicstaticvoidmain(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 C # client code

Create a common console project, introduce Thrift. dll, and add the generated Hello. cs to the project.

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;using Thrift.Transport;using Thrift.Protocol;namespace ThriftTest{    class ClientTest    {        static void Main(string[]args)        {            TTransporttransport = new TSocket("localhost", 8899);            TProtocolprotocol = new TBinaryProtocol(transport);            test.Hello.Client client = newtest.Hello.Client(protocol);            transport.Open();            Console.WriteLine("Client calls client.helloString().....");            Console.WriteLine(client.helloString("jiyiqin"));            client.Dispose();        }    }}
7 run

Run the java Server. java:

Thrift server init

Thrift server start

Thrift server host on port 8899.

Get jiyiqin 1

 

Run C # client code ClientTest. cs

Client callclient. helloString ().....

Hello jiyiqin 1

Press any key to continue...

We can see that the steps for developing cross-language RPC frameworks such as Thrift and ICE are very similar, almost identical, and the generated files are similar, but they differ greatly from cross-language RPC frameworks such as Servlet-based Hessian.

 

Related Article

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.