1. 程式人生 > >Thrift安裝以及測試【java】

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

執行效果: