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:org.hawk.service.servlet.Activator.java

License:Open Source License

@Override
public void start(BundleContext bundleContext) throws Exception {
    Activator.context = bundleContext;
    HManager.getInstance();/*from  w  w  w . j  av  a  2 s  . co m*/

    String artemisHost = System.getProperty(ARTEMIS_HOST_PROPERTY);
    if (artemisHost == null) {
        artemisHost = TransportConstants.DEFAULT_HOST;
    }

    String sArtemisPort = System.getProperty(ARTEMIS_PORT_PROPERTY);
    int artemisPort;
    if (sArtemisPort == null) {
        artemisPort = TransportConstants.DEFAULT_PORT;
    } else {
        artemisPort = Integer.valueOf(sArtemisPort);
    }

    artemis = new Server(artemisHost, artemisPort);
    String sListenAll = System.getProperty(ARTEMIS_LISTENALL_PROPERTY);
    if (sListenAll != null) {
        artemis.setListenOnAllInterfaces(Boolean.valueOf(sListenAll));
    }
    String sSSLEnabled = System.getProperty(ARTEMIS_SSL_PROPERTY);
    if (sSSLEnabled != null) {
        artemis.setSSLEnabled(Boolean.valueOf(sSSLEnabled));
    }
    try {
        artemis.start();
    } catch (Exception e) {
        throw new ServletException(e);
    }

    final String sTCPPort = System.getProperty(TCP_PORT_PROPERTY);
    if (sTCPPort != null) {
        final String sThriftProtocol = System.getProperty(TCP_TPROTOCOL_PROPERTY);
        final ThriftProtocol thriftProtocol = (sThriftProtocol != null)
                ? ThriftProtocol.valueOf(sThriftProtocol)
                : ThriftProtocol.TUPLE;

        final TServerSocket tcpServerSocket = new TServerSocket(Integer.valueOf(sTCPPort));
        final HawkThriftIface hawkIface = new HawkThriftIface(ThriftProtocol.TUPLE, null, artemis);
        final Processor<Iface> hawkTCPProcessor = new Hawk.Processor<Hawk.Iface>(hawkIface);
        final Args tcpServerArgs = new TThreadPoolServer.Args(tcpServerSocket).maxWorkerThreads(10_000)
                .protocolFactory(new TProtocolFactory() {
                    private static final long serialVersionUID = 1L;

                    @Override
                    public TProtocol getProtocol(TTransport arg0) {
                        return thriftProtocol.getProtocolFactory().getProtocol(new TZlibTransport(arg0));
                    }
                }).processor(hawkTCPProcessor);

        tcpServer = new TThreadPoolServer(tcpServerArgs);
        new Thread(new Runnable() {
            @Override
            public void run() {
                final Bundle bundle = context.getBundle();
                Platform.getLog(bundle).log(
                        new Status(IStatus.INFO, bundle.getSymbolicName(), "Starting Hawk TCP server on port "
                                + sTCPPort + " with Thrift protocol " + thriftProtocol.name()));
                tcpServer.serve();
            }
        }).start();
    }
}

From source file:org.igo.letsgo.rpc.apache.thrift.city.jpa.h2.server.ThriftCityServer.java

License:Open Source License

/**
 *
 * @param processor//from ww  w.j  a  v a 2 s  . c  om
 */
