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:StubServer.java

License:Apache License

public static void main(String argv[]) {
    try {/*from www.  j  a  va2 s .c o m*/
        int port = 9090;
        int numThreads = 32;
        if (argv.length != 1) {
            usage();
        }
        System.out.println(argv[0]);
        StubServer mapkeeper = new StubServer();
        TServer server = null;
        if (argv[0].equals("hsha")) {
            TNonblockingServerTransport trans = new TNonblockingServerSocket(port);
            THsHaServer.Args args = new THsHaServer.Args(trans);
            args.transportFactory(new TFramedTransport.Factory());
            args.protocolFactory(new TBinaryProtocol.Factory());
            args.processor(new MapKeeper.Processor(mapkeeper));
            args.workerThreads(numThreads);
            server = new THsHaServer(args);
        } else if (argv[0].equals("nonblocking")) {
            TNonblockingServerTransport trans = new TNonblockingServerSocket(port);
            TNonblockingServer.Args args = new TNonblockingServer.Args(trans);
            args.transportFactory(new TFramedTransport.Factory());
            args.protocolFactory(new TBinaryProtocol.Factory());
            args.processor(new MapKeeper.Processor(mapkeeper));
            server = new TNonblockingServer(args);
        } else if (argv[0].equals("threadpool")) {
            TServerTransport trans = new TServerSocket(port);
            TThreadPoolServer.Args args = new TThreadPoolServer.Args(trans);
            args.transportFactory(new TFramedTransport.Factory());
            args.protocolFactory(new TBinaryProtocol.Factory());
            args.processor(new MapKeeper.Processor(mapkeeper));
            server = new TThreadPoolServer(args);
        } else if (argv[0].equals("selector")) {
            TNonblockingServerTransport trans = new TNonblockingServerSocket(port);
            TThreadedSelectorServer.Args args = new TThreadedSelectorServer.Args(trans);
            args.transportFactory(new TFramedTransport.Factory());
            args.protocolFactory(new TBinaryProtocol.Factory());
            args.processor(new MapKeeper.Processor(mapkeeper));
            args.selectorThreads(4);
            args.workerThreads(numThreads);
            server = new TThreadedSelectorServer(args);
        } else {
            usage();
        }
        server.serve();
    } catch (Exception x) {
        System.out.println(x.toString() + " " + x.getMessage());
    }
}

From source file:FilesystemServer.java

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

From source file:alluxio.master.AlluxioJobMasterProcess.java

License:Apache License

protected void startServingRPCServer() {
    // set up multiplexed thrift processors
    TMultiplexedProcessor processor = new TMultiplexedProcessor();
    registerServices(processor, mJobMaster.getServices());
    // register meta services
    processor.registerProcessor(Constants.JOB_MASTER_CLIENT_SERVICE_NAME,
            new JobMasterClientService.Processor<>(new JobMasterClientServiceHandler(mJobMaster)));
    LOG.info("registered service " + Constants.JOB_MASTER_CLIENT_SERVICE_NAME);

    // Return a TTransportFactory based on the authentication type
    TTransportFactory transportFactory;//from w w w  . j  a  v  a  2s.  c om
    try {
        String serverName = NetworkAddressUtils.getConnectHost(ServiceType.JOB_MASTER_RPC);
        transportFactory = mTransportProvider.getServerTransportFactory(serverName);
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }

    try {
        if (mTServerSocket != null) {
            mTServerSocket.close();
        }
        mTServerSocket = ThriftUtils.createThriftServerSocket(mRpcBindAddress);
    } catch (TTransportException e) {
        throw new RuntimeException(e);
    }
    // create master thrift service with the multiplexed processor.
    Args args = new Args(mTServerSocket).maxWorkerThreads(mMaxWorkerThreads).minWorkerThreads(mMinWorkerThreads)
            .processor(processor).transportFactory(transportFactory)
            .protocolFactory(new TBinaryProtocol.Factory(true, true));
    if (Configuration.getBoolean(PropertyKey.TEST_MODE)) {
        args.stopTimeoutVal = 0;
    } else {
        args.stopTimeoutVal = Constants.THRIFT_STOP_TIMEOUT_SECONDS;
    }
    mMasterServiceServer = new TThreadPoolServer(args);

    // start thrift rpc server
    mIsServing = true;
    mMasterServiceServer.serve();
}

From source file:alluxio.master.AlluxioMaster.java

License:Apache License

