Example usage for org.apache.thrift.server TThreadPoolServer TThreadPoolServer

List of usage examples for org.apache.thrift.server TThreadPoolServer TThreadPoolServer

Introduction

In this page you can find the example usage for org.apache.thrift.server TThreadPoolServer TThreadPoolServer.

Prototype

public TThreadPoolServer(Args args) 

Source Link

Usage

From source file:ezbake.thrift.ThriftUtils.java

License:Apache License

private static TServer startThreadedPoolServer(final TServerTransport transport, final TProcessor processor,
        Properties properties) throws Exception {

    TThreadPoolServer.Args serverArgs;/*from   ww  w.j  ava  2s  .c om*/
    if (properties == null) {
        serverArgs = new TThreadPoolServer.Args(transport).processor(processor);
    } else {
        serverArgs = (TThreadPoolServer.Args) ThriftUtils.getServerArgs(transport, properties)
                .processor(processor);
    }

    final TServer server = new TThreadPoolServer(serverArgs);
    new Thread(new Runnable() {
        @Override
        public void run() {
            server.serve();
        }
    }).start();
    return server;
}

From source file:ezbake.thriftrunner.EZBakeBaseThriftServiceRunner.java

License:Apache License

private void createServer() throws Exception {
    final ThriftConfigurationHelper tc = new ThriftConfigurationHelper(ezProperties);
    final boolean useTFramedTransport = ezProperties.getBoolean("tframe.transport", false);

    final TProcessor processor = service.getThriftProcessor();

    switch (tc.getServerMode()) {
    case Simple: {
        final TServer.Args serverArgs = (TServer.Args) getServerArgs(getTransport(tc.useSSL()), ezProperties)
                .processor(processor);//from  w  ww.  jav a2  s .  co m

        if (useTFramedTransport) {
            serverArgs.transportFactory(new TFramedTransport.Factory());
        }

        this.server = new TSimpleServer(serverArgs);
        break;
    }
    case ThreadedPool: {
        final TThreadPoolServer.Args serverArgs = (TThreadPoolServer.Args) getServerArgs(
                getTransport(tc.useSSL()), ezProperties).processor(processor);

        if (useTFramedTransport) {
            serverArgs.transportFactory(new TFramedTransport.Factory());
        }

        this.server = new TThreadPoolServer(serverArgs);
        break;
    }
    case HsHa: {
        final InetSocketAddress socketAddress = new InetSocketAddress(privateHostInfo.getHostText(),
                privateHostInfo.getPort());
        final TNonblockingServerSocket socket = new TNonblockingServerSocket(socketAddress);
        final THsHaServer.Args serverArgs = new THsHaServer.Args(socket);
        serverArgs.processor(processor);
        serverArgs.inputProtocolFactory(new TCompactProtocol.Factory());
        serverArgs.outputProtocolFactory(new TCompactProtocol.Factory());
        this.server = new THsHaServer(serverArgs);
        break;
    }
    }

    logger.info("{} has started on {}", tc.getServerMode(), privateHostInfo);
    logger.info("Detected Processor was {}", processor.getClass());
}

From source file:filesystemserver.controller.ThriftServer.java

public static void StartServer(int serverPort, int[] serversPorts, int serverID) {
    //int[] allServers = serversPorts;
    try {//from  www  .  j  a  va2 s .  c om
        TServerSocket serverTransport = new TServerSocket(serverPort);
        FileSystem.Processor processor = new FileSystem.Processor(new ServerImpl(serversPorts, serverID));
        TServer server = new TThreadPoolServer(
                new TThreadPoolServer.Args(serverTransport).processor(processor));
        System.out.println("Starting server on port " + serverPort + " ...");
        server.serve();
    } catch (TTransportException e) {
        e.printStackTrace();
        //return false;
    }

}

From source file:graphservice.run.GraphServer.java

public static void main(String[] args) {

    try {/*from w  w w .  j  a  va  2s. co  m*/

        TServerTransport serverTransport = new TServerSocket(9090);
        ServerHandler handler = new ServerHandler();
        Graph.Processor processor = new Graph.Processor(handler);

        handler.createVertice(1, 1, 1.0, "1");
        handler.createVertice(2, 2, 2.0, "2");
        handler.createVertice(3, 3, 3.0, "3");

        handler.createAresta(1, 2, 1.2, false, "1.2");
        handler.createAresta(2, 3, 2.3, true, "2.3 d");
        handler.createAresta(3, 1, 3.1, false, "3.1");

        TServer server = new TThreadPoolServer(
                new TThreadPoolServer.Args(serverTransport).processor(processor));
        //TServer server = new TSimpleServer(new Args(serverTransport).processor(processor));

        System.out.println("Starting the simple server...");

        server.serve();
    } catch (Exception x) {
        x.printStackTrace();
    }
}

From source file:helloworldservice.HelloWorldServerApp.java

