Search in sources :

Example 31 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 32 with TNonblockingServerSocket

use of org.apache.thrift.transport.TNonblockingServerSocket in project mlib by myshzzx.

the class AsyncTest1 method test2.

@Test
public void test2() throws Exception {
    TNonblockingServerSocket serverTransport = new TNonblockingServerSocket(new InetSocketAddress("l", 19000), 0);
    TServer server = new THsHaServer(new THsHaServer.Args(serverTransport).processor(new TService1.AsyncProcessor<TService1.AsyncIface>(new Service2Impl())).protocolFactory(new TCompactProtocol.Factory()));
    new Thread() {

        @Override
        public void run() {
            server.serve();
        }
    }.start();
    Thread.sleep(1000);
    TNonblockingSocket transport = new TNonblockingSocket("l", 19000, 5000);
    TService1.AsyncIface client = new TService1.AsyncClient(new TCompactProtocol.Factory(), new TAsyncClientManager(), transport);
    byte[] b = { 1, 2, 3 };
    AsyncMethodCallback<TService1.AsyncClient.getStr_call> h = new AsyncMethodCallback<TService1.AsyncClient.getStr_call>() {

        @Override
        public void onComplete(TService1.AsyncClient.getStr_call response) {
            try {
                System.out.println(response.getResult());
            } catch (TException e) {
                log.error("async get rsp fail.", e);
            }
        }

        @Override
        public void onError(Exception e) {
            log.error("async call fail.", e);
        }
    };
    for (int i = 0; i < 2; i++) {
        client.getStr("mysh", ByteBuffer.wrap(b), h);
    }
    Thread.sleep(10000000);
}
Also used : TException(org.apache.thrift.TException) TNonblockingSocket(org.apache.thrift.transport.TNonblockingSocket) TServer(org.apache.thrift.server.TServer) InetSocketAddress(java.net.InetSocketAddress) LoggerFactory(org.slf4j.LoggerFactory) TCompactProtocol(org.apache.thrift.protocol.TCompactProtocol) TAsyncClientManager(org.apache.thrift.async.TAsyncClientManager) THsHaServer(org.apache.thrift.server.THsHaServer) AsyncMethodCallback(org.apache.thrift.async.AsyncMethodCallback) TException(org.apache.thrift.TException) TNonblockingServerSocket(org.apache.thrift.transport.TNonblockingServerSocket) Test(org.junit.jupiter.api.Test)

Example 33 with TNonblockingServerSocket

use of org.apache.thrift.transport.TNonblockingServerSocket in project providence by morimekta.

the class NonblockingSocketClientHandlerTest method setUpServer.

@Before
public void setUpServer() throws Exception {
    setDefaultPollDelay(10, TimeUnit.MILLISECONDS);
    serializer = new BinarySerializer();
    TProtocolFactory factory = new TBinaryProtocol.Factory();
    impl = Mockito.mock(Iface.class);
    TNonblockingServerTransport transport = new TNonblockingServerSocket(0);
    server = new TNonblockingServer(new TNonblockingServer.Args(transport).protocolFactory(factory).processor(new Processor<>(impl)));
    port = ((TNonblockingServerSocket) transport).getPort();
    executor = Executors.newSingleThreadExecutor();
    executor.submit(server::serve);
    address = new InetSocketAddress("localhost", port);
}
Also used : TProtocolFactory(org.apache.thrift.protocol.TProtocolFactory) Iface(net.morimekta.test.thrift.thrift.service.MyService.Iface) TNonblockingServerTransport(org.apache.thrift.transport.TNonblockingServerTransport) InetSocketAddress(java.net.InetSocketAddress) TNonblockingServerSocket(org.apache.thrift.transport.TNonblockingServerSocket) TProtocolFactory(org.apache.thrift.protocol.TProtocolFactory) TNonblockingServer(org.apache.thrift.server.TNonblockingServer) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) Before(org.junit.Before)

Example 34 with TNonblockingServerSocket

use of org.apache.thrift.transport.TNonblockingServerSocket in project dubbo by apache.

the class ThriftProtocol method getTServer.

