Thrift Exchange (ii) Thrift Server and client implementation Nifty

Source: Internet
Author: User
Nifty is a Facebook company Open source, Netty based thrift server and client implementation. You can then use this package to quickly publish efficient server and client code based on Netty.

Https://github.com/facebook/nifty

Nifty Simple Example

1) Environment Construction

Pom file

<dependency>
			<groupId>com.facebook.nifty</groupId>
			<artifactid>nifty-core</ artifactid>
			<version>0.9.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.thrift</groupId>
			<artifactId>libthrift</artifactId>
			< Version>0.9.1</version>
		</dependency>

Thrift file

Namespace Java example  //defines the namespace  
   
typedef i32 INT  //typedefs to get convenient names for your Ty PES 
   
service Thrifttestservice {  
	string test (1:string name),//delay 3s
}

Server file

Import Java.util.concurrent.ExecutorService;

Import java.util.concurrent.Executors;

Import Org.apache.thrift.TProcessor;
Import Com.facebook.nifty.core.NettyServerTransport;
Import Com.facebook.nifty.core.ThriftServerDef;

Import Com.facebook.nifty.core.ThriftServerDefBuilder; Import example.
Thrifttestservice; Import example.

Thrifttestserviceimpl; public class Server {/** * @param args */public static void main (string[] args) {//Create the handler Thr

				Ifttestservice.iface serviceinterface = new Thrifttestserviceimpl (); Create the processor Tprocessor processor = new Thrifttestservice.processor<thrifttestservice.iface> (s

				Erviceinterface); Build the server definition thriftserverdef serverdef = new Thriftserverdefbuilder (). Listen (7790). withproces

				SOR (processor). build ();

				Create the server transport final Nettyservertransport server = new Nettyservertransport (SERVERDEF);

	Start the server Server.start ();			Arrange to stop the server at Shutdown runtime.getruntime (). Addshutdownhook (New Thread () {@Override P
						ublic void Run () {try {server.stop ();
						The catch (Interruptedexception e) {thread.currentthread (). interrupt ();

				}
					}
				});

	SYSTEM.OUT.PRINTLN ("Server started successfully ...");
 }

}
Client

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 example. Thrifttestservice;

public class Client {

	/**
	 * @param args
	 *
	/public static void main (string[] args) throws Exception {
		T Transport transport = new Tsocket ("localhost", 7790);
		Transport.open ();
		Tprotocol protocol = new Tbinaryprotocol (transport);
		Thrifttestservice.client Client = new thrifttestservice.client (protocol);
		System.out.println (Client.test ("AA"));
		Transport.close ();
	}







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.