thrift简单实例
来源:互联网 发布:protobuf数据解析 编辑:程序博客网 时间:2024/04/19 19:27
eclipse:jar依赖 libthrift-0.9.3.jar slf4j-api-1.7.21.jar slf4j-log4j12-1.7.21 log4j-1.2.17.jar
caculator.thrift
namespace java com.serverservice CaculatorService { i32 add(1:i32 x1, 2:i32 x2); i32 min(1:i32 x1, 2:i32 x2); i32 mul(1:i32 x1, 2:i32 x2); i32 div(1:i32 x1, 2:i32 x2);}
thrift -r -gen java caculator.thrift
gen-java/com/server/CaculatorService.java
1. CaculatorServiceHandler.java
package com.server;import org.apache.thrift.TException;public class CaculatorServiceHandler implements CaculatorService.Iface { @Override public int add(int x1, int x2) throws TException { // TODO Auto-generated method stub return x1 + x2; } @Override public int min(int x1, int x2) throws TException { // TODO Auto-generated method stub return x1 - x2; } @Override public int mul(int x1, int x2) throws TException { // TODO Auto-generated method stub return x1 * x2; } @Override public int div(int x1, int x2) throws TException { // TODO Auto-generated method stub return x1 / x2; }}
2. MyServer.java
package com.server;import org.apache.thrift.server.TServer;import org.apache.thrift.server.TServer.Args;import org.apache.thrift.server.TSimpleServer;import org.apache.thrift.transport.TServerSocket;import org.apache.thrift.transport.TServerTransport;import org.apache.thrift.transport.TTransportException;public class MyServer { public static void startServer(CaculatorService.Processor<CaculatorServiceHandler> processor) { try { TServerTransport serverTransport = new TServerSocket(8888); TServer server = new TSimpleServer(new Args(serverTransport).processor(processor)); System.out.println("Starting the simple server..."); server.serve(); } catch (TTransportException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { // TODO Auto-generated method stub startServer(new CaculatorService.Processor<CaculatorServiceHandler>(new CaculatorServiceHandler())); }}3. MyClient.java
package com.client;import java.util.Random;import org.apache.thrift.TException;import org.apache.thrift.protocol.TBinaryProtocol;import org.apache.thrift.protocol.TProtocol;import org.apache.thrift.transport.TSocket;import org.apache.thrift.transport.TTransport;import org.apache.thrift.transport.TTransportException;import com.server.CaculatorService;public class MyClient { public static void main(String[] args) { // TODO Auto-generated method stub TTransport transport = new TSocket("localhost", 8888); try { transport.open(); TProtocol protocal = new TBinaryProtocol(transport); CaculatorService.Client client = new CaculatorService.Client(protocal); Random random = new Random(); for (int i = 0; i < 10; ++i) { int x = random.nextInt(1000), y = random.nextInt(1000); try { System.out.println("" + x + " + " + y + " = " + client.add(x, y)); System.out.println("" + x + " - " + y + " = " + client.min(x, y)); System.out.println("" + x + " * " + y + " = " + client.mul(x, y)); System.out.println("" + x + " / " + y + " = " + client.div(x, y)); } catch (TException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } catch (TTransportException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { transport.close(); } }}
ide直接运行
Starting the simple server...
300 + 262 = 562
300 - 262 = 38
300 * 262 = 78600
300 / 262 = 1
279 + 866 = 1145
279 - 866 = -587
279 * 866 = 241614
279 / 866 = 0
554 + 808 = 1362
554 - 808 = -254
554 * 808 = 447632
554 / 808 = 0
311 + 910 = 1221
311 - 910 = -599
311 * 910 = 283010
311 / 910 = 0
7 + 212 = 219
7 - 212 = -205
7 * 212 = 1484
7 / 212 = 0
910 + 367 = 1277
910 - 367 = 543
910 * 367 = 333970
910 / 367 = 2
275 + 911 = 1186
275 - 911 = -636
275 * 911 = 250525
275 / 911 = 0
145 + 483 = 628
145 - 483 = -338
145 * 483 = 70035
145 / 483 = 0
326 + 720 = 1046
326 - 720 = -394
326 * 720 = 234720
326 / 720 = 0
792 + 435 = 1227
792 - 435 = 357
792 * 435 = 344520
792 / 435 = 1
- thrift简单实例
- Thrift在 Java中的简单实例(-)
- Thrift实例
- thrift 实例
- thrift实例
- Thrift在Java中的简单实例(二)
- Thrift在Java中的简单实例(三)
- Thrift在Java中的简单实例(四)
- Thrift 实例 Helloworld
- Thrift 实例 Helloworld
- Thrift使用实例
- thrift入门实例
- thrift 使用实例
- thrift使用实例
- 一个thrift实例
- thrift理解及实例
- Thrift之java实例
- Thrift之c++实例
- Android EventLog简介
- PCA主成分分析与SVG隐语义
- SQL题
- Intelj Idea 的全键盘使用
- Ubuntu 16.0.4 安装deb文件
- thrift简单实例
- MATLAB从数组中以等概率随机选出不同的元素
- 使用Opengl ES GlSurfaceView Render Camera preview
- linux添加环境变量(path)
- classpath、path、JAVA_HOME的作用
- Session对象的使用
- MySQL学习之路_数据的组织方式-表
- [总结]联想笔记本E460/虚拟机VMware10.0+Ubuntu14.04+opencv+Ros+pcl+OpenNI+g2o+DSO+Pangolin+win7(Ubuntu双系统)安装过程整理
- 中序和先序创建树