Search in sources :

Example 36 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 37 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 38 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)

Example 39 with TNonblockingServerSocket

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

the class ThriftServiceThread method openNonblockingTransport.

private TServerTransport openNonblockingTransport(String bindAddress, int port, int connectionTimeoutInMS) throws TTransportException {
    int maxRetry = 5;
    long retryIntervalMS = 5000;
    TTransportException lastExp = null;
    for (int i = 0; i < maxRetry; i++) {
        try {
            return new TNonblockingServerSocket(new InetSocketAddress(bindAddress, port), connectionTimeoutInMS);
        } catch (TTransportException e) {
            lastExp = e;
            try {
                Thread.sleep(retryIntervalMS);
            } catch (InterruptedException interruptedException) {
                Thread.currentThread().interrupt();
                break;
            }
        }
    }
    throw lastExp == null ? new TTransportException() : lastExp;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) TNonblockingServerSocket(org.apache.thrift.transport.TNonblockingServerSocket) TTransportException(org.apache.thrift.transport.TTransportException)

Example 40 with TNonblockingServerSocket

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

the class ThriftSource method getTThreadedSelectorServer.

private TServer getTThreadedSelectorServer() {
    if (isSslEnabled() || enableKerberos) {
        return null;
    }
    Class<?> serverClass;
    Class<?> argsClass;
    TServer.AbstractServerArgs args;
    try {
        serverClass = Class.forName("org.apache.thrift" + ".server.TThreadedSelectorServer");
        argsClass = Class.forName("org.apache.thrift" + ".server.TThreadedSelectorServer$Args");
        TServerTransport serverTransport = new TNonblockingServerSocket(new InetSocketAddress(bindAddress, port));
        ExecutorService sourceService;
        ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("Flume Thrift IPC Thread %d").build();
        if (maxThreads == 0) {
            sourceService = Executors.newCachedThreadPool(threadFactory);
        } else {
            sourceService = Executors.newFixedThreadPool(maxThreads, threadFactory);
        }
        args = (TNonblockingServer.AbstractNonblockingServerArgs) argsClass.getConstructor(TNonblockingServerTransport.class).newInstance(serverTransport);
        Method m = argsClass.getDeclaredMethod("executorService", ExecutorService.class);
        m.invoke(args, sourceService);
        populateServerParams(args);
        /*
       * Both THsHaServer and TThreadedSelectorServer allows us to pass in
       * the executor service to use - unfortunately the "executorService"
       * method does not exist in the parent abstract Args class,
       * so use reflection to pass the executor in.
       *
       */
        server = (TServer) serverClass.getConstructor(argsClass).newInstance(args);
    } catch (ClassNotFoundException e) {
        return null;
    } catch (Throwable ex) {
        throw new FlumeException("Cannot start Thrift Source.", ex);
    }
    return server;
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) TNonblockingServerTransport(org.apache.thrift.transport.TNonblockingServerTransport) TServer(org.apache.thrift.server.TServer) InetSocketAddress(java.net.InetSocketAddress) TNonblockingServer(org.apache.thrift.server.TNonblockingServer) Method(java.lang.reflect.Method) TServerTransport(org.apache.thrift.transport.TServerTransport) FlumeException(org.apache.flume.FlumeException) TNonblockingServerSocket(org.apache.thrift.transport.TNonblockingServerSocket) ExecutorService(java.util.concurrent.ExecutorService) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder)

Aggregations

TNonblockingServerSocket (org.apache.thrift.transport.TNonblockingServerSocket)40 TThreadedSelectorServer (org.apache.thrift.server.TThreadedSelectorServer)17 TServer (org.apache.thrift.server.TServer)16 TProcessor (org.apache.thrift.TProcessor)14 THsHaServer (org.apache.thrift.server.THsHaServer)14 InetSocketAddress (java.net.InetSocketAddress)13 TNonblockingServerTransport (org.apache.thrift.transport.TNonblockingServerTransport)13 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)11 TFramedTransport (org.apache.thrift.transport.TFramedTransport)11 TCompactProtocol (org.apache.thrift.protocol.TCompactProtocol)10 TNonblockingServer (org.apache.thrift.server.TNonblockingServer)9 ExecutorService (java.util.concurrent.ExecutorService)7 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)7 TTransportException (org.apache.thrift.transport.TTransportException)6 TProcessorFactory (org.apache.thrift.TProcessorFactory)5 TProtocolFactory (org.apache.thrift.protocol.TProtocolFactory)5 TException (org.apache.thrift.TException)4 TServerSocket (org.apache.thrift.transport.TServerSocket)4 TTransportFactory (org.apache.thrift.transport.TTransportFactory)4 Constructor (java.lang.reflect.Constructor)3