Thrift示例


http://blog.zhengdong.me/2012/05/10/hello-world-by-thrift-using-java/

demo.thrift 文件内容如下
namespace java com.vv.test

struct Item {
  1: i64 id,
  2: string content,
}

service CrawlingService {
    void write(1:list<Item> items),
}

使用命令自动生成文件
F:\>thrift-0.10.0.exe --gen java demo.thrift
然后拷贝到项目

然后编写样例代码

  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3.   
  4. import org.apache.thrift.TException;  
  5. import org.apache.thrift.protocol.TBinaryProtocol;  
  6. import org.apache.thrift.protocol.TProtocol;  
  7. import org.apache.thrift.server.TServer;  
  8. import org.apache.thrift.server.TThreadPoolServer;  
  9. import org.apache.thrift.transport.TServerSocket;  
  10. import org.apache.thrift.transport.TSocket;  
  11. import org.apache.thrift.transport.TTransport;  
  12. import org.apache.thrift.transport.TTransportException;  
  13.   
  14. import com.vv.test.CrawlingService;  
  15. import com.vv.test.Item;  
  16.   
  17. public class T {  
  18.     public static void main(String[] args) throws InterruptedException {  
  19.         new Thread(new Server()).start();  
  20.   
  21.         Thread.sleep(1000);  
  22.         Client client = new Client();  
  23.         List<Item> list = new ArrayList<Item>();  
  24.         for (int i = 1; i <= 10; i++) {  
  25.             Item item = new Item();  
  26.             item.setId(i);  
  27.             item.setContent("hello world " + i);  
  28.             list.add(item);  
  29.         }  
  30.         client.write(list);  
  31.     }  
  32.   
  33. }  
  34.   
  35. class Server implements Runnable {  
  36.     @Override  
  37.     public void run() {  
  38.         try {  
  39.             // Set port  
  40.             TServerSocket serverTransport = new TServerSocket(9090);  
  41.             // Set CrawlingHandler we defined before  
  42.             // to processor, which handles RPC calls  
  43.             // Remember, one service per server  
  44.             CrawlingHandler handler = new CrawlingHandler();  
  45.             CrawlingService.Processor<CrawlingService.Iface> processor = new CrawlingService.Processor<CrawlingService.Iface>(  
  46.                     handler);  
  47.   
  48.             TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));  
  49.   
  50.             System.out.println("Starting server on port 9090 ...");  
  51.             server.serve();  
  52.         } catch (TTransportException e) {  
  53.             e.printStackTrace();  
  54.         }  
  55.     }  
  56.   
  57. }  
  58.   
  59. class Client {  
  60.     public void write(List<Item> items) {  
  61.         TTransport transport;  
  62.         try {  
  63.             transport = new TSocket("localhost"9090);  
  64.             transport.open();  
  65.   
  66.             TProtocol protocol = new TBinaryProtocol(transport);  
  67.             CrawlingService.Client client = new CrawlingService.Client(protocol);  
  68.   
  69.             client.write(items);  
  70.             transport.close();  
  71.         } catch (TTransportException e) {  
  72.             e.printStackTrace();  
  73.         } catch (TException e) {  
  74.             e.printStackTrace();  
  75.         }  
  76.     }  
  77. }  
  78.   
  79. class CrawlingHandler implements CrawlingService.Iface {  
  80.     @Override  
  81.     public void write(List<Item> items) throws TException {  
  82.         for (Item item : items) {  
  83.             System.out.println(item);  
  84.         }  
  85.     };  
  86. }  

执行结果如下:


但是输出的最后一行显示"Received 1" ,这个输出是从哪里来的?以后慢慢再看吧.

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29254281/viewspace-2135577/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29254281/viewspace-2135577/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值