protected void startServingRPCServer() {
    // set up multiplexed thrift processors
    TMultiplexedProcessor processor = new TMultiplexedProcessor();
    registerServices(processor, mBlockMaster.getServices());
    registerServices(processor, mFileSystemMaster.getServices());
    if (LineageUtils.isLineageEnabled()) {
        registerServices(processor, mLineageMaster.getServices());
    }//w ww .  j a va  2  s  .c o m
    // register additional masters for RPC service
    for (Master master : mAdditionalMasters) {
        registerServices(processor, master.getServices());
    }

    // Return a TTransportFactory based on the authentication type
    TTransportFactory transportFactory;
    try {
        transportFactory = mTransportProvider.getServerTransportFactory();
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }

    // create master thrift service with the multiplexed processor.
    Args args = new TThreadPoolServer.Args(mTServerSocket).maxWorkerThreads(mMaxWorkerThreads)
            .minWorkerThreads(mMinWorkerThreads).processor(processor).transportFactory(transportFactory)
            .protocolFactory(new TBinaryProtocol.Factory(true, true));
    if (Configuration.getBoolean(PropertyKey.TEST_MODE)) {
        args.stopTimeoutVal = 0;
    } else {
        args.stopTimeoutVal = Constants.THRIFT_STOP_TIMEOUT_SECONDS;
    }
    mMasterServiceServer = new TThreadPoolServer(args);

    // start thrift rpc server
    mIsServing = true;
    mStartTimeMs = System.currentTimeMillis();
    mMasterServiceServer.serve();
}

From source file:alluxio.master.AlluxioMasterProcess.java

License:Apache License

/**
 * Starts the Thrift RPC server. The AlluxioMaster registers the Services of registered
 * {@link Master}s and meta services to a multiplexed processor, then creates the master thrift
 * service with the multiplexed processor.
 *///from   ww w  .  j av  a 2s.  c om