public static void main(String[] args) {
    try {/*from www  . ja v a  2 s. co m*/
        HelloWorldServer hwServer = new HelloWorldServer();
        HelloWorldService.Processor processor = new HelloWorldService.Processor(hwServer);
        TServerTransport serverTransport = new TServerSocket(9091);
        TServer server = new TThreadPoolServer(
                new TThreadPoolServer.Args(serverTransport).processor(processor));
        System.out.println("Starting Java server");
        server.serve();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:if4031.FSServer.java

public static void simple(FSService.Processor processor) {
    try {/*from  ww  w. j  av  a 2s  .com*/
        TServerTransport serverTransport = new TServerSocket(9090);
        TServer server;
        server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));
        System.out.println("Starting the simple server...");
        server.serve();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:if4031.Server.java

public static void simple(ServerService.Processor processor) {
    try {// w w w  .jav a 2  s  . c o m
        TServerTransport serverTransport = new TServerSocket(9090);
        //        TServer server = new TSimpleServer(new Args(serverTransport).processor(processor));
        TServer server = new TThreadPoolServer(
                new TThreadPoolServer.Args(serverTransport).processor(processor));
        System.out.println("Starting the simple server...");
        server.serve();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:io.opentracing.thrift.TracingTest.java

License:Apache License

private void startNewThreadPoolServer() throws Exception {
    TServerTransport transport = new TServerSocket(port);
    TProtocolFactory protocolFactory = new TBinaryProtocol.Factory();
    //TTransportFactory transportFactory = new TFramedTransport.Factory();

    CustomHandler customHandler = new CustomHandler();
    final TProcessor customProcessor = new CustomService.Processor<CustomService.Iface>(customHandler);

    TThreadPoolServer.Args args = new TThreadPoolServer.Args(transport)
            .processorFactory(new TProcessorFactory(new SpanProcessor(customProcessor)))
            .protocolFactory(protocolFactory)
            //.transportFactory(transportFactory)
            .minWorkerThreads(5)//  ww  w.  j av  a 2s.  com
            //.executorService(Executors.newCachedThreadPool())
            .maxWorkerThreads(10);

    server = new TThreadPoolServer(args);

    new Thread(new Runnable() {
        @Override
        public void run() {
            server.serve();
        }
    }).start();
}

From source file:io.warp10.continuum.geo.GeoDirectory.java

License:Apache License

private void startThrift(Properties properties) {
    ////from   w ww . j  a  va 2s .  co m
    // Start the Thrift Service
    //

    GeoDirectoryService.Processor processor = new GeoDirectoryService.Processor(this);

    ServiceInstance<Map> instance = null;

    ServiceDiscovery<Map> sd = ServiceDiscoveryBuilder.builder(Map.class)
            .basePath(properties.getProperty(Configuration.GEODIR_ZK_SERVICE_ZNODE)).client(this.serviceCurator)
            .build();

    try {
        int port = Integer.parseInt(properties.getProperty(Configuration.GEODIR_THRIFT_PORT));
        String host = properties.getProperty(Configuration.GEODIR_THRIFT_HOST);
        InetSocketAddress bindAddress = new InetSocketAddress(host, port);
        TServerTransport transport = new TServerSocket(bindAddress);
        TThreadPoolServer.Args args = new TThreadPoolServer.Args(transport);
        args.processor(processor);

        int maxThreads = Integer.parseInt(properties.getProperty(Configuration.GEODIR_THRIFT_MAXTHREADS));
        int maxThriftFrameLength = Integer
                .parseInt(properties.getProperty(Configuration.GEODIR_THRIFT_MAXFRAMELEN));

        args.maxWorkerThreads(maxThreads);
        args.minWorkerThreads(Math.max(1, maxThreads >> 2));

        if (0 != maxThriftFrameLength) {
            args.inputTransportFactory(new io.warp10.thrift.TFramedTransport.Factory(maxThriftFrameLength));
            args.outputTransportFactory(new io.warp10.thrift.TFramedTransport.Factory(maxThriftFrameLength));
        } else {
            args.inputTransportFactory(new io.warp10.thrift.TFramedTransport.Factory());
            args.outputTransportFactory(new io.warp10.thrift.TFramedTransport.Factory());
        }

        args.inputProtocolFactory(new TCompactProtocol.Factory());
        args.outputProtocolFactory(new TCompactProtocol.Factory());
        TServer server = new TThreadPoolServer(args);

        ServiceInstanceBuilder<Map> builder = ServiceInstance.builder();
        builder.port(((TServerSocket) transport).getServerSocket().getLocalPort());
        builder.address(((TServerSocket) transport).getServerSocket().getInetAddress().getHostAddress());
        builder.id(UUID.randomUUID().toString());
        builder.name(GEODIR_SERVICE);
        builder.serviceType(ServiceType.DYNAMIC);
        Map<String, String> payload = new HashMap<String, String>();
        payload.put(INSTANCE_PAYLOAD_MODULUS, Long.toString(this.modulus));
        payload.put(INSTANCE_PAYLOAD_REMAINDER, Long.toString(this.remainder));
        payload.put("thrift.protocol", "org.apache.thrift.protocol.TCompactProtocol");
        payload.put("thrift.transport", "org.apache.thrift.transport.TFramedTransport");
        if (0 != maxThriftFrameLength) {
            payload.put(INSTANCE_PAYLOAD_THRIFT_MAXFRAMELEN, Integer.toString(maxThriftFrameLength));
        }
        payload.put(INSTANCE_PAYLOAD_GEODIR, this.name);

        builder.payload(payload);

        instance = builder.build();

        sd.start();
        sd.registerService(instance);

        server.serve();
    } catch (TTransportException tte) {
        LOG.error("Thrift transport error", tte);
    } catch (Exception e) {
        LOG.error("Caught exception while starting Thrift service", e);
    } finally {
        if (null != instance) {
            try {
                sd.unregisterService(instance);
            } catch (Exception e) {
            }
        }
    }
}

From source file:io.warp10.continuum.store.Directory.java

License:Apache License

@Override
public void run() {

    ///*from ww w .  j av a 2  s  .c  om*/
    // Wait until cache has been populated
    //

    while (!this.cachePopulated.get()) {
        Sensision.set(SensisionConstants.SENSISION_CLASS_CONTINUUM_DIRECTORY_JVM_FREEMEMORY,
                Sensision.EMPTY_LABELS, Runtime.getRuntime().freeMemory());
        LockSupport.parkNanos(1000000000L);
    }

    //
    // Let's call GC once after populating so we take the trash out.
    //

    LOG.info("Triggering a GC to clean up after initial loading.");
    long nano = System.nanoTime();
    Runtime.getRuntime().gc();
    nano = System.nanoTime() - nano;
    LOG.info("GC performed in " + (nano / 1000000.0D) + " ms.");

    this.fullyInitialized.set(true);

    Sensision.set(SensisionConstants.SENSISION_CLASS_CONTINUUM_DIRECTORY_JVM_FREEMEMORY, Sensision.EMPTY_LABELS,
            Runtime.getRuntime().freeMemory());

    //
    // Start the Thrift Service
    //

    DirectoryService.Processor processor = new DirectoryService.Processor(this);

    ServiceInstance<Map> instance = null;

    try {
        InetSocketAddress bindAddress = new InetSocketAddress(this.host, this.port);
        TServerTransport transport = new TServerSocket(bindAddress);
        TThreadPoolServer.Args args = new TThreadPoolServer.Args(transport);
        args.processor(processor);
        //
        // FIXME(dmn): Set the min/max threads in the config file ?
        args.maxWorkerThreads(this.serviceNThreads);
        args.minWorkerThreads(this.serviceNThreads);
        if (0 != maxThriftFrameLength) {
            args.inputTransportFactory(new io.warp10.thrift.TFramedTransport.Factory(maxThriftFrameLength));
            args.outputTransportFactory(new io.warp10.thrift.TFramedTransport.Factory(maxThriftFrameLength));
        } else {
            args.inputTransportFactory(new io.warp10.thrift.TFramedTransport.Factory());
            args.outputTransportFactory(new io.warp10.thrift.TFramedTransport.Factory());
        }
        args.inputProtocolFactory(new TCompactProtocol.Factory());
        args.outputProtocolFactory(new TCompactProtocol.Factory());
        TServer server = new TThreadPoolServer(args);

        //
        // TODO(hbs): Check that the number of registered services does not go over the licensed number
        //

        ServiceInstanceBuilder<Map> builder = ServiceInstance.builder();
        builder.port(((TServerSocket) transport).getServerSocket().getLocalPort());
        builder.address(((TServerSocket) transport).getServerSocket().getInetAddress().getHostAddress());
        builder.id(UUID.randomUUID().toString());
        builder.name(DIRECTORY_SERVICE);
        builder.serviceType(ServiceType.DYNAMIC);
        Map<String, String> payload = new HashMap<String, String>();

        payload.put(PAYLOAD_MODULUS_KEY, Integer.toString(modulus));
        payload.put(PAYLOAD_REMAINDER_KEY, Integer.toString(remainder));
        payload.put(PAYLOAD_THRIFT_PROTOCOL_KEY, "org.apache.thrift.protocol.TCompactProtocol");
        payload.put(PAYLOAD_THRIFT_TRANSPORT_KEY, "org.apache.thrift.transport.TFramedTransport");
        payload.put(PAYLOAD_STREAMING_PORT_KEY, Integer.toString(this.streamingport));
        if (0 != maxThriftFrameLength) {
            payload.put(PAYLOAD_THRIFT_MAXFRAMELEN_KEY, Integer.toString(maxThriftFrameLength));
        }
        builder.payload(payload);

        instance = builder.build();

        if (this.register) {
            sd.start();
            sd.registerService(instance);
        }

        server.serve();

    } catch (TTransportException tte) {
        LOG.error("", tte);
    } catch (Exception e) {
        LOG.error("", e);
    } finally {
        if (null != instance) {
            try {
                sd.unregisterService(instance);
            } catch (Exception e) {
            }
        }
    }
}