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 ");  
	    }    
}

运行效果:



你可能感兴趣的:(JAVA)