亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Thrift java服務(wù)器與客戶端示例 - john c - 博

系統(tǒng) 1865 0

Thrift java服務(wù)器與客戶端示例 - john c - 博客園

Thrift java服務(wù)器與客戶端示例

Posted on 2011-06-19 03:04 john c 閱讀(3540) 評論( 0 ) 編輯 收藏

簡單的實現(xiàn)一個PING的功能

1.安裝thrift

http://thrift.apache.org/download/

人人網(wǎng)鏡像下載:

http://labs.renren.com/apache-mirror/thrift/0.6.1/thrift-0.6.1.exe

2.編寫Thrift文件 (定義接口,結(jié)構(gòu),異常等),保存為test.thrift

namespace java net.johnc.thrift
service Test{
?
? void ping( 1 : i32 length)
?
}

?

3.生成接口代碼

把thrift-0.6.1.exe和test.thrift文件放在同一個目錄,當(dāng)然也可以把thrift-0.6.1.exe文件放進環(huán)境變量

進入DOS命令執(zhí)行:thrift-0.6.1.exe? - - gen? java ?test . thrift

生成文件 gen-java/net/johnc/thrift/Test.java

4.編寫服務(wù)端接口實現(xiàn)類

在POM.xml文件加入以下依賴:

<dependency>
?????? <groupId>org.apache.thrift</groupId>
?????? <artifactId>libthrift</artifactId>
?????? <version> 0.6 . 1 </version>
?? </dependency>

?

? 把生成的Test.java復(fù)制到項目下

            
1 package net.johnc.thrift; 2 3 ? import org.apache.thrift.TException; 4 5 ? public class TestImpl implements Test.Iface { 6 7 public void ping( int length) throws TException { 8 System.out.println( " calling ping ,length= " + length); 9 } 10 11 }

5.編寫啟動服務(wù)代碼

            
1 package net.johnc.thrift; 2 3 ? import net.johnc.thrift.Test.Processor; 4 5 ? import org.apache.thrift.protocol.TBinaryProtocol; 6 ? import org.apache.thrift.protocol.TBinaryProtocol.Factory; 7 ? import org.apache.thrift.server.TServer; 8 ? import org.apache.thrift.server.TThreadPoolServer; 9 ? import org.apache.thrift.server.TThreadPoolServer.Args; 10 ? import org.apache.thrift.transport.TServerSocket; 11 ? import org.apache.thrift.transport.TTransportException; 12 13 ? public class Server { 14 public void startServer() { 15 try { 16 17 TServerSocket serverTransport = new TServerSocket( 1234 ); 18 19 Test.Processor process = new Processor( new TestImpl()); 20 21 Factory portFactory = new TBinaryProtocol.Factory( true , true ); 22 23 Args args = new Args(serverTransport); 24 args.processor(process); 25 args.protocolFactory(portFactory); 26 27 TServer server = new TThreadPoolServer(args); 28 server.serve(); 29 } catch (TTransportException e) { 30 e.printStackTrace(); 31 } 32 } 33 34 public static void main(String[] args) { 35 Server server = new Server(); 36 server.startServer(); 37 } 38 }

?

6.編寫客戶端代碼

            
1 package net.johnc.thrift; 2 3 ? import org.apache.thrift.TException; 4 ? import org.apache.thrift.protocol.TBinaryProtocol; 5 ? import org.apache.thrift.protocol.TProtocol; 6 ? import org.apache.thrift.transport.TSocket; 7 ? import org.apache.thrift.transport.TTransport; 8 ? import org.apache.thrift.transport.TTransportException; 9 10 ? public class Client { 11 12 public void startClient() { 13 TTransport transport; 14 try { 15 transport = new TSocket( " localhost " , 1234 ); 16 TProtocol protocol = new TBinaryProtocol(transport); 17 Test.Client client = new Test.Client(protocol); 18 transport.open(); 19 client.ping( 2012 ); 20 transport.close(); 21 } catch (TTransportException e) { 22 e.printStackTrace(); 23 } catch (TException e) { 24 e.printStackTrace(); 25 } 26 } 27 28 public static void main(String[] args) { 29 Client client = new Client(); 30 client.startClient(); 31 } 32 }

Thrift java服務(wù)器與客戶端示例 - john c - 博客園


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 99久久精品免费看国产交换 | 欧美成人性做爰网站免费 | 日韩手机看片 | 成人毛片免费网站 | 91在线观 | 国产精品在线观看 | 久久久精品国产免费观看同学 | 欧美高清亚洲欧美一区h | 久久99精品久久久久久国产越南 | 九九热精品免费观看 | 青青青在线视频国产 | 国产九九在线视频 | 国产91久久久久久久免费 | 国产一级大片免费看 | 一道本不卡免费视频 | 成人特级毛片69免费观看 | 欧美成人精品免费播放 | 久久亚洲精品久久久久 | 天天操狠狠操夜夜操 | 中文字幕亚洲 综合久久 | 九九视频在线看精品 | 玖热在线 | 七七久久 | 性做久久久久久久免费看 | 久久精品小视频 | 成人欧美精品久久久久影院 | 亚洲一区二区三区不卡在线播放 | 香港一级a毛片在线播放 | 国产成人一区二区三区在线播放 | 国产丰满老厨女房乱 | 黑人特级粗α级毛片 | 亚洲好色网 | 视频一区二区欧美日韩在线 | 精品国产国产综合精品 | 五月婷婷开心综合 | 日日插日日操 | 四虎成年永久免费网站 | 成人夜视频 | 亚洲精品区一区二区三区四 | 亚色在线播放 | 又粗又硬又大久久久 |