Thrift Java multi-thread non-blocking synchronous/asynchronous call instance

Source: Internet
Author: User

This article by larrylgq prepared, reproduced please note the Source: http://blog.csdn.net/larrylgq/article/details/7497342

Author: Lu guiqiang

Email: larry.lv.word@gmail.com

First create the thrift File

Namespace Java thriftservice Hello {string hellostring (1: String para )}
Execute thrift-gen Java test. thrift to generate a hello. Java file.
Copy the hello. Java file to the IDE
Server code:

 

Package COM. thrift. test. thrift; import Org. apache. thrift. tprocessorfactory; import Org. apache. thrift. protocol. tcompactprotocol; import Org. apache. thrift. server. thshaserver; import Org. apache. thrift. server. tserver; import Org. apache. thrift. transport. tframedtransport; import Org. apache. thrift. transport. tnonblockingserversocket; import Org. apache. thrift. transport. ttransportexception;/*** @ author Lu Gui strong * @ email larry.lv.word@gmail.com * @ version Creation Time: 8:14:50 */public class server {public final static int Port = 8989; @ suppresswarnings ({"rawtypes", "unchecked"}) Private void start () {try {tnonblockingserversocket socket = new tnonblockingserversocket (port); Final hello. processor processor = new HELLO. processor (New helloimpl (); thshaserver. ARGs Arg = new thshaserver. ARGs (socket); // efficient and intensive binary encoding format for data transmission // non-blocking mode for block size transmission, similar to nioarg in Java. protocolfactory (New tcompactprotocol. factory (); arg. transportfactory (New tframedtransport. factory (); arg. processorfactory (New tprocessorfactory (processor); tserver Server = new thshaserver (ARG); server. serve (); system. out. println ("# service start-Use: non-blocking & Efficient binary encoding");} catch (ttransportexception e) {e. printstacktrace ();} catch (exception e) {e. printstacktrace () ;}} public static void main (string ARGs []) {server SRV = new server (); srv. start ();}}


Client code:

Package COM. thrift. test. async; import Java. io. ioexception; import Org. apache. thrift. tapplicationexception; import Org. apache. thrift. texception; import Org. apache. thrift. async. tasyncclientmanager; import Org. apache. thrift. protocol. tcompactprotocol; import Org. apache. thrift. protocol. tprotocol; import Org. apache. thrift. protocol. tprotocolfactory; import Org. apache. thrift. transport. tframedtransport; import Org. apache. thrift. transport. tnonblockingsocket; import Org. apache. thrift. transport. tnonblockingtransport; import Org. apache. thrift. transport. tsocket; import Org. apache. thrift. transport. tTransport; import Org. apache. thrift. transport. ttransportexception;/*** @ author Lu Gui strong * @ email larry.lv.word@gmail.com * @ version Creation Time: 8:17:38 */public class client {public static final string address = "127.0.0.1 "; public static final int Port = 8989; public static final int clienttimeout = 30000; public static void main_syn () {tTransport transport = new tframedtransport (New tsocket (address, port, clienttimeout )); tprotocol protocol = new tcompactprotocol (transport); Hello. client client = new HELLO. client (Protocol); try {transport. open (); system. out. println (client. hellostring ("Larry");} catch (tapplicationexception e) {system. out. println (E. getmessage () + "" + E. getType ();} catch (ttransportexception e) {e. printstacktrace ();} catch (texception e) {e. printstacktrace ();} transport. close ();} public static void main_asy () throws exception {try {tasyncclientmanager clientmanager = new tasyncclientmanager (); tnonblockingtransport transport = new tnonblockingsocket (address, port, clienttimeout ); tprotocolfactory protocol = new tcompactprotocol. factory (); Hello. asyncclient = new HELLO. asyncclient (protocol, clientmanager, transport); system. out. println ("client CILS ..... "); mycallback callback = new mycallback (); asyncclient. hellostring ("Larry", callback); While (true) {thread. sleep (1) ;}} catch (ioexception e) {e. printstacktrace () ;}} public static void main (string [] ARGs) throws exception {main_asy ();}}

The callback function used by the client:

Package COM. thrift. test. async; import Org. apache. thrift. texception; import Org. apache. thrift. async. asyncmethodcallback; import COM. thrift. test. async. hello. asyncclient. hellostring_call;/*** @ author Lu Gui strong * @ email larry.lv.word@gmail.com * @ version Creation Time: 11:17:32 */public class mycallback implements asyncmethodcallback 


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.