Apache thrift RPC bidirectional communication, apachethrift
In the previous article, we introduced the installation and use of Apache thrift, and wrote a simple demo to explain the publishing and client call of the thrift service. However, it is only a one-way client that sends messages and the server receives messages. The client does not receive the server response.
Without restrictions on the Language Platform, WebService is competent for processing these servers.
Based on most business needs, the server must be able to respond to and process data. Next, I will introduce the use of Apache thrift two-way communication through a demo case.
I. First, we need to install Apache thrift. Here I will not go into details, stamp here to view the introduction of my previous article: http://www.cnblogs.com/sumingk/articles/6073105.html
2. Prepare the jar package required by thrift:
3. Create a Java web project and write the thrift script named student. thrift as follows:
namespace java com.zhj.studenttypedef i32 int typedef i16 shorttypedef i64 long//Student Entitystruct Student { 1: string name} service Zthrift { oneway void send(1:Student msg)}
4. Execute the student. thrift file, thrift -- gen java student. thrift (this file is still executed in the c root directory), and then generate the gen-java file, as shown below:
5. Merge the newly generated two files into the project. Student. java is the entity class, And Zthrift. java is the generated class.
6. Compile the thrift server class.
Package com. zhj. server; import org. apache. thrift. TException; import org. apache. thrift. TProcessor; import org. apache. thrift. TProcessorFactory; import org. apache. thrift. protocol. TBinaryProtocol; import org. apache. thrift. server. TThreadPoolServer; import org. apache. thrift. transport. TServerSocket; import org. apache. thrift. transport. TTransport; import org. apache. thrift. transport. TTransportException; import com. zhj. student. student; import com. zhj. student. zthrift; import com. zhj. student. zthrift. iface; public class ZServer {public static void main (String [] args) {try {TServerSocket tServerSocket = new TServerSocket (9999); TThreadPoolServer. args targs = new TThreadPoolServer. args (tServerSocket); TBinaryProtocol. factory factory = new TBinaryProtocol. factory (); // obtain processFactory TProcessorFactory tProcessorFactory = getProcessorFactory (); targs. protocolFactory (factory); targs. processorFactory (tProcessorFactory); TThreadPoolServer tThreadPoolServer = new TThreadPoolServer (targs); System. out. println ("start server... "); tThreadPoolServer. serve ();} catch (TTransportException e) {// TODO Auto-generated catch block e. printStackTrace () ;}}/*** internal class get getProcessorFactory * @ return */public static int tt = 0; public static TProcessorFactory getProcessorFactory () {TProcessorFactory tProcessorFactory = new TProcessorFactory (null) {public TProcessor getProcessor (final TTransport tTransport) {Thread thread = new Thread (new Runnable () {@ Override public void run () {try {System. out. println ("after the server is sleeping for 5 seconds, the server executes the response ...... "); // five seconds delayed response (delayed execution sends messages to the client) Thread. sleep (5000); tt + = 100; System. out. println ("tt =" + tt) when the reply is delayed for five seconds); // you can extract the client as a member variable to use Zthrift multiple times. client client = new Zthrift. client (new TBinaryProtocol (tTransport); // return the message to the client. send (new Student (".... test ");} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (TException e) {// TODO Auto-generated catch block e. printStackTrace () ;}}); thread. start (); return new Zthrift. processor <Iface> (new Iface () {@ Override public void send (Student msg) throws TException {// TODO Auto-generated method stub tt + = 10; System. out. println ("tt =" + tt when receiving client messages); // receives the client message System. out. println ("..... "+ msg. toString () ;}}}; return tProcessorFactory ;}}
Here, internal classes are frequently used and reading is difficult. Zthrift, Processor constructor needs to input an Iface interface, which has a method of sending () to receive the client, and msg is a Student object.
7. Implemented client call. As follows:
Package com. zhj. client; import org. apache. thrift. TException; import org. apache. thrift. protocol. TBinaryProtocol; import org. apache. thrift. transport. TSocket; import org. apache. thrift. transport. TTransportException; import com. zhj. student. student; import com. zhj. student. zthrift. iface; import com. zhj. student. zthrift; public class ZClient {public static void main (String [] args) {final TSocket tSocket = new TSocket ("127.0.0.1", 9999); Zthrift. client client = new Zthrift. client (new TBinaryProtocol (tSocket); try {tSocket. open (); runMethod (tSocket); // sends a message to the server client. send (new Student ("James 1");} catch (TTransportException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (TException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} public static void runMethod (final TSocket tSocket) {Thread thread = new Thread (new Runnable () {@ Override public void run () {Zthrift. processor <Iface> mp = new Zthrift. processor <Zthrift. iface> (new Iface () {@ Override public void send (Student msg) throws TException {// TODO Auto-generated method stub Long start = System. currentTimeMillis (); try {while (true) {// The specific receipt time To be determined if (System. currentTimeMillis ()-start)> 0.1*60*1000) {System. out. println ("Response Message timeout... "); break;} else {System. out. println ("received Server Response Message:" + msg);} // sleep for two seconds Thread. sleep (2000L) ;}} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace () ;}}); try {while (mp. process (new TBinaryProtocol (tSocket), new TBinaryProtocol (tSocket) {// blocking method, NO content System. out. println ("blocking method"); // disable tScoket // tSocket. close () ;}} catch (TException e) {System. out. println ("connection disconnected... "); e. printStackTrace () ;}}); thread. start ();}}
Here, I add an endless loop of timeout responses to receive messages returned by the server. The console can view the response messages sent by the Service.
8. Run the main method of the server and client. The console prints the following:
Code Reading is difficult, difficult, or unreasonable. Please point it out. Thank you!