Thrift半同期半非同期のサービスモデル-THsHaServer(非同期呼び出しクライアント)

4465 ワード

半同期半非同期のサービス・エンド・モデルは、TFRamedTransportデータ転送の方法として指定する必要があります.
サービス側java:
package cn.slimsmart.thrift.demo.helloworld;

import org.apache.thrift.TException;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.THsHaServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;

/**
 *                   ,     : TFramedTransport        。 THsHaServer
 *    
 */
public class HelloTHsHaServer {
	//     
	public static final int SERVER_PORT = 8080;

	public static void main(String[] args) throws TException {
		TProcessor tprocessor = new HelloWorld.Processor<HelloWorld.Iface>(new HelloWorldImpl());
		//      -        
		TNonblockingServerSocket serverTransport = new TNonblockingServerSocket(SERVER_PORT);
		//      
		THsHaServer.Args tArgs = new THsHaServer.Args(serverTransport);
		tArgs.processor(tprocessor);
		tArgs.transportFactory(new TFramedTransport.Factory());
		//     
		tArgs.protocolFactory(new TBinaryProtocol.Factory());
		//            
		TServer server = new THsHaServer(tArgs);
		System.out.println("HelloTHsHaServer start....");
		server.serve(); //     
	}
}

クライアント
HelloAsyncClient.java
:
package cn.slimsmart.thrift.demo.helloworld;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.apache.thrift.TException;
import org.apache.thrift.async.AsyncMethodCallback;
import org.apache.thrift.async.TAsyncClientManager;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocolFactory;
import org.apache.thrift.transport.TNonblockingSocket;
import org.apache.thrift.transport.TNonblockingTransport;

import cn.slimsmart.thrift.demo.helloworld.HelloWorld.AsyncClient.sayHello_call;

/**
 *        ,      TNonblockingServer ,THsHaServer
 */
public class HelloAsyncClient {
	public static final String SERVER_IP = "127.0.0.1";
	public static final int SERVER_PORT = 8080;
	public static final int TIMEOUT = 30000;

	public static void main(String[] args) throws TException, IOException, InterruptedException {
		//       
		TAsyncClientManager clientManager = new TAsyncClientManager();
		//      ,     IO
		TNonblockingTransport transport = new TNonblockingSocket(SERVER_IP, SERVER_PORT, TIMEOUT);
		//          
		//TProtocolFactory tprotocol = new TCompactProtocol.Factory();
		TProtocolFactory tprotocol = new TBinaryProtocol.Factory();
		
		HelloWorld.AsyncClient asyncClient = new HelloWorld.AsyncClient(tprotocol, clientManager, transport);
		CountDownLatch latch = new CountDownLatch(1);
		AsynCallback callBack = new AsynCallback(latch);
		System.out.println("call method sayHello start ...");
		//     
		asyncClient.sayHello("jack", callBack);
		System.out.println("call method sayHello .... end");
		//        
		boolean wait = latch.await(30, TimeUnit.SECONDS);
		System.out.println("latch.await =:" + wait);
	}
}
class AsynCallback implements AsyncMethodCallback<sayHello_call> {
	private CountDownLatch latch;

	public AsynCallback(CountDownLatch latch) {
		this.latch = latch;
	}

	@Override
	public void onComplete(sayHello_call response) {
		System.out.println("onComplete");
		try {
			System.out.println("AsynCall result :" + response.getResult().toString());
		} catch (TException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			latch.countDown();
		}
	}
	@Override
	public void onError(Exception exception) {
		System.out.println("onError :" + exception.getMessage());
		latch.countDown();
	}
}

SSL暗号化プロトコルを使用する:
//SSL   
		TSSLTransportParameters parameters = new TSSLTransportParameters();
		//keystore      
		parameters.setKeyStore("../../.keystore", "thrift");
		TServerTransport serverTransport = TSSLTransportFactory.getServerSocket(8080, 3000, null, parameters);
		
		//SSL   
		TSSLTransportParameters parameters = new TSSLTransportParameters();
		parameters.setTrustStore("../../.trustore", "thrift", "SunX509", "JKS");
		TTransport tTransport = TSSLTransportFactory.getClientSocket("127.0.0.1", 8080, 3000, parameters);