Thrift安装以及测试【java】

链接:http://download.csdn.net/detail/mantantan/9861020

链接部分是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 ");  
	    }    
}

运行效果:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值