当前位置:编程学习 > JAVA >>

thrift java多线程非阻塞同步/异步调用实例


首先创建thrift文件
[java]
</pre><p></p><p>namespace java thriftservice Hello{  string helloString(1:string para)}</p><p></p><p>执行thrift -gen java test.thrift</p><p>会生成一个Hello.java文件</p><p></p><p>将Hello.java文件拷贝至IDE</p><p></p><p>server端代码:</p><p><span style="font-family: monospace; "><span style="white-space:pre"></span></span></p><pre name="code" class="java">package com.thrift.test.Async; 
 
import org.apache.thrift.TProcessorFactory; 
import org.apache.thrift.protocol.TCompactProtocol; 
import org.apache.thrift.server.THsHaServer; 
import org.apache.thrift.server.TNonblockingServer; 
import org.apache.thrift.server.TServer; 
import org.apache.thrift.transport.TFramedTransport; 
import org.apache.thrift.transport.TNonblockingServerSocket; 
import org.apache.thrift.transport.TTransportException; 
 
/**
 * @ * @version
 */ 
public class Server { 
    public final static int PORT = 8989; 
 
    @SuppressWarnings({ "rawtypes", "unchecked" }) 
    private void start() { 
        try { 
            TNonblockingServerSocket socket = new TNonblockingServerSocket(PORT); 
            final Hello.Processor processor = new Hello.Processor(new HelloImpl()); 
            THsHaServer.Args arg = new THsHaServer.Args(socket); 
            // 高效率的、密集的二进制编码格式进行数据传输 
            // 使用非阻塞方式,按块的大小进行传输,类似于 Java 中的 NIO 
            arg.protocolFactory(new TCompactProtocol.Factory()); 
            arg.transportFactory(new TFramedTransport.Factory()); 
            arg.processorFactory(new TProcessorFactory(processor)); 
            System.out.println("#服务启动-使用:非阻塞&高效二进制编码"); 
            //TServer server = new THsHaServer(arg); 
            TServer server = new TNonblockingServer(arg); 
            server.serve(); 
        } catch (TTransportException e) { 
            e.printStackTrace(); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
 
    public static void main(String args[]) { 
        Server srv = new Server(); 
        srv.start(); 
    } 

 


client端代码:
[java]
package com.thrift.test.Async; 
 
import java.io.IOException; 
 
import org.apache.thrift.TApplicationException; 
import org.apache.thrift.TException; 
import org.apache.thrift.async.TAsyncClientManager; 
import org.apache.thrift.protocol.TCompactProtocol; 
import org.apache.thrift.protocol.TProtocol; 
import org.apache.thrift.protocol.TProtocolFactory; 
import org.apache.thrift.transport.TFramedTransport; 
import org.apache.thrift.transport.TNonblockingSocket; 
import org.apache.thrift.transport.TNonblockingTransport; 
import org.apache.thrift.transport.TSocket; 
import org.apache.thrift.transport.TTransport; 
import org.apache.thrift.transport.TTransportException; 
 
/**
 * 
 */  www.zzzyk.com
public class Client { 
    public static final String address = "127.0.0.1"; 
    public static final int port = 8989; 
    public static final int clientTimeout = 30000; 
 
    public static void main_syn() { 
        TTransport transport = new TFramedTransport(new TSocket(address, port, clientTimeout)); 
        TProtocol protocol = new TCompactProtocol(transport); 
        Hello.Client client = new Hello.Client(protocol); 
 
        try { 
            transport.open(); 
            System.out.println(client.helloString("larry")); 
 
        } catch (TApplicationException e) { 
            System.out.println(e.getMessage() + " " + e.getType()); 
        } catch (TTransportException e) { 
            e.printStackTrace(); 
        } catch (TException e) { 
            e.printStackTrace(); 
        } 
        transport.close(); 
    } 
 
    public static void main_asy() throws Exception { 
        try { 
            TAsyncClientManager clientManager = new TAsyncClientManager(); 
            TNonblockingTransport transport = new TNonblockingSocket(address, port, clientTimeout); 
            TProtocolFactory protocol = new TCompactProtocol.Factory(); 
            Hello.AsyncClient asyncClient = new Hello.AsyncClient(protocol, clientManager, transport); 
            System.out.println("Client calls ....."); 
            MyCallback callBack = new MyCallback(); 
            asyncClient.helloString("larry", callBack); 
 
            while (true) { 
          &

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,