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 ();
}