protected void startServingRPCServer() {
    // set up multiplexed thrift processors
    TMultiplexedProcessor processor = new TMultiplexedProcessor();
    // register master services
    for (Master master : mRegistry.getServers()) {
        registerServices(processor, master.getServices());
    }
    // register meta services
    processor.registerProcessor(Constants.META_MASTER_SERVICE_NAME,
            new MetaMasterClientService.Processor<>(new MetaMasterClientServiceHandler(this)));

    // Return a TTransportFactory based on the authentication type
    TTransportFactory transportFactory;
    try {
        transportFactory = mTransportProvider.getServerTransportFactory();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    try {
        if (mTServerSocket != null) {
            mTServerSocket.close();
        }
        mTServerSocket = new TServerSocket(mRpcAddress,
                Configuration.getInt(PropertyKey.MASTER_CONNECTION_TIMEOUT_MS));
    } catch (TTransportException e) {
        throw new RuntimeException(e);
    }
    // create master thrift service with the multiplexed processor.
    Args args = new TThreadPoolServer.Args(mTServerSocket).maxWorkerThreads(mMaxWorkerThreads)
            .minWorkerThreads(mMinWorkerThreads).processor(processor).transportFactory(transportFactory)
            .protocolFactory(new TBinaryProtocol.Factory(true, true));
    if (Configuration.getBoolean(PropertyKey.TEST_MODE)) {
        args.stopTimeoutVal = 0;
    } else {
        args.stopTimeoutVal = Constants.THRIFT_STOP_TIMEOUT_SECONDS;
    }
    mThriftServer = new TThreadPoolServer(args);

    // start thrift rpc server
    mIsServing = true;
    mStartTimeMs = System.currentTimeMillis();
    mThriftServer.serve();
}

From source file:alluxio.security.authentication.AuthenticationUtilsTest.java

License:Apache License

private void startServerThread() throws Exception {
    // create args and use them to build a Thrift TServer
    TTransportFactory tTransportFactory = AuthenticationUtils.getServerTransportFactory(mConfiguration);

    mServer = new TThreadPoolServer(new TThreadPoolServer.Args(mServerTSocket).maxWorkerThreads(2)
            .minWorkerThreads(1).processor(null).transportFactory(tTransportFactory)
            .protocolFactory(new TBinaryProtocol.Factory(true, true)));

    // start the server in a new thread
    Thread serverThread = new Thread(new Runnable() {
        @Override//from  w  ww  .ja va  2s. c  o  m
        public void run() {
            mServer.serve();
        }
    });

    serverThread.start();

    // ensure server is running, and break if it does not start serving in 2 seconds.
    int count = 40;
    while (!mServer.isServing() && serverThread.isAlive()) {
        if (count <= 0) {
            throw new RuntimeException("TThreadPoolServer does not start serving");
        }
        Thread.sleep(50);
        count--;
    }
}

From source file:alluxio.security.authentication.TransportProviderTest.java

License:Apache License

private void startServerThread() throws Exception {
    // create args and use them to build a Thrift TServer
    TTransportFactory tTransportFactory = mTransportProvider.getServerTransportFactory();

    mServer = new TThreadPoolServer(new TThreadPoolServer.Args(mServerTSocket).maxWorkerThreads(2)
            .minWorkerThreads(1).processor(null).transportFactory(tTransportFactory)
            .protocolFactory(new TBinaryProtocol.Factory(true, true)));

    // start the server in a new thread
    Thread serverThread = new Thread(new Runnable() {
        @Override/* w ww. j  a  va2 s  .c  o  m*/
        public void run() {
            mServer.serve();
        }
    });

    serverThread.start();

    // ensure server is running, and break if it does not start serving in 2 seconds.
    int count = 40;
    while (!mServer.isServing() && serverThread.isAlive()) {
        if (count <= 0) {
            throw new RuntimeException("TThreadPoolServer does not start serving");
        }
        Thread.sleep(50);
        count--;
    }
}

From source file:alluxio.worker.AlluxioJobWorkerProcess.java

License:Apache License

/**
 *
 * Helper method to create a thrift server for handling incoming RPC requests.
 *
 * @return a thrift server/*from  w ww  .  j  a va 2  s .  com*/
 */
private TThreadPoolServer createThriftServer() {
    int minWorkerThreads = Configuration.getInt(PropertyKey.WORKER_BLOCK_THREADS_MIN);
    int maxWorkerThreads = Configuration.getInt(PropertyKey.WORKER_BLOCK_THREADS_MAX);
    TMultiplexedProcessor processor = new TMultiplexedProcessor();

    registerServices(processor, mJobWorker.getServices());

    // Return a TTransportFactory based on the authentication type
    TTransportFactory tTransportFactory;
    try {
        String serverName = NetworkAddressUtils.getConnectHost(ServiceType.JOB_WORKER_RPC);
        tTransportFactory = mTransportProvider.getServerTransportFactory(serverName);
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
    TThreadPoolServer.Args args = new TThreadPoolServer.Args(mThriftServerSocket)
            .minWorkerThreads(minWorkerThreads).maxWorkerThreads(maxWorkerThreads).processor(processor)
            .transportFactory(tTransportFactory).protocolFactory(new TBinaryProtocol.Factory(true, true));
    if (Configuration.getBoolean(PropertyKey.TEST_MODE)) {
        args.stopTimeoutVal = 0;
    } else {
        args.stopTimeoutVal = Constants.THRIFT_STOP_TIMEOUT_SECONDS;
    }
    return new TThreadPoolServer(args);
}

From source file:alluxio.worker.AlluxioWorker.java

License:Apache License

/**
 * Helper method to create a {@link org.apache.thrift.server.TThreadPoolServer} for handling
 * incoming RPC requests.//from  w w w .  java 2  s. com
 *
 * @return a thrift server
 */
private TThreadPoolServer createThriftServer() {
    int minWorkerThreads = Configuration.getInt(Constants.WORKER_WORKER_BLOCK_THREADS_MIN);
    int maxWorkerThreads = Configuration.getInt(Constants.WORKER_WORKER_BLOCK_THREADS_MAX);
    TMultiplexedProcessor processor = new TMultiplexedProcessor();

    registerServices(processor, mBlockWorker.getServices());
    registerServices(processor, mFileSystemWorker.getServices());
    // register additional workers for RPC service
    for (Worker worker : mAdditionalWorkers) {
        registerServices(processor, worker.getServices());
    }

    // Return a TTransportFactory based on the authentication type
    TTransportFactory tTransportFactory;
    try {
        tTransportFactory = mTransportProvider.getServerTransportFactory();
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
    TThreadPoolServer.Args args = new TThreadPoolServer.Args(mThriftServerSocket)
            .minWorkerThreads(minWorkerThreads).maxWorkerThreads(maxWorkerThreads).processor(processor)
            .transportFactory(tTransportFactory).protocolFactory(new TBinaryProtocol.Factory(true, true));
    if (Configuration.getBoolean(Constants.IN_TEST_MODE)) {
        args.stopTimeoutVal = 0;
    } else {
        args.stopTimeoutVal = Constants.THRIFT_STOP_TIMEOUT_SECONDS;
    }
    return new TThreadPoolServer(args);
}

From source file:alluxio.worker.AlluxioWorkerProcess.java

License:Apache License

/**
 * Helper method to create a {@link org.apache.thrift.server.TThreadPoolServer} for handling
 * incoming RPC requests./* ww  w  .j  a  v  a  2 s  .  com*/
 *
 * @return a thrift server
 */
private TThreadPoolServer createThriftServer() {
    int minWorkerThreads = Configuration.getInt(PropertyKey.WORKER_BLOCK_THREADS_MIN);
    int maxWorkerThreads = Configuration.getInt(PropertyKey.WORKER_BLOCK_THREADS_MAX);
    TMultiplexedProcessor processor = new TMultiplexedProcessor();

    for (Worker worker : mRegistry.getServers()) {
        registerServices(processor, worker.getServices());
    }

    // Return a TTransportFactory based on the authentication type
    TTransportFactory tTransportFactory;
    try {
        tTransportFactory = mTransportProvider.getServerTransportFactory();
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
    TThreadPoolServer.Args args = new TThreadPoolServer.Args(mThriftServerSocket)
            .minWorkerThreads(minWorkerThreads).maxWorkerThreads(maxWorkerThreads).processor(processor)
            .transportFactory(tTransportFactory).protocolFactory(new TBinaryProtocol.Factory(true, true));
    if (Configuration.getBoolean(PropertyKey.TEST_MODE)) {
        args.stopTimeoutVal = 0;
    } else {
        args.stopTimeoutVal = Constants.THRIFT_STOP_TIMEOUT_SECONDS;
    }
    return new TThreadPoolServer(args);
}