Thrift implementing C # calling Java development steps

Source: Internet
Author: User
Tags gz file

Reprint Please specify source:Jiq's technical Blog
Apache Thrift is an efficient framework for Facebook to implement remote service calls that support multiple programming languages.
similar cross-language RPC frameworks include ice, Hessian, Protocol Buffer, Avro, and so on.
1 Downloads 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 repository for each language in thrift, as well as some test program codes, etc.

2 compile build. NET libraries (DLLs) and Java Libraries (jars)

Unzip the thrift-0.9.3.tar.gz file.

(1) Generate. NET Library

Open Project: E:\Thrift\thrift-0.9.3\lib\csharp\src\Thrift.sln compile to generate Thrift.dll

My environment is VS2010 and. NET 4.0

(2) Creating a Java library

The Java library is built with Ant and requires ant installation, and the installation steps are not discussed here.

To open the command line CD to the Java Library code path: E:\Thrift\thrift-0.9.3\lib\java (contains Build.xml)

You can then execute the ant command directly to find the corresponding jar file in the build directory.

3 Writing Thrift intermediate files

namespace Java testnamespace CSharp testservice Hello {      string hellostring (1:string Word)}

4 generating the respective interface files for Java and C #

Thrift-0.9.3.exe–gen Java Test.thrift

Thrift-0.9.3.exe–gen CSharp Test.thrift

You can see that the generated corresponding code appears in the current directory.

5 Writing Java service-side code

create a new generic Java project that will generate related jar files (Libthrift-0.9.3.jar and E:\Thrift\thrift-0.9.3\lib\java\build\) from the previous ant compilation All jar files in the Lib directory) Add tobuild path,and then also add the generated Hello.java to the project, note 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 Writing C # client code

Create a new normal console project, introduce Thrift.dll, and then 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 Java server Server.java:

Thrift Server Init

Thrift Server Start

Thrift Server Host on port 8899

Get Jiyiqin 1

Running C # client code ClientTest.cs

Client calls Client.hellostring () .....

Hello Jiyiqin 1

Please press any key to continue ...

You can see that cross-language RPC framework development steps like thrift and ice are very similar, almost identical, and produce similar files, but the cross-language RPC framework is significantly different from the servlet-based Hessian.

Thrift implementing C # calling Java development steps

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.