Thrift安装以及测试【java】
2017-06-05 14:15
387 查看
链接:http://download.csdn.net/detail/mantantan/9861020
链接部分是windows的thrift的安装包以及我测试的一个JAVA的项目源代码
名字修改为thrift.exe;这个时候后在CMD下就可以使用了。
(1)新建testJava.thrift文件,内容如下
(2)执行命令thrift --gen java testJava.thrift 生成如下所示的文件夹,那么已经成功了一大半了
(3)集合到程序中,代码在文章开始的链接中
HelloImpl.java 真正的实现类
服务器端的代码 Server.java
客户端的代码Client.java
运行效果:
链接部分是windows的thrift的安装包以及我测试的一个JAVA的项目源代码
1.thrift windows安装:
将安装包的exe放在一个盘符下,路径最好是没用中文和空格,配置环境变量;我放在C盘下的thrift文件夹下了,并将名字修改为thrift.exe;这个时候后在CMD下就可以使用了。
2.测试用例
(1)新建testJava.thrift文件,内容如下namespace java com.mtt.test service Hello { string helloString(1:string word) }
(2)执行命令thrift --gen java testJava.thrift 生成如下所示的文件夹,那么已经成功了一大半了
(3)集合到程序中,代码在文章开始的链接中
HelloImpl.java 真正的实现类
package com.mtt.test; import org.apache.thrift.TException; public class HelloImpl implements Hello.Iface{ /** * Hello的具体实现接口 */ @Override public String helloString(String word) throws TException { return "Server:"+word; } }
服务器端的代码 Server.java
package com.mtt.server; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TBinaryProtocol.Factory; import org.apache.thrift.server.TServer; import org.apache.thrift.server.TThreadPoolServer; import org.apache.thrift.server.TThreadPoolServer.Args; import org.apache.thrift.transport.TServerSocket; import org.apache.thrift.transport.TTransportException; import com.mtt.test.Hello; import com.mtt.test.Hello.Processor; import com.mtt.test.HelloImpl; public class Server { public void startServer() { try { System.out.println("thrift server open port 8889"); TServerSocket serverTransport = new TServerSocket(8889); Hello.Processor process = new Processor(new HelloImpl()); Factory portFactory = new TBinaryProtocol.Factory(true, true); Args args = new Args(serverTransport); args.processor(process); args.protocolFactory(portFactory); TServer server = new TThreadPoolServer(args); server.serve(); } catch (TTransportException e) { e.printStackTrace(); } } public static void main(String[] args) { System.out.println("thrift server init"); Server server = new Server(); System.out.println("thrift server start"); server.startServer(); System.out.println("thrift server end"); } }
客户端的代码Client.java
package com.mtt.client; import java.util.Scanner; 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.mtt.test.Hello; public class Client { public void startClient() { TTransport transport; try { System.out.println("thrift client connext server at 8889 port "); transport = new TSocket("localhost", 8889); TProtocol protocol = new TBinaryProtocol(transport); Hello.Client client = new Hello.Client(protocol); transport.open(); boolean bye=false; while(!bye){ Scanner in=new Scanner(System.in); String res=in.next(); if("bye".equals(res)){ bye=true; }else{ System.out.println(client.helloString(res)); } } transport.close(); System.out.println("thrift client close connextion"); } catch (TTransportException e) { e.printStackTrace(); } catch (TException e) { e.printStackTrace(); } } public static void main(String[] args) { System.out.println("thrift client init "); Client client = new Client(); System.out.println("thrift client start "); client.startClient(); System.out.println("thrift client end "); } }
运行效果:
相关文章推荐
- Apache Thrift入门(安装、测试与java程序编写)
- 安装部署(六) Kafka集群安装部署以及Producer和Consumer的JAVA代码测试
- Ubuntu环境下Thrift的安装、编译以及测试
- Thrift 学习笔记2——Windows环境下Thrift的安装、编译以及测试
- Thrift 学习笔记1——Ubuntu环境下Thrift的安装、编译以及测试
- 解决Linux下安装thrift出现的Building Java Library ........ : no问题(以及其他安装thrift出现的问题)
- Thrift 学习笔记2——Windows环境下Thrift的安装、编译以及测试
- Thrift在windows下的安装以及thrift命令生成java文件
- Thrift在windows下的安装以及thrift命令生成java文件
- Apache Thrift入门(安装、测试与java程序编写)
- Thrift 学习笔记1——Ubuntu环境下Thrift的安装、编译以及测试
- Thrift 学习笔记1——Ubuntu环境下Thrift的安装、编译以及测试 - 张进の专栏 - 博客频道 - CSDN.NET
- 如何制作Java可执行程序以及安装程序(补上了图片)
- Linux下JRE安装以及JAVA_HOME的设定
- 安装双机xen以及测试
- java collection 知识点 以及在测试中的应用【待学习】
- 【转】安装jdk配置环境变量并测试java
- Sysbench0.4.12安装以及测试
- Java tcp服务器框架 以及一个简单命令行聊天的测试例子
- Red Hat Linux9.0下配置JAVA环境以及安装Tomcat(个人备忘)