Search in sources :

Example 16 with TNonblockingServerSocket

use of org.apache.thrift.transport.TNonblockingServerSocket in project commons by twitter.

the class ThriftServer method getServerSocketFor.

/**
   * Thrift doesn't provide access to socket it creates,
   * this is the only way to know what ephemeral port we bound to.
   * TODO:  Patch thrift to provide access so we don't have to do this.
   */
@VisibleForTesting
static ServerSocket getServerSocketFor(TNonblockingServerSocket thriftSocket) throws TTransportException {
    try {
        Field field = TNonblockingServerSocket.class.getDeclaredField("serverSocket_");
        field.setAccessible(true);
        return (ServerSocket) field.get(thriftSocket);
    } catch (NoSuchFieldException e) {
        throw new TTransportException("Couldn't get listening port", e);
    } catch (SecurityException e) {
        throw new TTransportException("Couldn't get listening port", e);
    } catch (IllegalAccessException e) {
        throw new TTransportException("Couldn't get listening port", e);
    }
}
Also used : Field(java.lang.reflect.Field) TTransportException(org.apache.thrift.transport.TTransportException) ServerSocket(java.net.ServerSocket) TNonblockingServerSocket(org.apache.thrift.transport.TNonblockingServerSocket) TServerSocket(org.apache.thrift.transport.TServerSocket) TMonitoredServerSocket(com.twitter.common.thrift.monitoring.TMonitoredServerSocket) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 17 with TNonblockingServerSocket

use of org.apache.thrift.transport.TNonblockingServerSocket in project lucida by claritylab.

the class QADaemon method main.

/** 
	 * Entry point for question-answer.
	 * @param args the argument list. Provide port numbers
	 * for both sirius and qa.
	 */
public static void main(String[] args) throws TTransportException, IOException, InterruptedException {
    TProcessor proc = new LucidaService.AsyncProcessor(new QAServiceHandler.AsyncQAServiceHandler());
    TNonblockingServerTransport transport = new TNonblockingServerSocket(8083);
    TThreadedSelectorServer.Args arguments = new TThreadedSelectorServer.Args(transport).processor(proc).protocolFactory(new TBinaryProtocol.Factory()).transportFactory(new TFramedTransport.Factory());
    final TThreadedSelectorServer server = new TThreadedSelectorServer(arguments);
    System.out.println("QA at port 8083");
    server.serve();
}
Also used : AsyncQAServiceHandler(lucida.handler.QAServiceHandler.AsyncQAServiceHandler) TNonblockingServerTransport(org.apache.thrift.transport.TNonblockingServerTransport) TProcessor(org.apache.thrift.TProcessor) TFramedTransport(org.apache.thrift.transport.TFramedTransport) TNonblockingServerSocket(org.apache.thrift.transport.TNonblockingServerSocket) TThreadedSelectorServer(org.apache.thrift.server.TThreadedSelectorServer) AsyncQAServiceHandler(lucida.handler.QAServiceHandler.AsyncQAServiceHandler)

Example 18 with TNonblockingServerSocket

use of org.apache.thrift.transport.TNonblockingServerSocket in project tech by ffyyhh995511.

the class ThriftDemo method nioServer.

/**
 * 基于非阻塞IO(NIO)的服务端
 */
