thrift java多線程非阻塞同步/非同步呼叫執行個體

來源:互聯網
上載者:User

本文由larrylgq編寫,轉載請註明出處:http://blog.csdn.net/larrylgq/article/details/7497342

作者:呂桂強

郵箱:larry.lv.word@gmail.com

首先建立thrift檔案

namespace java thriftservice Hello{  string helloString(1:string para)}
執行thrift -gen java test.thrift會產生一個Hello.java檔案
將Hello.java檔案拷貝至IDE
server端代碼:


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 呂桂強 * @email larry.lv.word@gmail.com * @version 建立時間:2012-4-24 下午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);// 高效率的、密集的二進位編碼格式進行資料轉送// 使用非阻塞方式,按塊的大小進行傳輸,類似於 Java 中的 NIOarg.protocolFactory(new TCompactProtocol.Factory());arg.transportFactory(new TFramedTransport.Factory());arg.processorFactory(new TProcessorFactory(processor));TServer server = new THsHaServer(arg);server.serve();System.out.println("#服務啟動-使用:非阻塞&高效二進位編碼");} catch (TTransportException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}}public static void main(String args[]) {Server srv = new Server();srv.start();}}


client端代碼:

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 呂桂強 * @email larry.lv.word@gmail.com * @version 建立時間:2012-4-24 下午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 asyncClient = new Hello.AsyncClient(protocol, clientManager, transport);System.out.println("Client calls .....");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();}}

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 呂桂強 * @email larry.lv.word@gmail.com * @version 建立時間:2012-4-25 上午11:17:32 */public class MyCallback implements AsyncMethodCallback<helloString_call> {// 返回結果@Overridepublic void onComplete(helloString_call response) {System.out.println("onComplete");try {System.out.println(response.getResult().toString());} catch (TException e) {e.printStackTrace();}}// 返回異常@Overridepublic void onError(Exception exception) {System.out.println("onError");}}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.