public static void myserver(CityService.Processor processor) {
    try {
        TServerTransport serverTransport = new TServerSocket(8080);
        TServer server = new TThreadPoolServer(
                new TThreadPoolServer.Args(serverTransport).processor(processor));
        server.serve();
    } catch (TTransportException ex) {
        Logger.getLogger(ThriftCityServer.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.jcronjob.agent.AgentBootstrap.java

License:Apache License

@Override
public void start() throws Exception {
    try {/*from  w ww.ja  v a  2  s  .  co  m*/
        TServerSocket serverTransport = new TServerSocket(port);
        AgentProcessor agentProcessor = new AgentProcessor(password, port);
        CronJob.Processor processor = new CronJob.Processor(agentProcessor);
        TBinaryProtocol.Factory protFactory = new TBinaryProtocol.Factory(true, true);
        TThreadPoolServer.Args arg = new TThreadPoolServer.Args(serverTransport);
        arg.protocolFactory(protFactory);
        arg.processor(processor);
        this.server = new TThreadPoolServer(arg);
        logger.info("[cronjob]agent started @ port:{},pid:{}", port, getPid());
        this.server.serve();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.kaaproject.kaa.it.thrift.cli.CliThriftIT.java

License:Apache License

/**
 * Before test.//from  ww w  . j a  v a2  s. co m
 *
 * @throws Exception the exception
 */
@Before
public void beforeTest() throws Exception {
    if (!thriftServerStarted) {
        CliThriftService.Processor<CliThriftService.Iface> processor = new CliThriftService.Processor<CliThriftService.Iface>(
                new TestCliThriftService(THRIFT_SERVER_SHORT_NAME));
        TServerTransport serverTransport = new TServerSocket(new InetSocketAddress(HOST, PORT));
        server = new TThreadPoolServer(new Args(serverTransport).processor(processor));
        thriftServerThread = new Thread(new Runnable() {
            @Override
            public void run() {
                logger.info("Thrift Server started.");
                server.serve();
                logger.info("Thrift Server stopped.");
            }
        });

        thriftServerThread.start();

        Thread.sleep(100);

        thriftServerStarted = true;
    }
    cliSession = new CliSessionState();
    cliSession.in = System.in;

    systemOut = new ByteArrayOutputStream();
    PrintStream out = new PrintStream(systemOut, true, "UTF-8");
    System.setOut(out);

    systemErr = new ByteArrayOutputStream();
    PrintStream err = new PrintStream(systemErr, true, "UTF-8");
    System.setErr(err);

    cliSession.out = System.out;
    cliSession.err = System.err;

    CliSessionState.start(cliSession);
}

From source file:org.kaaproject.kaa.server.bootstrap.service.initialization.DefaultBootstrapInitializationService.java

License:Apache License

/**
 * Start thrift.//from   ww w .jav a 2 s.c  o  m
 *
 * @param thriftStartupLatch
 *            CountDownLatch
 * @param thriftShutdownLatch
 *            CountDownLatch
 */
private void startThrift(final CountDownLatch thriftStartupLatch, final CountDownLatch thriftShutdownLatch) {
    Runnable thriftRunnable = new Runnable() {

        @Override
        public void run() {

            LOG.info("Initializing Thrift service for Bootstrap server at {}:{}...", thriftHost, thriftPort);

            try {
                BootstrapThriftService.Processor<BootstrapThriftService.Iface> processor = new BootstrapThriftService.Processor<BootstrapThriftService.Iface>(
                        bootstrapThriftService);

                TServerTransport serverTransport = new TServerSocket(
                        new InetSocketAddress(thriftHost, thriftPort));

                TThreadPoolServer.Args args = new Args(serverTransport).processor(processor);
                args.stopTimeoutVal = 3;
                args.stopTimeoutUnit = TimeUnit.SECONDS;
                server = new TThreadPoolServer(args);

                LOG.info("Bootstrap Server Started");

                thriftStartupLatch.countDown();

                server.serve();

                LOG.info("Bootstrap Server Stopped");

                thriftShutdownLatch.countDown();
            } catch (TTransportException e) {
                LOG.error("TTransportException", e);
            } finally {
                server = null;

                if (thriftStartupLatch.getCount() > 0) {
                    thriftStartupLatch.countDown();
                }
                if (thriftShutdownLatch.getCount() > 0) {
                    LOG.info("Thrift Bootstrap Server Stopped.");
                    thriftShutdownLatch.countDown();
                }
            }
        }
    };

    new Thread(thriftRunnable).start();
}

From source file:org.kaaproject.kaa.server.control.service.ControlServiceImpl.java

License:Apache License

/**
 * Creates the server./*from  w w  w .  ja v  a  2s .com*/
 *
 * @param serverTransport
 *            the server transport
 * @param processor
 *            the processor
 * @return the t server
 */
public TServer createServer(TServerTransport serverTransport,
        ControlThriftService.Processor<ControlThriftService.Iface> processor) {
    TThreadPoolServer.Args args = new Args(serverTransport).processor(processor);
    args.stopTimeoutVal = 3;
    args.stopTimeoutUnit = TimeUnit.SECONDS;

    SynchronousQueue<Runnable> executorQueue = // NOSONAR
            new SynchronousQueue<Runnable>();
    executorService = new ThreadPoolExecutor(args.minWorkerThreads, args.maxWorkerThreads, 60, TimeUnit.SECONDS,
            executorQueue);
    args.executorService = executorService;
    return new TThreadPoolServer(args);
}

From source file:org.kaaproject.kaa.server.node.service.initialization.KaaNodeInitializationService.java

License:Apache License

/**
 * Creates the server.//from  w  w  w.  j  av  a2s  .  c o  m
 *
 * @param serverTransport
 *            the server transport
 * @param processor
 *            the processor
 * @return the t server
 */
public TServer createServer(TServerTransport serverTransport, TMultiplexedProcessor processor) {
    TThreadPoolServer.Args args = new Args(serverTransport).processor(processor);
    args.stopTimeoutVal = 3;
    args.stopTimeoutUnit = TimeUnit.SECONDS;

    SynchronousQueue<Runnable> executorQueue = // NOSONAR
            new SynchronousQueue<Runnable>();
    executorService = new ThreadPoolExecutor(args.minWorkerThreads, args.maxWorkerThreads, 60, TimeUnit.SECONDS,
            executorQueue);
    args.executorService = executorService;
    return new TThreadPoolServer(args);
}

From source file:org.kaaproject.kaa.server.operations.service.bootstrap.DefaultOperationsBootstrapService.java

License:Apache License

/**
 * Start thrift service./*from   w  w  w.j a  va 2s  .  c  o m*/
 *
 * @param thriftShutdownLatch
 * @param thriftStartupLatch
 */
private void startThrift(final CountDownLatch thriftStartupLatch, final CountDownLatch thriftShutdownLatch) {
    Runnable thriftRunnable = new Runnable() {

        @Override
        public void run() {
            LOG.info("Initializing Thrift Service for Operations Server....");
            LOG.info("thrift host: {}", getConfig().getThriftHost());
            LOG.info("thrift port: {}", getConfig().getThriftPort());

            try {
                OperationsThriftService.Processor<OperationsThriftService.Iface> processor = new OperationsThriftService.Processor<OperationsThriftService.Iface>(
                        operationsThriftService);
                TServerTransport serverTransport = new TServerSocket(
                        new InetSocketAddress(getConfig().getThriftHost(), getConfig().getThriftPort()));

                TThreadPoolServer.Args args = new Args(serverTransport).processor(processor);
                args.stopTimeoutVal = 3;
                args.stopTimeoutUnit = TimeUnit.SECONDS;
                server = new TThreadPoolServer(args);

                LOG.info("Thrift Operations Server Started.");

                thriftStartupLatch.countDown();

                server.serve();

                LOG.info("Thrift Operations Server Stopped.");

                thriftShutdownLatch.countDown();
            } catch (TTransportException e) {
                LOG.error("TTransportException", e);
            } finally {
                if (thriftStartupLatch.getCount() > 0) {
                    thriftStartupLatch.countDown();
                }
                if (thriftShutdownLatch.getCount() > 0) {
                    LOG.info("Thrift Operations Server Stopped.");
                    thriftShutdownLatch.countDown();
                }
            }
        }
    };

    new Thread(thriftRunnable).start();
}

From source file:org.ofbiz.service.ThriftContainer.java

License:Apache License

public void simple(o3erp.thrift.BaseService.Processor processor) {
    try {/*from   ww  w . ja v  a 2 s.  co  m*/
        TServerTransport serverTransport = new TServerSocket(port);
        TServer server = null;

        if (useThreadPool) /* multithreaded server */ {
            server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));
        } else {
            server = new TSimpleServer(new Args(serverTransport).processor(processor));
        }

        Debug.logInfo("Starting the simple server...", module);

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

From source file:org.ofbiz.service.ThriftContainer.java

License:Apache License

public void secure(BaseService.Processor processor) {
    try {//  w w w .  j  a  va  2  s  . c o  m
        /*
         * Use TSSLTransportParameters to setup the required SSL parameters. In this example
         * we are setting the keystore and the keystore password. Other things like algorithms,
         * cipher suites, client auth etc can be set. 
         */
        TSSLTransportParameters params = new TSSLTransportParameters();
        // The Keystore contains the private key
        params.setKeyStore(pathToKeyStore, "thrift", null, null);

        /*
         * Use any of the TSSLTransportFactory to get a server transport with the appropriate
         * SSL configuration. You can use the default settings if properties are set in the command line.
         * Ex: -Djavax.net.ssl.keyStore=.keystore and -Djavax.net.ssl.keyStorePassword=thrift
         * 
         * Note: You need not explicitly call open(). The underlying server socket is bound on return
         * from the factory class. 
         */
        TServerTransport serverTransport = TSSLTransportFactory.getServerSocket(port, 0, null, params);
        TServer server = null;

        if (useThreadPool) /* multithreaded server */ {
            server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));
        } else {
            server = new TSimpleServer(new Args(serverTransport).processor(processor));
        }

        Debug.logInfo("Starting the secure server...", module);
        server.serve();
    } catch (Exception e) {
        e.printStackTrace();
    }
}