private <T> TServer getTServer(T impl, Class<T> type, URL url) {
    TThreadedSelectorServer.Args tArgs = null;
    String typeName = type.getName();
    TServer tserver;
    if (typeName.endsWith(THRIFT_IFACE)) {
        String processorClsName = typeName.substring(0, typeName.indexOf(THRIFT_IFACE)) + THRIFT_PROCESSOR;
        try {
            Class<?> clazz = Class.forName(processorClsName);
            Constructor constructor = clazz.getConstructor(type);
            TProcessor tprocessor = (TProcessor) constructor.newInstance(impl);
            processor.registerProcessor(typeName, tprocessor);
            tserver = SERVER_MAP.get(url.getAddress());
            if (tserver == null) {
                /**
                 *Solve the problem of only 50 of the default number of concurrent connections
                 */
                TNonblockingServerSocket.NonblockingAbstractServerSocketArgs args = new TNonblockingServerSocket.NonblockingAbstractServerSocketArgs();
                /**
                 *1000 connections
                 */
                args.backlog(1000);
                String bindIp = url.getParameter(Constants.BIND_IP_KEY, url.getHost());
                if (url.getParameter(ANYHOST_KEY, false)) {
                    bindIp = ANYHOST_VALUE;
                }
                int bindPort = url.getParameter(Constants.BIND_PORT_KEY, url.getPort());
                args.bindAddr(new InetSocketAddress(bindIp, bindPort));
                /**
                 *timeout: 10s
                 */
                args.clientTimeout(10000);
                TNonblockingServerSocket transport = new TNonblockingServerSocket(args);
                tArgs = new TThreadedSelectorServer.Args(transport);
                tArgs.workerThreads(200);
                tArgs.selectorThreads(4);
                tArgs.acceptQueueSizePerThread(256);
                tArgs.processor(processor);
                tArgs.transportFactory(new TFramedTransport.Factory());
                tArgs.protocolFactory(new TCompactProtocol.Factory());
            } else {
                // if server is starting, return and do nothing here
                return null;
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new RpcException("Fail to create nativethrift server(" + url + ") : " + e.getMessage(), e);
        }
    }
    if (tArgs == null) {
        logger.error("Fail to create native thrift server(" + url + ") due to null args");
        throw new RpcException("Fail to create nativethrift server(" + url + ") due to null args");
    }
    tserver = new TThreadedSelectorServer(tArgs);
    return tserver;
}
Also used : TServer(org.apache.thrift.server.TServer) Constructor(java.lang.reflect.Constructor) InetSocketAddress(java.net.InetSocketAddress) TThreadedSelectorServer(org.apache.thrift.server.TThreadedSelectorServer) TCompactProtocol(org.apache.thrift.protocol.TCompactProtocol) TException(org.apache.thrift.TException) RpcException(org.apache.dubbo.rpc.RpcException) TProcessor(org.apache.thrift.TProcessor) TFramedTransport(org.apache.thrift.transport.TFramedTransport) TNonblockingServerSocket(org.apache.thrift.transport.TNonblockingServerSocket) RpcException(org.apache.dubbo.rpc.RpcException)

Example 35 with TNonblockingServerSocket

use of org.apache.thrift.transport.TNonblockingServerSocket in project EFAK by smartloli.

the class WorkNodeServer method start.

private void start(int port) throws TTransportException {
    TNonblockingServerSocket socket = new TNonblockingServerSocket(port);
    final WorkNodeService.Processor processor = new WorkNodeService.Processor(new WorkNodeServiceHandler());
    THsHaServer.Args arg = new THsHaServer.Args(socket);
    /**
     * Binary coded format efficient, intensive data transmission, The use
     * of non blocking mode of transmission, according to the size of the
     * block, similar to the Java of NIO.
     */
    arg.protocolFactory(new TCompactProtocol.Factory());
    arg.transportFactory(new TFramedTransport.Factory());
    arg.processorFactory(new TProcessorFactory(processor));
    TServer server = new THsHaServer(arg);
    server.serve();
}
Also used : TServer(org.apache.thrift.server.TServer) WorkNodeService(org.smartloli.kafka.eagle.core.task.rpc.WorkNodeService) WorkNodeServiceHandler(org.smartloli.kafka.eagle.core.task.rpc.handler.WorkNodeServiceHandler) TCompactProtocol(org.apache.thrift.protocol.TCompactProtocol) TProcessorFactory(org.apache.thrift.TProcessorFactory) THsHaServer(org.apache.thrift.server.THsHaServer) TFramedTransport(org.apache.thrift.transport.layered.TFramedTransport) TNonblockingServerSocket(org.apache.thrift.transport.TNonblockingServerSocket)

Aggregations

TNonblockingServerSocket (org.apache.thrift.transport.TNonblockingServerSocket)36 TThreadedSelectorServer (org.apache.thrift.server.TThreadedSelectorServer)15 THsHaServer (org.apache.thrift.server.THsHaServer)14 TServer (org.apache.thrift.server.TServer)14 TProcessor (org.apache.thrift.TProcessor)13 TNonblockingServerTransport (org.apache.thrift.transport.TNonblockingServerTransport)12 InetSocketAddress (java.net.InetSocketAddress)11 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)11 TFramedTransport (org.apache.thrift.transport.TFramedTransport)10 TCompactProtocol (org.apache.thrift.protocol.TCompactProtocol)8 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)7 TNonblockingServer (org.apache.thrift.server.TNonblockingServer)7 ExecutorService (java.util.concurrent.ExecutorService)6 TTransportException (org.apache.thrift.transport.TTransportException)6 TProcessorFactory (org.apache.thrift.TProcessorFactory)5 TProtocolFactory (org.apache.thrift.protocol.TProtocolFactory)5 TServerSocket (org.apache.thrift.transport.TServerSocket)4 TTransportFactory (org.apache.thrift.transport.TTransportFactory)4 Constructor (java.lang.reflect.Constructor)2 InetAddress (java.net.InetAddress)2