Thrift 示例(包含自定义异常, 枚举, 传输Java Bean)
2016-03-29 20:53
597 查看
编写demoHello.thrift
2.生成相关的Java类
可以看到枚举, 异常, Service都生成了,
生成的文件列表如下:
3.编写服务端的doAction实现
4.编写服务端
5.编写客户端
namespace java com.micmiu.thrift.demo enum RequestType{ SAY_HELLO, QUERY_TIME } struct Request{ 1: required RequestType type; 2: required string name; 3: optional i32 age; } exception RequestException{ 1: required i32 code; 2: optional string reson; } service HelloWorldService{ string doAction(1: Request request) throws (1: RequestException qe); }
2.生成相关的Java类
.\thrift-0.8.0.exe -r -gen java .\demoHello.thrift
可以看到枚举, 异常, Service都生成了,
生成的文件列表如下:
HelloWorldService.java Request.java RequestException.java RequestType.java
3.编写服务端的doAction实现
package com.micmiu.thrift.demo; import org.apache.commons.lang.StringUtils; import org.apache.thrift.TException; import java.util.Date; /** * Created by yinliang on 2016/3/29. */ public class HelloWordServiceImpl implements HelloWorldService.Iface { public String doAction(Request request) throws RequestException, TException { System.out.println("Get request: " + request); if (StringUtils.isBlank(request.getName()) || request.getType() == null) { throw new RequestException(); } String result = "Hello " + request.getName(); if (request.getType() == RequestType.SAY_HELLO) { result += ", Welcome"; } else { result += ", Now is " + new Date().toLocaleString(); } return result; } }
4.编写服务端
package com.micmiu.thrift.demo; import org.apache.thrift.TProcessor; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.server.TServer; import org.apache.thrift.server.TSimpleServer; import org.apache.thrift.transport.TServerSocket; import org.apache.thrift.transport.TTransportException; import java.io.IOException; import java.net.ServerSocket; /** * Created by yinliang on 2016/3/29. */ public class HelloWordServer { public static final int SERVER_PORT = 8090; public static void main(String[] args) { System.out.println("Runing server..."); try { TProcessor tProcessor = new HelloWorldService.Processor<HelloWorldService.Iface>(new HelloWordServiceImpl()); TServerSocket serverTransport = new TServerSocket(SERVER_PORT); TServer.Args tArgs = new TServer.Args(serverTransport); tArgs.processor(tProcessor); tArgs.protocolFactory(new TBinaryProtocol.Factory()); TServer server = new TSimpleServer(tArgs); server.serve(); } catch (TTransportException e) { System.out.println("Server start error!!!"); } } }
5.编写客户端
package com.micmiu.thrift.demo; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.transport.TSocket; import org.apache.thrift.transport.TTransport; /** * Created by yinliang on 2016/3/29. */ public class HelloWordClient { public static final String SERVER_IP = "localhost"; public static final int SERVER_PORT = 8090; public static final int TIMEOUT = 30000; public static void main(String[] args) { TTransport transport = null; try { transport = new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT); TProtocol protocol = new TBinaryProtocol(transport); HelloWorldService.Client client = new HelloWorldService.Client(protocol); transport.open(); Request request = new Request(); request.setAge(22); request.setName("zhangsan"); request.setType(RequestType.SAY_HELLO); String result = client.doAction(request); System.out.println(result); request.setType(RequestType.QUERY_TIME); result = client.doAction(request); System.out.println(result); transport.close(); } catch (Exception e) { } } }
相关文章推荐
- 白话Spring(中级篇)---拦截器(上)
- Java基础知识强化之IO流笔记78:NIO之 FileChannel
- SE Springer小组之《Spring音乐播放器》可行性研究报告一、二(转载)
- 深入理解 Java 虚拟机-Java 代码编译和执行的整个过程
- eclipse 中maven编译出现问题-Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOM
- SSH图片上传
- java中List对象列表去重或取出以及排序
- 20145229吴姗珊两天小总结 《Java程序设计》第4周学习总结
- 深入理解 Java 虚拟机之走进Java
- Spring 控制反转
- 三个最简单的排序算法:插入、冒泡、选择
- Java编程手册-Collection框架(上)
- SE Springer小组之《Spring音乐播放器》可行性研究报告五、六
- javacv1.1环境配置
- Spring概述
- Java守护线程
- Hello--Spring
- struts2实现让文件输入流流向浏览器显示
- Eclipse自动生成set,get方法
- 深入分析HashMap