Apache Thrift-java Development Detailed

Source: Internet
Author: User

1. Add Dependent jar

<dependency>
  <groupId>org.apache.thrift</groupId>
  <artifactid>libthrift</ artifactid>
  <version>0.8.0</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  < Version>1.6.1</version>
</dependency>

2. Write IDL file Hello.thrift

namespace Java Service.demo
Service Hello {
String hellostring (1:string para)
I32 Helloint (1:i32 para)
BOOL Helloboolean (1:bool para)
void Hellovoid ()
String Hellonull ()
}


3. Generate Code

Thrift-o <output directory>-gen java hello.thrift
To generate a code thumbnail:



4. Write implementation class and realize Hello.iface:

Thumbnail Image:



5, the writing service side, the release (Blocking IO + multithreading processing) service.

/**
	 * Blocking, multithreading
	 * 
	 * @param args
	/@SuppressWarnings ({"Unchecked", "Rawtypes"})
	Public static void Main (string[] args) {
		try {
			//Set transport channel, normal channel
			tservertransport servertransport = new Tserversocket ( 7911);
			
			Use high density binary protocol
			tprotocolfactory profactory = new Tcompactprotocol.factory ();
			
			Set the processor Helloimpl
			Tprocessor processor = new Hello.processor (new Helloimpl ());
			
			Create the server
			tserver server = new Tthreadpoolserver (
					new Args (Servertransport)
					. Protocolfactory ( Profactory)
					. Processor (processor)
				);
			
			SYSTEM.OUT.PRINTLN ("Start Server on port 7911 ...");
			Server.serve ();
		} catch (Exception e) {
			e.printstacktrace ();
		}
	}



6, write client, call (blocking IO + multithreading) Services:

public static void Main (string[] args) throws Exception {
		//Set transport channel-Normal IO circulation path
		ttransport transport = new Tsocket (" localhost ", 7911);
		Transport.open ();
		
		Use high density binary protocol
		tprotocol protocol = new Tcompactprotocol (transport);
		
		Create client
		hello.client client = new Hello.client (protocol);
		
		Long start = System.currenttimemillis ();
		for (int i=0; i<10000; i++) {
			Client.helloboolean (false);
			Client.helloint (a);
			Client.hellonull ();
			Client.hellostring ("Dongjian");
			Client.hellovoid ();
		}
		SYSTEM.OUT.PRINTLN ("Time Consuming:" + (System.currenttimemillis ()-start));
		
		Close Resource
		transport.close ();
	}



Now complete the entire development process, super invincible simple.

The protocols used by the server side need to be aligned with the client .

--------------------------------------------------------------------------------------------------------------- ----


It shows the common and commonly used server and client, and here is the non-blocking io, the NIO in Java:


service-side based on non-blocking io (NIO) :

public static void Main (string[] args) {
		try {
			//transport channel-non-blocking mode
			Tnonblockingservertransport servertransport = n EW Tnonblockingserversocket (7911);
			
			Asynchronous IO, which requires the use of tframedtransport, which reads the block cache.
			ttransportfactory transportfactory = new Tframedtransport.factory ();
			
			Use high density binary protocol
			tprotocolfactory profactory = new Tcompactprotocol.factory ();
			
			Set the processor Helloimpl
			Tprocessor processor = new Hello.processor (new Helloimpl ());
			
			Create the server
			tserver server = new Tthreadedselectorserver (
					new Args (Servertransport)
					. Protocolfactory ( profactory)
					. Transportfactory (transportfactory)
					. Processor (processor)
				);
			
			SYSTEM.OUT.PRINTLN ("Start Server on port 7911 ...");
			Server.serve ();
		} catch (Exception e) {
			e.printstacktrace ();
		}
	}



to invoke a client that does not block Io (NIO) Services :

public static void Main (string[] args) throws Exception {
		//Set transport channel, for non-blocking services, you need to use Tframedtransport, which sends
		data blocks Ttransport transport = new Tframedtransport (new Tsocket ("localhost", 7911));
		Transport.open ();
		
		Use high density binary protocol
		tprotocol protocol = new Tcompactprotocol (transport);
		
		Create client
		hello.client client = new Hello.client (protocol);
		
		Long start = System.currenttimemillis ();
		for (int i=0; i<10000; i++) {
			Client.helloboolean (false);
			Client.helloint (a);
			Client.hellonull ();
			Client.hellostring ("360buy");
			Client.hellovoid ();
		}
		SYSTEM.OUT.PRINTLN ("Time Consuming:" + (System.currenttimemillis ()-start));
		
		Close Resource
		transport.close ();
	}



--------------------------------------------------------------------------------------------------------------- --------------------

Client Asynchronous Invocation :

/** call [non-blocking IO] service, asynchronous/public static void main (string[] args) {try {//Asynchronous Call Manager Tasyncclientmanager Clientmanager
			= new Tasyncclientmanager ();
			Sets the transport channel to call non-blocking IO.  
			Final Tnonblockingtransport transport = new Tnonblockingsocket ("localhost", 7911);  
            Set protocol Tprotocolfactory protocol = new Tcompactprotocol.factory ();
			Create client final Hello.asyncclient client = new Hello.asyncclient (protocol, Clientmanager, transport);
            Invoke service System.out.println ("Start:" + System.currenttimemillis ()); Client.helloboolean (False, new asyncmethodcallback 


--------------------------------------------------------------------------------------------------------------- --------------------

service-side using SSL:



to invoke a client based on the SSL server:



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.