Java Quick Start Thrift

Source: Internet
Author: User

Apache Thrift is a facebook resume RPC framework and is now a top-level Apache project. Thrift allows you to define data types and service interfaces through a cross-language definition file, which serves as the standard for communication between RPC clients and servers, you can also go to Thrift's White Paper to learn more.

According to the official site description of Apache Thrift, Thrift is one:

Software framework, for scalable cross-language services development, combines a software stack with a code generation engine to build services that work efficiently and seamlessly between C ++, Java, Python, PHP, ruby, Erlang, Perl, Haskell, C #, Cocoa, JavaScript, Node. js, Smalltalk, OCaml and Delphi and other ages.
 
If thriftcomparisons are installed, compile thrift.exe in the Windows official website and download and install thrift.exe.

Write Thrift definition file (. thrift file)
If you have been familiar with this before, writing definition files is very simple. But here you can refer to the official tutorial to start quickly.

Example definition file (add. thrift)

1
Namespace java com. eviac. blog. samples. thrift. server // defines the namespace
2

3
Typedef i32 int // typedefs to get convenient names for your types
4

5
Service AdditionService {// defines the service to add two numbers
6
Int add (1: int n1, 2: int n2), // defines a method
7
}
Compile the Thrift definition file
The following command compiles the. thrift File

1
Thrift -- gen <language> <Thrift filename>
For my example, the command is:

1
Thrift -- gen java add. thrift
After the code is executed, you will find useful source code for building the RPC server and client in the gen-java directory. In my example, I will create a java file called AddtionService. java.

Write a service handler
The Service handler class must implement the AdditionService. Iface interface.
Example Service handler (AdditionServiceHandler. java)

01
Package com. eviac. blog. samples. thrift. server;
02

03
Import org. apache. thrift. TException;
04

05
Public class AdditionServiceHandler implements AdditionService. Iface {
06

07
@ Override
08
Public int add (int n1, int n2) throws TException {
09
Return n1 + n2;
10
}
11

12
}
Write a simple server
The following sample code is a simple Thrift server. The following code is annotated. You can remove the annotation to enable the multi-threaded server.
Example server (MyServer. java)

01
Package com. eviac. blog. samples. thrift. server;
02

03
Import org. apache. thrift. transport. TServerSocket;
04
Import org. apache. thrift. transport. TServerTransport;
05
Import org. apache. thrift. server. TServer;
06
Import org. apache. thrift. server. TServer. Args;
07
Import org. apache. thrift. server. TSimpleServer;
08

09
Public class MyServer {
10

11
Public static void StartsimpleServer (AdditionService. Processor <AdditionServiceHandler> processor ){
12
Try {
13
TServerTransport serverTransport = new TServerSocket (9090 );
14
TServer server = new TSimpleServer (
15
New Args (serverTransport). processor (processor ));
16

17
// Use this for a multithreaded server
18
// TServer server = new TThreadPoolServer (new
19
// TThreadPoolServer. Args (serverTransport). processor (processor ));
20

21
System. out. println ("Starting the simple server ...");
22
Server. serve ();
23
} Catch (Exception e ){
24
E. printStackTrace ();
25
}
26
}
27

28
Public static void main (String [] args ){
29
StartsimpleServer (new AdditionService. Processor <AdditionServiceHandler> (new AdditionServiceHandler ()));
30
}
31

32
}
Write a client
The following example shows a service for short use of AdditionService on a client written in Java.

 

01
Package com. eviac. blog. samples. thrift. client;
02

03
Import org. apache. thrift. TException;
04
Import org. apache. thrift. protocol. TBinaryProtocol;
05
Import org. apache. thrift. protocol. TProtocol;
06
Import org. apache. thrift. transport. TSocket;
07
Import org. apache. thrift. transport. TTransport;
08
Import org. apache. thrift. transport. TTransportException;
09

10
Public class AdditionClient {
11

12
Public static void main (String [] args ){
13

14
Try {
15
TTransport transport;
16

17
Transport = new TSocket ("localhost", 9090 );
18
Transport. open ();
19

20
TProtocol protocol = new TBinaryProtocol (transport );
21
AdditionService. Client client = new AdditionService. Client (protocol );
22

23
System. out. println (client. add (100,200 ));
24

25
Transport. close ();
26
} Catch (TTransportException e ){
27
E. printStackTrace ();
28
} Catch (TException x ){
29
X. printStackTrace ();
30
}
31
}
32

33
}
Run the server code (MyServer. java) and you will see the following output.

 
1
Starting the simple server...
Run the client code (AdditionClient. java) and the following output is displayed.

1
300


Author: Wang zhenwei

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.