{"id":1154,"date":"2016-06-07T18:23:08","date_gmt":"2016-06-07T10:23:08","guid":{"rendered":"http:\/\/www.yeetrack.com\/?p=1154"},"modified":"2016-06-07T18:23:08","modified_gmt":"2016-06-07T10:23:08","slug":"apache-thrift-%e5%8f%af%e4%bc%b8%e7%bc%a9%e7%9a%84%e8%b7%a8%e8%af%ad%e8%a8%80%e6%9c%8d%e5%8a%a1%e5%bc%80%e5%8f%91%e6%a1%86%e6%9e%b6","status":"publish","type":"post","link":"https:\/\/www.yeetrack.com\/?p=1154","title":{"rendered":"Apache Thrift &#8211; \u53ef\u4f38\u7f29\u7684\u8de8\u8bed\u8a00\u670d\u52a1\u5f00\u53d1\u6846\u67b6"},"content":{"rendered":"<div id=\"ibm-leadspace-body\">\n<h1>Apache Thrift \u662f Facebook \u5b9e\u73b0\u7684\u4e00\u79cd\u9ad8\u6548\u7684\u3001\u652f\u6301\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\u7684\u8fdc\u7a0b\u670d\u52a1\u8c03\u7528\u7684\u6846\u67b6\u3002\u672c\u6587\u5c06\u4ece Java \u5f00\u53d1\u4eba\u5458\u89d2\u5ea6\u8be6\u7ec6\u4ecb\u7ecd Apache Thrift \u7684\u67b6\u6784\u3001\u5f00\u53d1\u548c\u90e8\u7f72\uff0c\u5e76\u4e14\u9488\u5bf9\u4e0d\u540c\u7684\u4f20\u8f93\u534f\u8bae\u548c\u670d\u52a1\u7c7b\u578b\u7ed9\u51fa\u76f8\u5e94\u7684 Java \u5b9e\u4f8b\uff0c\u540c\u65f6\u8be6\u7ec6\u4ecb\u7ecd Thrift \u5f02\u6b65\u5ba2\u6237\u7aef\u7684\u5b9e\u73b0\uff0c\u6700\u540e\u63d0\u51fa\u4f7f\u7528 Thrift \u9700\u8981\u6ce8\u610f\u7684\u4e8b\u9879\u3002<\/h1>\n<\/div>\n<p><!--more--><\/p>\n<h2>\u524d\u8a00\uff1a<\/h2>\n<p>\u76ee\u524d\u6d41\u884c\u7684\u670d\u52a1\u8c03\u7528\u65b9\u5f0f\u6709\u5f88\u591a\u79cd\uff0c\u4f8b\u5982\u57fa\u4e8e SOAP \u6d88\u606f\u683c\u5f0f\u7684 Web Service\uff0c\u57fa\u4e8e JSON \u6d88\u606f\u683c\u5f0f\u7684 RESTful \u670d\u52a1\u7b49\u3002\u5176\u4e2d\u6240\u7528\u5230\u7684\u6570\u636e\u4f20\u8f93\u65b9\u5f0f\u5305\u62ec XML\uff0cJSON \u7b49\uff0c\u7136\u800c XML \u76f8\u5bf9\u4f53\u79ef\u592a\u5927\uff0c\u4f20\u8f93\u6548\u7387\u4f4e\uff0cJSON \u4f53\u79ef\u8f83\u5c0f\uff0c\u65b0\u9896\uff0c\u4f46\u8fd8\u4e0d\u591f\u5b8c\u5584\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u7531 Facebook \u5f00\u53d1\u7684\u8fdc\u7a0b\u670d\u52a1\u8c03\u7528\u6846\u67b6 Apache Thrift\uff0c\u5b83\u91c7\u7528\u63a5\u53e3\u63cf\u8ff0\u8bed\u8a00\u5b9a\u4e49\u5e76\u521b\u5efa\u670d\u52a1\uff0c\u652f\u6301\u53ef\u6269\u5c55\u7684\u8de8\u8bed\u8a00\u670d\u52a1\u5f00\u53d1\uff0c\u6240\u5305\u542b\u7684\u4ee3\u7801\u751f\u6210\u5f15\u64ce\u53ef\u4ee5\u5728\u591a\u79cd\u8bed\u8a00\u4e2d\uff0c\u5982 C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, Smalltalk \u7b49\u521b\u5efa\u9ad8\u6548\u7684\u3001\u65e0\u7f1d\u7684\u670d\u52a1\uff0c\u5176\u4f20\u8f93\u6570\u636e\u91c7\u7528\u4e8c\u8fdb\u5236\u683c\u5f0f\uff0c\u76f8\u5bf9 XML \u548c JSON \u4f53\u79ef\u66f4\u5c0f\uff0c\u5bf9\u4e8e\u9ad8\u5e76\u53d1\u3001\u5927\u6570\u636e\u91cf\u548c\u591a\u8bed\u8a00\u7684\u73af\u5883\u66f4\u6709\u4f18\u52bf\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd Thrift \u7684\u4f7f\u7528\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e30\u5bcc\u7684\u5b9e\u4f8b\u4ee3\u7801\u52a0\u4ee5\u89e3\u91ca\u8bf4\u660e\uff0c\u5e2e\u52a9\u4f7f\u7528\u8005\u5feb\u901f\u6784\u5efa\u670d\u52a1\u3002<\/p>\n<div class=\"ibm-alternate-rule\"><\/div>\n<p class=\"ibm-ind-link ibm-back-to-top\"><a class=\"ibm-anchor-up-link\" href=\"http:\/\/www.ibm.com\/developerworks\/cn\/java\/j-lo-apachethrift\/#ibm-pcon\">\u56de\u9875\u9996<\/a><\/p>\n<h2 id=\"major2\">\u4e00\u4e2a\u7b80\u5355\u7684 Thrift \u5b9e\u4f8b<\/h2>\n<p>\u672c\u6587\u9996\u5148\u4ecb\u7ecd\u4e00\u4e2a\u7b80\u5355\u7684 Thrift \u5b9e\u73b0\u5b9e\u4f8b\uff0c\u4f7f\u8bfb\u8005\u80fd\u591f\u5feb\u901f\u76f4\u89c2\u5730\u4e86\u89e3\u4ec0\u4e48\u662f Thrift \u4ee5\u53ca\u5982\u4f55\u4f7f\u7528 Thrift \u6784\u5efa\u670d\u52a1\u3002<\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u670d\u52a1 Hello\u3002\u9996\u5148\u6839\u636e Thrift \u7684\u8bed\u6cd5\u89c4\u8303\u7f16\u5199\u811a\u672c\u6587\u4ef6 Hello.thrift\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<h5 id=\"listing1\">\u6e05\u5355 1. Hello.thrift<\/h5>\n<div class=\"codesection\">\n<pre class=\"displaycode\"> namespace java service.demo \n service Hello{ \n  string helloString(1:string para) \n  i32 helloInt(1:i32 para) \n  bool helloBoolean(1:bool para) \n  void helloVoid() \n  string helloNull() \n }<\/pre>\n<\/div>\n<p>\u5176\u4e2d\u5b9a\u4e49\u4e86\u670d\u52a1 Hello \u7684\u4e94\u4e2a\u65b9\u6cd5\uff0c\u6bcf\u4e2a\u65b9\u6cd5\u5305\u542b\u4e00\u4e2a\u65b9\u6cd5\u540d\uff0c\u53c2\u6570\u5217\u8868\u548c\u8fd4\u56de\u7c7b\u578b\u3002\u6bcf\u4e2a\u53c2\u6570\u5305\u62ec\u53c2\u6570\u5e8f\u53f7\uff0c\u53c2\u6570\u7c7b\u578b\u4ee5\u53ca\u53c2\u6570\u540d\u3002 Thrift \u662f\u5bf9 IDL(Interface Definition Language) \u63cf\u8ff0\u6027\u8bed\u8a00\u7684\u4e00\u79cd\u5177\u4f53\u5b9e\u73b0\u3002\u56e0\u6b64\uff0c\u4ee5\u4e0a\u7684\u670d\u52a1\u63cf\u8ff0\u6587\u4ef6\u4f7f\u7528 IDL \u8bed\u6cd5\u7f16\u5199\u3002\u4f7f\u7528 Thrift \u5de5\u5177\u7f16\u8bd1 Hello.thrift\uff0c\u5c31\u4f1a\u751f\u6210\u76f8\u5e94\u7684 Hello.java \u6587\u4ef6\u3002\u8be5\u6587\u4ef6\u5305\u542b\u4e86\u5728 Hello.thrift \u6587\u4ef6\u4e2d\u63cf\u8ff0\u7684\u670d\u52a1 Hello \u7684\u63a5\u53e3\u5b9a\u4e49\uff0c\u5373 Hello.Iface \u63a5\u53e3\uff0c\u4ee5\u53ca\u670d\u52a1\u8c03\u7528\u7684\u5e95\u5c42\u901a\u4fe1\u7ec6\u8282\uff0c\u5305\u62ec\u5ba2\u6237\u7aef\u7684\u8c03\u7528\u903b\u8f91 Hello.Client \u4ee5\u53ca\u670d\u52a1\u5668\u7aef\u7684\u5904\u7406\u903b\u8f91 Hello.Processor\uff0c\u7528\u4e8e\u6784\u5efa\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u7aef\u7684\u529f\u80fd\u3002<\/p>\n<p>\u521b\u5efa HelloServiceImpl.java \u6587\u4ef6\u5e76\u5b9e\u73b0 Hello.java \u6587\u4ef6\u4e2d\u7684 Hello.Iface \u63a5\u53e3\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<h5 id=\"listing2\">\u6e05\u5355 2. HelloServiceImpl.java<\/h5>\n<div class=\"codesection\">\n<pre class=\"displaycode\"> package service.demo; \n import org.apache.thrift.TException; \n public class HelloServiceImpl implements Hello.Iface { \n    @Override \n    public boolean helloBoolean(boolean para) throws TException { \n        return para; \n    } \n    @Override \n    public int helloInt(int para) throws TException { \n        try { \n            Thread.sleep(20000); \n        } catch (InterruptedException e) { \n            e.printStackTrace(); \n        } \n        return para; \n    } \n    @Override \n    public String helloNull() throws TException { \n        return null; \n    } \n    @Override \n    public String helloString(String para) throws TException { \n        return para; \n    } \n    @Override \n    public void helloVoid() throws TException { \n        System.out.println(\"Hello World\"); \n    } \n }<\/pre>\n<\/div>\n<p>\u521b\u5efa\u670d\u52a1\u5668\u7aef\u5b9e\u73b0\u4ee3\u7801\uff0c\u5c06 HelloServiceImpl \u4f5c\u4e3a\u5177\u4f53\u7684\u5904\u7406\u5668\u4f20\u9012\u7ed9 Thrift \u670d\u52a1\u5668\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<h5 id=\"listing3\">\u6e05\u5355 3. HelloServiceServer.java<\/h5>\n<div class=\"codesection\">\n<pre class=\"displaycode\"> package service.server; \n import org.apache.thrift.TProcessor; \n import org.apache.thrift.protocol.TBinaryProtocol; \n import org.apache.thrift.protocol.TBinaryProtocol.Factory; \n import org.apache.thrift.server.TServer; \n import org.apache.thrift.server.TThreadPoolServer; \n import org.apache.thrift.transport.TServerSocket; \n import org.apache.thrift.transport.TTransportException; \n import service.demo.Hello; \n import service.demo.HelloServiceImpl; \n\n public class HelloServiceServer { \n    \/** \n     * \u542f\u52a8 Thrift \u670d\u52a1\u5668\n     * @param args \n     *\/ \n    public static void main(String[] args) { \n        try { \n            \/\/ \u8bbe\u7f6e\u670d\u52a1\u7aef\u53e3\u4e3a 7911 \n            TServerSocket serverTransport = new TServerSocket(7911); \n            \/\/ \u8bbe\u7f6e\u534f\u8bae\u5de5\u5382\u4e3a TBinaryProtocol.Factory \n            Factory proFactory = new TBinaryProtocol.Factory(); \n            \/\/ \u5173\u8054\u5904\u7406\u5668\u4e0e Hello \u670d\u52a1\u7684\u5b9e\u73b0\n            TProcessor processor = new Hello.Processor(new HelloServiceImpl()); \n            TServer server = new TThreadPoolServer(processor, serverTransport, \n                    proFactory); \n            System.out.println(\"Start server on port 7911...\"); \n            server.serve(); \n        } catch (TTransportException e) { \n            e.printStackTrace(); \n        } \n    } \n }<\/pre>\n<\/div>\n<p>\u521b\u5efa\u5ba2\u6237\u7aef\u5b9e\u73b0\u4ee3\u7801\uff0c\u8c03\u7528 Hello.client \u8bbf\u95ee\u670d\u52a1\u7aef\u7684\u903b\u8f91\u5b9e\u73b0\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<h5 id=\"listing4\">\u6e05\u5355 4. HelloServiceClient.java<\/h5>\n<div class=\"codesection\">\n<pre class=\"displaycode\"> package service.client; \n import org.apache.thrift.TException; \n import org.apache.thrift.protocol.TBinaryProtocol; \n import org.apache.thrift.protocol.TProtocol; \n import org.apache.thrift.transport.TSocket; \n import org.apache.thrift.transport.TTransport; \n import org.apache.thrift.transport.TTransportException; \n import service.demo.Hello; \n\n public class HelloServiceClient { \n \/** \n     * \u8c03\u7528 Hello \u670d\u52a1\n     * @param args \n     *\/ \n    public static void main(String[] args) { \n        try { \n            \/\/ \u8bbe\u7f6e\u8c03\u7528\u7684\u670d\u52a1\u5730\u5740\u4e3a\u672c\u5730\uff0c\u7aef\u53e3\u4e3a 7911 \n            TTransport transport = new TSocket(\"localhost\", 7911); \n            transport.open(); \n            \/\/ \u8bbe\u7f6e\u4f20\u8f93\u534f\u8bae\u4e3a TBinaryProtocol \n            TProtocol protocol = new TBinaryProtocol(transport); \n            Hello.Client client = new Hello.Client(protocol); \n            \/\/ \u8c03\u7528\u670d\u52a1\u7684 helloVoid \u65b9\u6cd5\n            client.helloVoid(); \n            transport.close(); \n        } catch (TTransportException e) { \n            e.printStackTrace(); \n        } catch (TException e) { \n            e.printStackTrace(); \n        } \n    } \n }<\/pre>\n<\/div>\n<p>\u4ee3\u7801\u7f16\u5199\u5b8c\u540e\u8fd0\u884c\u670d\u52a1\u5668\uff0c\u518d\u542f\u52a8\u5ba2\u6237\u7aef\u8c03\u7528\u670d\u52a1 Hello \u7684\u65b9\u6cd5 helloVoid\uff0c\u5728\u670d\u52a1\u5668\u7aef\u7684\u63a7\u5236\u53f0\u7a97\u53e3\u8f93\u51fa\u201cHello World\u201d(helloVoid \u65b9\u6cd5\u5b9e\u73b0\u5728\u63a7\u5236\u53f0\u6253\u5370\u5b57\u7b26\u4e32\uff0c\u6ca1\u6709\u8fd4\u56de\u503c\uff0c\u6240\u4ee5\u5ba2\u6237\u7aef\u8c03\u7528\u65b9\u6cd5\u540e\u6ca1\u6709\u8fd4\u56de\u503c\u8f93\u51fa\uff0c\u8bfb\u8005\u53ef\u4ee5\u81ea\u5df1\u5c1d\u8bd5\u5176\u4ed6\u6709\u8fd4\u56de\u503c\u65b9\u6cd5\u7684\u8c03\u7528\uff0c\u5176\u7ed3\u679c\u53ef\u4ee5\u6253\u5370\u5728\u5ba2\u6237\u7aef\u7684\u63a7\u5236\u53f0\u7a97\u53e3 )\u3002<\/p>\n<div class=\"ibm-alternate-rule\"><\/div>\n<p class=\"ibm-ind-link ibm-back-to-top\"><a class=\"ibm-anchor-up-link\" href=\"http:\/\/www.ibm.com\/developerworks\/cn\/java\/j-lo-apachethrift\/#ibm-pcon\">\u56de\u9875\u9996<\/a><\/p>\n<h2 id=\"major3\">Thrift \u67b6\u6784<\/h2>\n<p>Thrift \u5305\u542b\u4e00\u4e2a\u5b8c\u6574\u7684\u5806\u6808\u7ed3\u6784\u7528\u4e8e\u6784\u5efa\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u7aef\u3002\u4e0b\u56fe\u63cf\u7ed8\u4e86 Thrift \u7684\u6574\u4f53\u67b6\u6784\u3002<\/p>\n<h5 id=\"fig1\">\u56fe 1. \u67b6\u6784\u56fe<\/h5>\n<p><img decoding=\"async\" src=\"http:\/\/www.ibm.com\/developerworks\/cn\/java\/j-lo-apachethrift\/image003.jpg\" alt=\"\u56fe 1. \u67b6\u6784\u56fe\" width=\"348\" \/><\/p>\n<p>\u5982\u56fe\u6240\u793a\uff0c\u56fe\u4e2d\u9ec4\u8272\u90e8\u5206\u662f\u7528\u6237\u5b9e\u73b0\u7684\u4e1a\u52a1\u903b\u8f91\uff0c\u8910\u8272\u90e8\u5206\u662f\u6839\u636e Thrift \u5b9a\u4e49\u7684\u670d\u52a1\u63a5\u53e3\u63cf\u8ff0\u6587\u4ef6\u751f\u6210\u7684\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u7aef\u4ee3\u7801\u6846\u67b6\uff0c\u7ea2\u8272\u90e8\u5206\u662f\u6839\u636e Thrift \u6587\u4ef6\u751f\u6210\u4ee3\u7801\u5b9e\u73b0\u6570\u636e\u7684\u8bfb\u5199\u64cd\u4f5c\u3002\u7ea2\u8272\u90e8\u5206\u4ee5\u4e0b\u662f Thrift \u7684\u4f20\u8f93\u4f53\u7cfb\u3001\u534f\u8bae\u4ee5\u53ca\u5e95\u5c42 I\/O \u901a\u4fe1\uff0c\u4f7f\u7528 Thrift \u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u5b9a\u4e49\u4e00\u4e2a\u670d\u52a1\u5e76\u4e14\u9009\u62e9\u4e0d\u540c\u7684\u4f20\u8f93\u534f\u8bae\u548c\u4f20\u8f93\u5c42\u800c\u4e0d\u7528\u91cd\u65b0\u751f\u6210\u4ee3\u7801\u3002<\/p>\n<p>Thrift \u670d\u52a1\u5668\u5305\u542b\u7528\u4e8e\u7ed1\u5b9a\u534f\u8bae\u548c\u4f20\u8f93\u5c42\u7684\u57fa\u7840\u67b6\u6784\uff0c\u5b83\u63d0\u4f9b\u963b\u585e\u3001\u975e\u963b\u585e\u3001\u5355\u7ebf\u7a0b\u548c\u591a\u7ebf\u7a0b\u7684\u6a21\u5f0f\u8fd0\u884c\u5728\u670d\u52a1\u5668\u4e0a\uff0c\u53ef\u4ee5\u914d\u5408\u670d\u52a1\u5668 \/ \u5bb9\u5668\u4e00\u8d77\u8fd0\u884c\uff0c\u53ef\u4ee5\u548c\u73b0\u6709\u7684 J2EE \u670d\u52a1\u5668 \/Web \u5bb9\u5668\u65e0\u7f1d\u7684\u7ed3\u5408\u3002<\/p>\n<p>\u670d\u52a1\u7aef\u548c\u5ba2\u6237\u7aef\u5177\u4f53\u7684\u8c03\u7528\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n<h5 id=\"fig2\">\u56fe 2. Server \u7aef\u542f\u52a8\u3001\u670d\u52a1\u65f6\u5e8f\u56fe\uff08<a href=\"http:\/\/www.ibm.com\/developerworks\/cn\/java\/j-lo-apachethrift\/image004.png\">\u67e5\u770b\u5927\u56fe<\/a>\uff09<\/h5>\n<p><img decoding=\"async\" src=\"http:\/\/www.ibm.com\/developerworks\/cn\/java\/j-lo-apachethrift\/image005.gif\" alt=\"\u56fe 2. Server \u7aef\u542f\u52a8\u3001\u670d\u52a1\u65f6\u5e8f\u56fe\" width=\"577\" \/><\/p>\n<p>\u8be5\u56fe\u6240\u793a\u662f HelloServiceServer \u542f\u52a8\u7684\u8fc7\u7a0b\u4ee5\u53ca\u670d\u52a1\u88ab\u5ba2\u6237\u7aef\u8c03\u7528\u65f6\uff0c\u670d\u52a1\u5668\u7684\u54cd\u5e94\u8fc7\u7a0b\u3002\u4ece\u56fe\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u7a0b\u5e8f\u8c03\u7528\u4e86 TThreadPoolServer \u7684 serve \u65b9\u6cd5\u540e\uff0cserver \u8fdb\u5165\u963b\u585e\u76d1\u542c\u72b6\u6001\uff0c\u5176\u963b\u585e\u5728 TServerSocket \u7684 accept \u65b9\u6cd5\u4e0a\u3002\u5f53\u63a5\u6536\u5230\u6765\u81ea\u5ba2\u6237\u7aef\u7684\u6d88\u606f\u540e\uff0c\u670d\u52a1\u5668\u53d1\u8d77\u4e00\u4e2a\u65b0\u7ebf\u7a0b\u5904\u7406\u8fd9\u4e2a\u6d88\u606f\u8bf7\u6c42\uff0c\u539f\u7ebf\u7a0b\u518d\u6b21\u8fdb\u5165\u963b\u585e\u72b6\u6001\u3002\u5728\u65b0\u7ebf\u7a0b\u4e2d\uff0c\u670d\u52a1\u5668\u901a\u8fc7 TBinaryProtocol \u534f\u8bae\u8bfb\u53d6\u6d88\u606f\u5185\u5bb9\uff0c\u8c03\u7528 HelloServiceImpl \u7684 helloVoid \u65b9\u6cd5\uff0c\u5e76\u5c06\u7ed3\u679c\u5199\u5165 helloVoid_result \u4e2d\u4f20\u56de\u5ba2\u6237\u7aef\u3002<\/p>\n<h5 id=\"fig3\">\u56fe 3. Client \u7aef\u8c03\u7528\u670d\u52a1\u65f6\u5e8f\u56fe\uff08<a href=\"http:\/\/www.ibm.com\/developerworks\/cn\/java\/j-lo-apachethrift\/image006.png\">\u67e5\u770b\u5927\u56fe<\/a>\uff09<\/h5>\n<p><img decoding=\"async\" src=\"http:\/\/www.ibm.com\/developerworks\/cn\/java\/j-lo-apachethrift\/image007.gif\" alt=\"\u56fe 3. Client \u7aef\u8c03\u7528\u670d\u52a1\u65f6\u5e8f\u56fe\" width=\"573\" \/><\/p>\n<p>\u8be5\u56fe\u6240\u793a\u662f HelloServiceClient \u8c03\u7528\u670d\u52a1\u7684\u8fc7\u7a0b\u4ee5\u53ca\u63a5\u6536\u5230\u670d\u52a1\u5668\u7aef\u7684\u8fd4\u56de\u503c\u540e\u5904\u7406\u7ed3\u679c\u7684\u8fc7\u7a0b\u3002\u4ece\u56fe\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u7a0b\u5e8f\u8c03\u7528\u4e86 Hello.Client \u7684 helloVoid \u65b9\u6cd5\uff0c\u5728 helloVoid \u65b9\u6cd5\u4e2d\uff0c\u901a\u8fc7 send_helloVoid \u65b9\u6cd5\u53d1\u9001\u5bf9\u670d\u52a1\u7684\u8c03\u7528\u8bf7\u6c42\uff0c\u901a\u8fc7 recv_helloVoid \u65b9\u6cd5\u63a5\u6536\u670d\u52a1\u5904\u7406\u8bf7\u6c42\u540e\u8fd4\u56de\u7684\u7ed3\u679c\u3002<\/p>\n<div class=\"ibm-alternate-rule\"><\/div>\n<p class=\"ibm-ind-link ibm-back-to-top\"><a class=\"ibm-anchor-up-link\" href=\"http:\/\/www.ibm.com\/developerworks\/cn\/java\/j-lo-apachethrift\/#ibm-pcon\">\u56de\u9875\u9996<\/a><\/p>\n<h2 id=\"major4\">\u6570\u636e\u7c7b\u578b<\/h2>\n<p>Thrift \u811a\u672c\u53ef\u5b9a\u4e49\u7684\u6570\u636e\u7c7b\u578b\u5305\u62ec\u4ee5\u4e0b\u51e0\u79cd\u7c7b\u578b\uff1a<\/p>\n<ul class=\"ibm-bullet-list\">\n<li>\u57fa\u672c\u7c7b\u578b\uff1a<\/li>\n<\/ul>\n<ul class=\"ibm-bullet-list\">\n<\/ul>\n<ul class=\"ibm-bullet-list\">\n<li>bool\uff1a\u5e03\u5c14\u503c\uff0ctrue \u6216 false\uff0c\u5bf9\u5e94 Java \u7684 boolean<\/li>\n<li>byte\uff1a8 \u4f4d\u6709\u7b26\u53f7\u6574\u6570\uff0c\u5bf9\u5e94 Java \u7684 byte<\/li>\n<li>i16\uff1a16 \u4f4d\u6709\u7b26\u53f7\u6574\u6570\uff0c\u5bf9\u5e94 Java \u7684 short<\/li>\n<li>i32\uff1a32 \u4f4d\u6709\u7b26\u53f7\u6574\u6570\uff0c\u5bf9\u5e94 Java \u7684 int<\/li>\n<li>i64\uff1a64 \u4f4d\u6709\u7b26\u53f7\u6574\u6570\uff0c\u5bf9\u5e94 Java \u7684 long<\/li>\n<li>double\uff1a64 \u4f4d\u6d6e\u70b9\u6570\uff0c\u5bf9\u5e94 Java \u7684 double<\/li>\n<li>string\uff1a\u672a\u77e5\u7f16\u7801\u6587\u672c\u6216\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\uff0c\u5bf9\u5e94 Java \u7684 String<\/li>\n<\/ul>\n<ul class=\"ibm-bullet-list\">\n<li>\u7ed3\u6784\u4f53\u7c7b\u578b\uff1a\n<ul class=\"ibm-bullet-list\">\n<li>struct\uff1a\u5b9a\u4e49\u516c\u5171\u7684\u5bf9\u8c61\uff0c\u7c7b\u4f3c\u4e8e C \u8bed\u8a00\u4e2d\u7684\u7ed3\u6784\u4f53\u5b9a\u4e49\uff0c\u5728 Java \u4e2d\u662f\u4e00\u4e2a JavaBean<\/li>\n<\/ul>\n<\/li>\n<li>\u5bb9\u5668\u7c7b\u578b\uff1a\n<ul class=\"ibm-bullet-list\">\n<li>list\uff1a\u5bf9\u5e94 Java \u7684 ArrayList<\/li>\n<li>set\uff1a\u5bf9\u5e94 Java \u7684 HashSet<\/li>\n<li>map\uff1a\u5bf9\u5e94 Java \u7684 HashMap<\/li>\n<\/ul>\n<\/li>\n<li>\u5f02\u5e38\u7c7b\u578b\uff1a\n<ul class=\"ibm-bullet-list\">\n<li>exception\uff1a\u5bf9\u5e94 Java \u7684 Exception<\/li>\n<\/ul>\n<\/li>\n<li>\u670d\u52a1\u7c7b\u578b\uff1a\n<ul class=\"ibm-bullet-list\">\n<li>service\uff1a\u5bf9\u5e94\u670d\u52a1\u7684\u7c7b<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<div class=\"ibm-alternate-rule\"><\/div>\n<p class=\"ibm-ind-link ibm-back-to-top\"><a class=\"ibm-anchor-up-link\" href=\"http:\/\/www.ibm.com\/developerworks\/cn\/java\/j-lo-apachethrift\/#ibm-pcon\">\u56de\u9875\u9996<\/a><\/p>\n<h2 id=\"major5\">\u534f\u8bae<\/h2>\n<p>Thrift \u53ef\u4ee5\u8ba9\u7528\u6237\u9009\u62e9\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u7aef\u4e4b\u95f4\u4f20\u8f93\u901a\u4fe1\u534f\u8bae\u7684\u7c7b\u522b\uff0c\u5728\u4f20\u8f93\u534f\u8bae\u4e0a\u603b\u4f53\u5212\u5206\u4e3a\u6587\u672c (text) \u548c\u4e8c\u8fdb\u5236 (binary) \u4f20\u8f93\u534f\u8bae\uff0c\u4e3a\u8282\u7ea6\u5e26\u5bbd\uff0c\u63d0\u9ad8\u4f20\u8f93\u6548\u7387\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u4f7f\u7528\u4e8c\u8fdb\u5236\u7c7b\u578b\u7684\u4f20\u8f93\u534f\u8bae\u4e3a\u591a\u6570\uff0c\u6709\u65f6\u8fd8\u4f1a\u4f7f\u7528\u57fa\u4e8e\u6587\u672c\u7c7b\u578b\u7684\u534f\u8bae\uff0c\u8fd9\u9700\u8981\u6839\u636e\u9879\u76ee \/ \u4ea7\u54c1\u4e2d\u7684\u5b9e\u9645\u9700\u6c42\u3002\u5e38\u7528\u534f\u8bae\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a<\/p>\n<ul class=\"ibm-bullet-list\">\n<li>TBinaryProtocol \u2014\u2014 \u4e8c\u8fdb\u5236\u7f16\u7801\u683c\u5f0f\u8fdb\u884c\u6570\u636e\u4f20\u8f93\u4f7f\u7528\u65b9\u6cd5\u5982\u6e05\u5355 3 \u548c\u6e05\u5355 4 \u6240\u793a\u3002<\/li>\n<li>TCompactProtocol \u2014\u2014 \u9ad8\u6548\u7387\u7684\u3001\u5bc6\u96c6\u7684\u4e8c\u8fdb\u5236\u7f16\u7801\u683c\u5f0f\u8fdb\u884c\u6570\u636e\u4f20\u8f93\u6784\u5efa TCompactProtocol \u534f\u8bae\u7684\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u53ea\u9700\u66ff\u6362\u6e05\u5355 3 \u548c\u6e05\u5355 4 \u4e2d TBinaryProtocol \u534f\u8bae\u90e8\u5206\u5373\u53ef\uff0c\u66ff\u6362\u6210\u5982\u4e0b\u4ee3\u7801\uff1a<br \/>\n<h5 id=\"listing5\">\u6e05\u5355 5. \u4f7f\u7528 TCompactProtocol \u534f\u8bae\u6784\u5efa\u7684 HelloServiceServer.java<\/h5>\n<div class=\"codesection\">\n<pre class=\"displaycode\"> TCompactProtocol.Factory proFactory = new TCompactProtocol.Factory();<\/pre>\n<\/div>\n<h5 id=\"listing6\">\u6e05\u5355 6. \u4f7f\u7528 TCompactProtocol \u534f\u8bae\u7684 HelloServiceClient.java<\/h5>\n<div class=\"codesection\">\n<pre class=\"displaycode\"> TCompactProtocol protocol = new TCompactProtocol(transport);<\/pre>\n<\/div>\n<\/li>\n<li>TJSONProtocol \u2014\u2014 \u4f7f\u7528 JSON \u7684\u6570\u636e\u7f16\u7801\u534f\u8bae\u8fdb\u884c\u6570\u636e\u4f20\u8f93\u6784\u5efa TJSONProtocol \u534f\u8bae\u7684\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u53ea\u9700\u66ff\u6362\u6e05\u5355 3 \u548c\u6e05\u5355 4 \u4e2d TBinaryProtocol \u534f\u8bae\u90e8\u5206\u5373\u53ef\uff0c\u66ff\u6362\u6210\u5982\u4e0b\u4ee3\u7801\uff1a<br \/>\n<h5 id=\"listing7\">\u6e05\u5355 7. \u4f7f\u7528 TJSONProtocol \u534f\u8bae\u6784\u5efa\u7684 HelloServiceServer.java<\/h5>\n<div class=\"codesection\">\n<pre class=\"displaycode\"> TJSONProtocol.Factory proFactory = new TJSONProtocol.Factory();<\/pre>\n<\/div>\n<h5 id=\"listing8\">\u6e05\u5355 8. \u4f7f\u7528 TJSONProtocol \u534f\u8bae\u7684 HelloServiceClient.java<\/h5>\n<div class=\"codesection\">\n<pre class=\"displaycode\"> TJSONProtocol protocol = new TJSONProtocol(transport);<\/pre>\n<\/div>\n<\/li>\n<li>TSimpleJSONProtocol \u2014\u2014 \u53ea\u63d0\u4f9b JSON \u53ea\u5199\u7684\u534f\u8bae\uff0c\u9002\u7528\u4e8e\u901a\u8fc7\u811a\u672c\u8bed\u8a00\u89e3\u6790<\/li>\n<\/ul>\n<div class=\"ibm-alternate-rule\"><\/div>\n<p class=\"ibm-ind-link ibm-back-to-top\"><a class=\"ibm-anchor-up-link\" href=\"http:\/\/www.ibm.com\/developerworks\/cn\/java\/j-lo-apachethrift\/#ibm-pcon\">\u56de\u9875\u9996<\/a><\/p>\n<h2 id=\"major6\">\u4f20\u8f93\u5c42<\/h2>\n<p>\u5e38\u7528\u7684\u4f20\u8f93\u5c42\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a<\/p>\n<ul class=\"ibm-bullet-list\">\n<li>TSocket \u2014\u2014 \u4f7f\u7528\u963b\u585e\u5f0f I\/O \u8fdb\u884c\u4f20\u8f93\uff0c\u662f\u6700\u5e38\u89c1\u7684\u6a21\u5f0f\u4f7f\u7528\u65b9\u6cd5\u5982\u6e05\u5355 4 \u6240\u793a\u3002<\/li>\n<li>TFramedTransport \u2014\u2014 \u4f7f\u7528\u975e\u963b\u585e\u65b9\u5f0f\uff0c\u6309\u5757\u7684\u5927\u5c0f\u8fdb\u884c\u4f20\u8f93\uff0c\u7c7b\u4f3c\u4e8e Java \u4e2d\u7684 NIO\u82e5\u4f7f\u7528 TFramedTransport \u4f20\u8f93\u5c42\uff0c\u5176\u670d\u52a1\u5668\u5fc5\u987b\u4fee\u6539\u4e3a\u975e\u963b\u585e\u7684\u670d\u52a1\u7c7b\u578b\uff0c\u5ba2\u6237\u7aef\u53ea\u9700\u66ff\u6362\u6e05\u5355 4 \u4e2d TTransport \u90e8\u5206\uff0c\u4ee3\u7801\u5982\u4e0b\uff0c\u6e05\u5355 9 \u4e2d TNonblockingServerTransport \u7c7b\u662f\u6784\u5efa\u975e\u963b\u585e socket \u7684\u62bd\u8c61\u7c7b\uff0cTNonblockingServerSocket \u7c7b\u7ee7\u627f TNonblockingServerTransport<br \/>\n<h5 id=\"listing9\">\u6e05\u5355 9. \u4f7f\u7528 TFramedTransport \u4f20\u8f93\u5c42\u6784\u5efa\u7684 HelloServiceServer.java<\/h5>\n<div class=\"codesection\">\n<pre class=\"displaycode\"> TNonblockingServerTransport serverTransport; \n serverTransport = new TNonblockingServerSocket(10005); \n Hello.Processor processor = new Hello.Processor(new HelloServiceImpl()); \n TServer server = new TNonblockingServer(processor, serverTransport); \n System.out.println(\"Start server on port 10005 ...\"); \n server.serve();<\/pre>\n<\/div>\n<h5 id=\"listing10\">\u6e05\u5355 10. \u4f7f\u7528 TFramedTransport \u4f20\u8f93\u5c42\u7684 HelloServiceClient.java<\/h5>\n<div class=\"codesection\">\n<pre class=\"displaycode\"> TTransport transport = new TFramedTransport(new TSocket(\"localhost\", 10005));<\/pre>\n<\/div>\n<\/li>\n<li>TNonblockingTransport \u2014\u2014 \u4f7f\u7528\u975e\u963b\u585e\u65b9\u5f0f\uff0c\u7528\u4e8e\u6784\u5efa\u5f02\u6b65\u5ba2\u6237\u7aef\u4f7f\u7528\u65b9\u6cd5\u8bf7\u53c2\u8003 Thrift \u5f02\u6b65\u5ba2\u6237\u7aef\u6784\u5efa<\/li>\n<\/ul>\n<div class=\"ibm-alternate-rule\"><\/div>\n<p class=\"ibm-ind-link ibm-back-to-top\"><a class=\"ibm-anchor-up-link\" href=\"http:\/\/www.ibm.com\/developerworks\/cn\/java\/j-lo-apachethrift\/#ibm-pcon\">\u56de\u9875\u9996<\/a><\/p>\n<h2 id=\"major7\">\u670d\u52a1\u7aef\u7c7b\u578b<\/h2>\n<p>\u5e38\u89c1\u7684\u670d\u52a1\u7aef\u7c7b\u578b\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a<\/p>\n<ul class=\"ibm-bullet-list\">\n<li>TSimpleServer \u2014\u2014 \u5355\u7ebf\u7a0b\u670d\u52a1\u5668\u7aef\u4f7f\u7528\u6807\u51c6\u7684\u963b\u585e\u5f0f I\/O\u4ee3\u7801\u5982\u4e0b\uff1a<br \/>\n<h5 id=\"listing11\">\u6e05\u5355 11. \u4f7f\u7528 TSimpleServer \u670d\u52a1\u7aef\u6784\u5efa\u7684 HelloServiceServer.java<\/h5>\n<div class=\"codesection\">\n<pre class=\"displaycode\"> TServerSocket serverTransport = new TServerSocket(7911); \n TProcessor processor = new Hello.Processor(new HelloServiceImpl()); \n TServer server = new TSimpleServer(processor, serverTransport); \n System.out.println(\"Start server on port 7911...\"); \n server.serve();<\/pre>\n<\/div>\n<p>\u5ba2\u6237\u7aef\u7684\u6784\u5efa\u65b9\u5f0f\u53ef\u53c2\u8003\u6e05\u5355 4\u3002<\/li>\n<li>TThreadPoolServer \u2014\u2014 \u591a\u7ebf\u7a0b\u670d\u52a1\u5668\u7aef\u4f7f\u7528\u6807\u51c6\u7684\u963b\u585e\u5f0f I\/O\u4f7f\u7528\u65b9\u6cd5\u5982\u6e05\u5355 3 \u6240\u793a\u3002<\/li>\n<li>TNonblockingServer \u2014\u2014 \u591a\u7ebf\u7a0b\u670d\u52a1\u5668\u7aef\u4f7f\u7528\u975e\u963b\u585e\u5f0f I\/O\u4f7f\u7528\u65b9\u6cd5\u8bf7\u53c2\u8003 Thrift \u5f02\u6b65\u5ba2\u6237\u7aef\u6784\u5efa<\/li>\n<\/ul>\n<div class=\"ibm-alternate-rule\"><\/div>\n<p class=\"ibm-ind-link ibm-back-to-top\"><a class=\"ibm-anchor-up-link\" href=\"http:\/\/www.ibm.com\/developerworks\/cn\/java\/j-lo-apachethrift\/#ibm-pcon\">\u56de\u9875\u9996<\/a><\/p>\n<h2 id=\"major8\">Thrift \u5f02\u6b65\u5ba2\u6237\u7aef\u6784\u5efa<\/h2>\n<p>Thrift \u63d0\u4f9b\u975e\u963b\u585e\u7684\u8c03\u7528\u65b9\u5f0f\uff0c\u53ef\u6784\u5efa\u5f02\u6b65\u5ba2\u6237\u7aef\u3002\u5728\u8fd9\u79cd\u65b9\u5f0f\u4e2d\uff0cThrift \u63d0\u4f9b\u4e86\u65b0\u7684\u7c7b TAsyncClientManager \u7528\u4e8e\u7ba1\u7406\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\uff0c\u5728\u4e00\u4e2a\u7ebf\u7a0b\u4e0a\u8ffd\u8e2a\u8bf7\u6c42\u548c\u54cd\u5e94\uff0c\u540c\u65f6\u901a\u8fc7\u63a5\u53e3 AsyncClient \u4f20\u9012\u6807\u51c6\u7684\u53c2\u6570\u548c callback \u5bf9\u8c61\uff0c\u670d\u52a1\u8c03\u7528\u5b8c\u6210\u540e\uff0ccallback \u63d0\u4f9b\u4e86\u5904\u7406\u8c03\u7528\u7ed3\u679c\u548c\u5f02\u5e38\u7684\u65b9\u6cd5\u3002<\/p>\n<p>\u9996\u5148\u6211\u4eec\u770b callback \u7684\u5b9e\u73b0\uff1a<\/p>\n<h5 id=\"listing12\">\u6e05\u5355 12.CallBack \u7684\u5b9e\u73b0\uff1aMethodCallback.java<\/h5>\n<div class=\"codesection\">\n<pre class=\"displaycode\"> package service.callback; \n import org.apache.thrift.async.AsyncMethodCallback; \n\n public class MethodCallback implements AsyncMethodCallback { \n    Object response = null; \n\n    public Object getResult() { \n        \/\/ \u8fd4\u56de\u7ed3\u679c\u503c\n        return this.response; \n    } \n\n    \/\/ \u5904\u7406\u670d\u52a1\u8fd4\u56de\u7684\u7ed3\u679c\u503c\n    @Override \n    public void onComplete(Object response) { \n        this.response = response; \n    } \n    \/\/ \u5904\u7406\u8c03\u7528\u670d\u52a1\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u7684\u5f02\u5e38\n    @Override \n    public void onError(Throwable throwable) { \n\n    } \n }<\/pre>\n<\/div>\n<p>\u5982\u4ee3\u7801\u6240\u793a\uff0conComplete \u65b9\u6cd5\u63a5\u6536\u670d\u52a1\u5904\u7406\u540e\u7684\u7ed3\u679c\uff0c\u6b64\u5904\u6211\u4eec\u5c06\u7ed3\u679c response \u76f4\u63a5\u8d4b\u503c\u7ed9 callback \u7684\u79c1\u6709\u5c5e\u6027 response\u3002onError \u65b9\u6cd5\u63a5\u6536\u670d\u52a1\u5904\u7406\u8fc7\u7a0b\u4e2d\u629b\u51fa\u7684\u5f02\u5e38\uff0c\u6b64\u5904\u672a\u5bf9\u5f02\u5e38\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<p>\u521b\u5efa\u975e\u963b\u585e\u670d\u52a1\u5668\u7aef\u5b9e\u73b0\u4ee3\u7801\uff0c\u5c06 HelloServiceImpl \u4f5c\u4e3a\u5177\u4f53\u7684\u5904\u7406\u5668\u4f20\u9012\u7ed9\u5f02\u6b65 Thrift \u670d\u52a1\u5668\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<h5 id=\"listing13\">\u6e05\u5355 13.HelloServiceAsyncServer.java<\/h5>\n<div class=\"codesection\">\n<pre class=\"displaycode\"> package service.server; \n import org.apache.thrift.server.TNonblockingServer; \n import org.apache.thrift.server.TServer; \n import org.apache.thrift.transport.TNonblockingServerSocket; \n import org.apache.thrift.transport.TNonblockingServerTransport; \n import org.apache.thrift.transport.TTransportException; \n import service.demo.Hello; \n import service.demo.HelloServiceImpl; \n\n public class HelloServiceAsyncServer { \n    \/** \n     * \u542f\u52a8 Thrift \u5f02\u6b65\u670d\u52a1\u5668\n     * @param args \n     *\/ \n    public static void main(String[] args) { \n        TNonblockingServerTransport serverTransport; \n        try { \n            serverTransport = new TNonblockingServerSocket(10005); \n            Hello.Processor processor = new Hello.Processor( \n                    new HelloServiceImpl()); \n            TServer server = new TNonblockingServer(processor, serverTransport); \n            System.out.println(\"Start server on port 10005 ...\"); \n            server.serve(); \n        } catch (TTransportException e) { \n            e.printStackTrace(); \n        } \n    } \n }<\/pre>\n<\/div>\n<p>HelloServiceAsyncServer \u901a\u8fc7 java.nio.channels.ServerSocketChannel \u521b\u5efa\u975e\u963b\u585e\u7684\u670d\u52a1\u5668\u7aef\u7b49\u5f85\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u3002<\/p>\n<p>\u521b\u5efa\u5f02\u6b65\u5ba2\u6237\u7aef\u5b9e\u73b0\u4ee3\u7801\uff0c\u8c03\u7528 Hello.AsyncClient \u8bbf\u95ee\u670d\u52a1\u7aef\u7684\u903b\u8f91\u5b9e\u73b0\uff0c\u5c06 MethodCallback \u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u8c03\u7528\u65b9\u6cd5\u4e2d\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<h5 id=\"listing14\">\u6e05\u5355 14.HelloServiceAsyncClient.java<\/h5>\n<div class=\"codesection\">\n<pre class=\"displaycode\"> package service.client; \n import java.io.IOException; \n import org.apache.thrift.async.AsyncMethodCallback; \n import org.apache.thrift.async.TAsyncClientManager; \n import org.apache.thrift.protocol.TBinaryProtocol; \n import org.apache.thrift.protocol.TProtocolFactory; \n import org.apache.thrift.transport.TNonblockingSocket; \n import org.apache.thrift.transport.TNonblockingTransport; \n import service.callback.MethodCallback; \n import service.demo.Hello; \n\n public class HelloServiceAsyncClient { \n    \/** \n     * \u8c03\u7528 Hello \u670d\u52a1\n     * @param args \n     *\/ \n    public static void main(String[] args) throws Exception { \n        try { \n            TAsyncClientManager clientManager = new TAsyncClientManager(); \n            TNonblockingTransport transport = new TNonblockingSocket( \n                    \"localhost\", 10005); \n            TProtocolFactory protocol = new TBinaryProtocol.Factory(); \n            Hello.AsyncClient asyncClient = new Hello.AsyncClient(protocol, \n                    clientManager, transport); \n            System.out.println(\"Client calls .....\"); \n            MethodCallback callBack = new MethodCallback(); \n            asyncClient.helloString(\"Hello World\", callBack); \n            Object res = callBack.getResult(); \n            while (res == null) { \n                res = callBack.getResult(); \n            } \n            System.out.println(((Hello.AsyncClient.helloString_call) res) \n                    .getResult()); \n        } catch (IOException e) { \n            e.printStackTrace(); \n        } \n  } \n }<\/pre>\n<\/div>\n<p>HelloServiceAsyncClient \u901a\u8fc7 java.nio.channels.Socketchannel \u521b\u5efa\u5f02\u6b65\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u5efa\u7acb\u8fde\u63a5\u3002\u5728\u672c\u6587\u4e2d\u5f02\u6b65\u5ba2\u6237\u7aef\u901a\u8fc7\u4ee5\u4e0b\u7684\u5faa\u73af\u4ee3\u7801\u5b9e\u73b0\u4e86\u540c\u6b65\u6548\u679c\uff0c\u8bfb\u8005\u53ef\u53bb\u9664\u8fd9\u90e8\u5206\u4ee3\u7801\u540e\u518d\u8fd0\u884c\u5bf9\u6bd4\u3002<\/p>\n<h5 id=\"N101A8\">\u6e05\u5355 15. \u5f02\u6b65\u5ba2\u6237\u7aef\u5b9e\u73b0\u540c\u6b65\u6548\u679c\u4ee3\u7801\u6bb5<\/h5>\n<div class=\"codesection\">\n<pre class=\"displaycode\">Object res = callBack.getResult();\n\/\/ \u7b49\u5f85\u670d\u52a1\u8c03\u7528\u540e\u7684\u8fd4\u56de\u7ed3\u679c\nwhile (res == null) {\n   res = callBack.getResult();\n}<\/pre>\n<\/div>\n<p>\u901a\u8fc7\u4e0e\u6e05\u5355 9 \u548c\u6e05\u5355 10 \u7684\u4ee3\u7801\u6bd4\u8f83\uff0c\u6211\u4eec\u53ef\u4ee5\u6784\u5efa\u4e00\u4e2a TNonblockingServer \u670d\u52a1\u7c7b\u578b\u7684\u670d\u52a1\u7aef\uff0c\u5728\u5ba2\u6237\u7aef\u6784\u5efa\u4e00\u4e2a TFramedTransport \u4f20\u8f93\u5c42\u7684\u540c\u6b65\u5ba2\u6237\u7aef\u548c\u4e00\u4e2a TNonblockingTransport \u4f20\u8f93\u5c42\u7684\u5f02\u6b65\u5ba2\u6237\u7aef\uff0c\u90a3\u4e48\u4e00\u4e2a\u670d\u52a1\u5c31\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a socket \u7aef\u53e3\u63d0\u4f9b\u4e24\u79cd\u4e0d\u540c\u7684\u8c03\u7528\u65b9\u5f0f\u3002\u6709\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u5c1d\u8bd5\u4e00\u4e0b\u3002<\/p>\n<div class=\"ibm-alternate-rule\"><\/div>\n<p class=\"ibm-ind-link ibm-back-to-top\"><a class=\"ibm-anchor-up-link\" href=\"http:\/\/www.ibm.com\/developerworks\/cn\/java\/j-lo-apachethrift\/#ibm-pcon\">\u56de\u9875\u9996<\/a><\/p>\n<h2 id=\"major9\">\u5e38\u89c1\u95ee\u9898<\/h2>\n<h3 id=\"minor9.1\">NULL \u95ee\u9898<\/h3>\n<p>\u6211\u4eec\u5728\u5bf9\u670d\u52a1\u7684\u67d0\u4e2a\u65b9\u6cd5\u8c03\u7528\u65f6\uff0c\u6709\u65f6\u4f1a\u51fa\u73b0\u8be5\u65b9\u6cd5\u8fd4\u56de null \u503c\u7684\u60c5\u51b5\uff0c\u5728 Thrift \u4e2d\uff0c\u76f4\u63a5\u8c03\u7528\u4e00\u4e2a\u8fd4\u56de null \u503c\u7684\u65b9\u6cd5\u4f1a\u629b\u51fa TApplicationException \u5f02\u5e38\u3002\u5728\u6e05\u5355 2 \u4e2d\uff0cHelloServiceImpl \u91cc\u5b9e\u73b0\u4e86 helloNull \u65b9\u6cd5\uff0c\u8fd4\u56de null \u503c\uff0c\u6211\u4eec\u5728 HelloServiceClient.java \u4e2d\u52a0\u5165\u8c03\u7528\u8be5\u65b9\u6cd5\u7684\u4ee3\u7801\uff0c\u51fa\u73b0\u5982\u4e0b\u56fe\u6240\u793a\u7684\u5f02\u5e38\uff1a<\/p>\n<h5 id=\"fig4\">\u56fe 4. TApplicationException \u5f02\u5e38<\/h5>\n<p><img decoding=\"async\" src=\"http:\/\/www.ibm.com\/developerworks\/cn\/java\/j-lo-apachethrift\/image009.jpg\" alt=\"\u56fe 4. TApplicationException \u5f02\u5e38\" width=\"572\" \/><\/p>\n<p>\u4e3a\u4e86\u5904\u7406\u8fd4\u56de null \u503c\u60c5\u51b5\uff0c\u6211\u4eec\u8981\u6355\u83b7\u8be5\u5f02\u5e38\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\uff0c\u5177\u4f53\u5ba2\u6237\u7aef\u4ee3\u7801\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<h5 id=\"listing15\">\u6e05\u5355 16. \u5904\u7406\u670d\u52a1\u8fd4\u56de\u503c\u4e3a null \u7684\u4ee3\u7801<\/h5>\n<div class=\"codesection\">\n<pre class=\"displaycode\"> package service.client; \n import org.apache.thrift.TApplicationException; \n import org.apache.thrift.TException; \n import org.apache.thrift.protocol.TBinaryProtocol; \n import org.apache.thrift.protocol.TProtocol; \n import org.apache.thrift.transport.TSocket; \n import org.apache.thrift.transport.TTransport; \n import org.apache.thrift.transport.TTransportException; \n import service.demo.Hello; \n\n public class HelloServiceClient { \n    \/** \n     * \u8c03\u7528 Hello \u670d\u52a1\uff0c\u5e76\u5904\u7406 null \u503c\u95ee\u9898\n     * @param args \n     *\/ \n    public static void main(String[] args) { \n        try { \n            TTransport transport = new TSocket(\"localhost\", 7911); \n            transport.open(); \n            TProtocol protocol = new TBinaryProtocol(transport); \n            Hello.Client client = new Hello.Client(protocol); \n            System.out.println(client.helloNull()); \n            transport.close(); \n        } catch (TTransportException e) { \n            e.printStackTrace(); \n        } catch (TException e) { \n            if (e instanceof TApplicationException \n                    &amp;&amp; ((TApplicationException) e).getType() ==   \n                                 TApplicationException.MISSING_RESULT) { \n                System.out.println(\"The result of helloNull function is NULL\"); \n            } \n        } \n    } \n }<\/pre>\n<\/div>\n<p>\u8c03\u7528 helloNull \u65b9\u6cd5\u540e\uff0c\u4f1a\u629b\u51fa TApplicationException \u5f02\u5e38\uff0c\u5e76\u4e14\u5f02\u5e38\u79cd\u7c7b\u4e3a MISSING_RESULT\uff0c\u672c\u6bb5\u4ee3\u7801\u663e\u793a\uff0c\u6355\u83b7\u8be5\u5f02\u5e38\u540e\uff0c\u76f4\u63a5\u5728\u63a7\u5236\u53f0\u6253\u5370\u201cThe result of helloNull function is NULL\u201d\u4fe1\u606f\u3002<\/p>\n<div class=\"ibm-alternate-rule\"><\/div>\n<p class=\"ibm-ind-link ibm-back-to-top\"><a class=\"ibm-anchor-up-link\" href=\"http:\/\/www.ibm.com\/developerworks\/cn\/java\/j-lo-apachethrift\/#ibm-pcon\">\u56de\u9875\u9996<\/a><\/p>\n<h2 id=\"major10\">\u5b89\u88c5\u90e8\u7f72<\/h2>\n<p>Apache Thrift \u7684\u5b98\u65b9\u7f51\u7ad9\u4e3a\uff1a<a href=\"http:\/\/thrift.apache.org\/tutorial\/%20\">http:\/\/thrift.apache.org\/tutorial\/<\/a>\uff0c\u5177\u4f53\u5b89\u88c5\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<ol type=\"1\">\n<li>\u4e0b\u8f7d thrift \u6e90\u6587\u4ef6\uff08<a href=\"http:\/\/svn.apache.org\/repos\/asf\/thrift\/tags\/thrift-0.6.1\/\">http:\/\/svn.apache.org\/repos\/asf\/thrift\/tags\/thrift-0.6.1\/<\/a>\uff09<\/li>\n<li>\u5c06 thrift \u6e90\u6587\u4ef6\u5bfc\u5165 eclipse\uff0c\u8fdb\u5165 \/lib\/java \u76ee\u5f55\uff0c\u4f7f\u7528 ant \u7f16\u8bd1 build.xml \u83b7\u5f97 libthrift-0.6.1-snapshot.jar<\/li>\n<li>\u5c06 libthrift-0.6.1-snapshot.jar\u3001slf4j-api-1.5.8.jar\u3001slf4j-log4j12-1.5.8.jar \u548c log4j-1.2.14.jar \u5bfc\u5165 eclipse \u5f00\u53d1\u73af\u5883<\/li>\n<li>\u4e0b\u8f7d thrift \u7f16\u8bd1\u5de5\u5177\uff0c\u8be5\u5de5\u5177\u53ef\u5c06 thrift \u811a\u672c\u6587\u4ef6\u7f16\u8bd1\u6210 java \u6587\u4ef6\uff0c\u4e0b\u8f7d\u5730\u5740\uff1a<a href=\"http:\/\/apache.etoak.com\/thrift\/0.6.0\/thrift-0.6.1.exe\">http:\/\/apache.etoak.com\/\/thrift\/0.6.0\/thrift-0.6.1.exe<\/a><\/li>\n<li>\u521b\u5efa Hello.thrift \u811a\u672c\u6587\u4ef6\uff0c\u5177\u4f53\u4ee3\u7801\u5982\u4e0a\u4e00\u7ae0\u8282\u6240\u8ff0\uff0c\u8fdb\u5165 thrift-0.6.1.exe \u6240\u5728\u76ee\u5f55\uff0c\u6267\u884c\u547d\u4ee4\"thrift-0.6.1.exe -gen java x:\\Hello.thrift\"\uff0c\u5728\u5f53\u524d\u8fd0\u884c\u76d8\u7b26\u4e0b\uff0c\u53ef\u770b\u89c1 gen-java \u76ee\u5f55\uff0c\u8fdb\u5165\u76ee\u5f55\u53ef\u770b\u5230\u751f\u6210\u7684 Java \u4ee3\u7801\u3002\u66f4\u591a thrift \u7684\u547d\u4ee4\u5185\u5bb9\uff0c\u8bf7\u53c2\u8003 thrift \u81ea\u5e26\u7684 help \u547d\u4ee4<\/li>\n<li>\u7f16\u5199\u670d\u52a1\u7aef\u548c\u5ba2\u6237\u7aef\u4ee3\u7801\uff0c\u5b8c\u6210 thrift \u7684\u5b89\u88c5\u548c\u90e8\u7f72<\/li>\n<\/ol>\n<p>\u57fa\u4e8e Apache Thrift \u6846\u67b6\u751f\u6210\u7684\u670d\u52a1\u5305\u62ec\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u7aef\uff0c\u5177\u4f53\u7684\u90e8\u7f72\u6a21\u5f0f\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<h5 id=\"fig5\">\u56fe 5. \u90e8\u7f72\u56fe<\/h5>\n<p><img decoding=\"async\" src=\"http:\/\/www.ibm.com\/developerworks\/cn\/java\/j-lo-apachethrift\/image011.jpg\" alt=\"\u56fe 5. \u90e8\u7f72\u56fe\" width=\"519\" \/><\/p>\n<p>\u4ece\u56fe\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u7aef\u90e8\u7f72\u65f6\uff0c\u9700\u8981\u7528\u5230\u516c\u5171\u7684 jar \u5305\u548c java \u6587\u4ef6\uff0c\u5982\u56fe\u201cCommon file\u201d\u533a\u57df\uff0c\u5176\u4e2d Hello.java \u7531 Hello.thrift \u7f16\u8bd1\u800c\u6765\u3002\u5728\u670d\u52a1\u5668\u7aef\uff0c\u670d\u52a1\u5fc5\u987b\u5b9e\u73b0 Hello.Iface \u63a5\u53e3\uff0c\u540c\u65f6\u8981\u5305\u62ec\u670d\u52a1\u5668\u7684\u542f\u52a8\u4ee3\u7801 HelloServiceServer.java\u3002\u5728\u5ba2\u6237\u7aef\uff0c\u5305\u62ec\u5ba2\u6237\u7aef\u8c03\u7528\u670d\u52a1\u7684\u4ee3\u7801 HelloServiceClient.java\u3002\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u901a\u8fc7 Hello.java \u63d0\u4f9b\u7684 API \u5b9e\u73b0\u8fdc\u7a0b\u670d\u52a1\u8c03\u7528\u3002<\/p>\n<div class=\"ibm-alternate-rule\"><\/div>\n<p class=\"ibm-ind-link ibm-back-to-top\"><a class=\"ibm-anchor-up-link\" href=\"http:\/\/www.ibm.com\/developerworks\/cn\/java\/j-lo-apachethrift\/#ibm-pcon\">\u56de\u9875\u9996<\/a><\/p>\n<h2 id=\"major11\">\u603b\u7ed3<\/h2>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86 Apache Thrift \u7684\u5b89\u88c5\u90e8\u7f72\u548c\u67b6\u6784\uff0c\u5e76\u901a\u8fc7\u5927\u91cf\u5b9e\u4f8b\u4ecb\u7ecd\u4e86\u5728\u4e0d\u540c\u60c5\u51b5\u4e0b\u5982\u4f55\u4f7f\u7528 Apache Thrift \u6765\u6784\u5efa\u670d\u52a1\uff0c\u540c\u65f6\u7740\u91cd\u4ecb\u7ecd\u4e86 Thrift \u5f02\u6b65\u5ba2\u6237\u7aef\u7684\u6784\u5efa\uff0c\u5e0c\u671b\u80fd\u7ed9\u8bfb\u8005\u5e26\u6765\u4e00\u4e9b\u5e2e\u52a9\u3002<\/p>\n<p>\u6765\u6e90\uff1ahttp:\/\/www.ibm.com\/developerworks\/cn\/java\/j-lo-apachethrift\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Apache Thrift \u662f Facebook \u5b9e\u73b0\u7684\u4e00\u79cd\u9ad8\u6548\u7684\u3001\u652f\u6301\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\u7684\u8fdc\u7a0b\u670d\u52a1\u8c03\u7528\u7684\u6846\u67b6\u3002\u672c\u6587\u5c06\u4ece Java \u5f00\u53d1\u4eba\u5458\u89d2\u5ea6\u8be6\u7ec6\u4ecb\u7ecd Apache Thrift \u7684\u67b6\u6784\u3001\u5f00\u53d1\u548c\u90e8\u7f72\uff0c\u5e76\u4e14&#46;&#46;&#46;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[34],"tags":[5],"class_list":["post-1154","post","type-post","status-publish","format-standard","hentry","category-software","tag-5"],"views":2680,"_links":{"self":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/1154","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1154"}],"version-history":[{"count":0,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/1154\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}