Thrift示例

壹頁書發表於2017-03-17

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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章