public void nioServer() {
    try {
        // 传输通道 - 非阻塞方式
        TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(7911);
        // 异步IO,需要使用TFramedTransport,它将分块缓存读取。
        TTransportFactory transportFactory = new TFramedTransport.Factory();
        // 使用高密度二进制协议
        TProtocolFactory proFactory = new TCompactProtocol.Factory();
        // 设置处理器 HelloImpl
        TProcessor processor = new Hello.Processor(new HelloImpl());
        // 创建服务器
        Args tArgs = new Args(serverTransport);
        tArgs.transportFactory(transportFactory);
        tArgs.protocolFactory(proFactory);
        tArgs.processor(processor);
        TServer server = new TThreadedSelectorServer(tArgs);
        System.out.println("Start server on port 7911...");
        server.serve();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : TProtocolFactory(org.apache.thrift.protocol.TProtocolFactory) TNonblockingServerTransport(org.apache.thrift.transport.TNonblockingServerTransport) Args(org.apache.thrift.server.TThreadedSelectorServer.Args) TProcessor(org.apache.thrift.TProcessor) HelloImpl(org.tech.model.impl.HelloImpl) TProcessor(org.apache.thrift.TProcessor) TServer(org.apache.thrift.server.TServer) TNonblockingServerSocket(org.apache.thrift.transport.TNonblockingServerSocket) TThreadedSelectorServer(org.apache.thrift.server.TThreadedSelectorServer) TProtocolFactory(org.apache.thrift.protocol.TProtocolFactory) TTransportFactory(org.apache.thrift.transport.TTransportFactory) TTransportFactory(org.apache.thrift.transport.TTransportFactory)

Example 19 with TNonblockingServerSocket

use of org.apache.thrift.transport.TNonblockingServerSocket in project tech by ffyyhh995511.

the class Test2 method main.

public static void main(String[] args) {
    try {
        // 传输通道 - 非阻塞方式
        TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(7911);
        // 异步IO,需要使用TFramedTransport,它将分块缓存读取。
        TTransportFactory transportFactory = new TFramedTransport.Factory();
        // 使用高密度二进制协议
        TProtocolFactory proFactory = new TCompactProtocol.Factory();
        // 设置处理器 HelloImpl
        TProcessor processor = new Hello.Processor(new HelloImpl());
        // 创建服务器
        Args tArgs = new Args(serverTransport);
        tArgs.transportFactory(transportFactory);
        tArgs.protocolFactory(proFactory);
        tArgs.processor(processor);
        TServer server = new TThreadedSelectorServer(tArgs);
        System.out.println("Start server on port 7911...");
        server.serve();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : TProtocolFactory(org.apache.thrift.protocol.TProtocolFactory) TNonblockingServerTransport(org.apache.thrift.transport.TNonblockingServerTransport) Args(org.apache.thrift.server.TThreadedSelectorServer.Args) TProcessor(org.apache.thrift.TProcessor) HelloImpl(org.tech.model.impl.HelloImpl) TProcessor(org.apache.thrift.TProcessor) TServer(org.apache.thrift.server.TServer) TNonblockingServerSocket(org.apache.thrift.transport.TNonblockingServerSocket) TThreadedSelectorServer(org.apache.thrift.server.TThreadedSelectorServer) TTransportFactory(org.apache.thrift.transport.TTransportFactory) TProtocolFactory(org.apache.thrift.protocol.TProtocolFactory) TTransportFactory(org.apache.thrift.transport.TTransportFactory)

Aggregations

TNonblockingServerSocket (org.apache.thrift.transport.TNonblockingServerSocket)19 THsHaServer (org.apache.thrift.server.THsHaServer)9 TNonblockingServerTransport (org.apache.thrift.transport.TNonblockingServerTransport)9 TThreadedSelectorServer (org.apache.thrift.server.TThreadedSelectorServer)8 TProcessor (org.apache.thrift.TProcessor)6 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)6 ExecutorService (java.util.concurrent.ExecutorService)4 TServer (org.apache.thrift.server.TServer)4 TFramedTransport (org.apache.thrift.transport.TFramedTransport)4 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)3 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)3 TProtocolFactory (org.apache.thrift.protocol.TProtocolFactory)3 TNonblockingServer (org.apache.thrift.server.TNonblockingServer)3 TTransportFactory (org.apache.thrift.transport.TTransportFactory)3 TCompactProtocol (org.apache.thrift.protocol.TCompactProtocol)2 Args (org.apache.thrift.server.TThreadedSelectorServer.Args)2 TServerSocket (org.apache.thrift.transport.TServerSocket)2 TTransportException (org.apache.thrift.transport.TTransportException)2 HelloImpl (org.tech.model.impl.HelloImpl)2 DistributedRPC (backtype.storm.generated.DistributedRPC)1