Example usage for org.apache.thrift.transport TServerSocket TServerSocket

List of usage examples for org.apache.thrift.transport TServerSocket TServerSocket

Introduction

In this page you can find the example usage for org.apache.thrift.transport TServerSocket TServerSocket.

Prototype

public TServerSocket(ServerSocketTransportArgs args) throws TTransportException 

Source Link

Usage

From source file:StubServer.java

License:Apache License

public static void main(String argv[]) {
    try {//from   w  w  w . j  ava 2  s  . co 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:JavaServer.java

License:Apache License

public static void simple(Calculator.Processor processor) {
    try {/*from www . ja  va2  s  .c o  m*/
        TServerTransport serverTransport = new TServerSocket(9090);
        TServer server = new TSimpleServer(new Args(serverTransport).processor(processor));

        // Use this for a multithreaded server
        // 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:FilesystemServer.java

public static void simple(FilesystemService.Processor processor) {
    try {//from   w w w .  jav a 2 s. co m
        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:adept.mappers.thrift.serialization.SerializationServer.java

License:Apache License

public static void simple(Serializer.Processor processor, int port) {
    try {/*from  w  w w.j  a  va2s  .com*/
        TServerTransport serverTransport = new TServerSocket(new java.net.ServerSocket(port));
        TServer server = new TSimpleServer(new Args(serverTransport).processor(processor));

        // Use this for a multithreaded server
        // 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:alluxio.master.AlluxioJobMasterProcess.java

License:Apache License

AlluxioJobMasterProcess(JournalSystem journalSystem) {
    if (!Configuration.containsKey(PropertyKey.MASTER_HOSTNAME)) {
        Configuration.set(PropertyKey.MASTER_HOSTNAME, NetworkAddressUtils.getLocalHostName());
    }//from  www.  j  av a  2  s.com
    mUfsManager = new JobUfsManager();
    mJournalSystem = Preconditions.checkNotNull(journalSystem, "journalSystem");
    mMinWorkerThreads = Configuration.getInt(PropertyKey.MASTER_WORKER_THREADS_MIN);
    mMaxWorkerThreads = Configuration.getInt(PropertyKey.MASTER_WORKER_THREADS_MAX);

    Preconditions.checkArgument(mMaxWorkerThreads >= mMinWorkerThreads, PropertyKey.MASTER_WORKER_THREADS_MAX
            + " can not be less than " + PropertyKey.MASTER_WORKER_THREADS_MIN);

    try {
        // Extract the port from the generated socket.
        // When running tests, it is fine to use port '0' so the system will figure out what port to
        // use (any random free port).
        // In a production or any real deployment setup, port '0' should not be used as it will make
        // deployment more complicated.
        if (!Configuration.getBoolean(PropertyKey.TEST_MODE)) {
            Preconditions.checkState(Configuration.getInt(PropertyKey.JOB_MASTER_RPC_PORT) > 0,
                    "Master rpc port is only allowed to be zero in test mode.");
            Preconditions.checkState(Configuration.getInt(PropertyKey.JOB_MASTER_WEB_PORT) > 0,
                    "Master web port is only allowed to be zero in test mode.");
        }
        mTransportProvider = TransportProvider.Factory.create();
        mTServerSocket = new TServerSocket(NetworkAddressUtils.getBindAddress(ServiceType.JOB_MASTER_RPC));
        mPort = ThriftUtils.getThriftPort(mTServerSocket);
        // reset master port
        Configuration.set(PropertyKey.JOB_MASTER_RPC_PORT, Integer.toString(mPort));
        mRpcBindAddress = NetworkAddressUtils.getBindAddress(ServiceType.JOB_MASTER_RPC);
        mRpcConnectAddress = NetworkAddressUtils.getConnectAddress(ServiceType.JOB_MASTER_RPC);

        // Create master.
        createMaster();
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        throw Throwables.propagate(e);
    }
}

From source file:alluxio.master.AlluxioMaster.java

License:Apache License

protected AlluxioMaster() {
    mMinWorkerThreads = Configuration.getInt(PropertyKey.MASTER_WORKER_THREADS_MIN);
    mMaxWorkerThreads = Configuration.getInt(PropertyKey.MASTER_WORKER_THREADS_MAX);

    Preconditions.checkArgument(mMaxWorkerThreads >= mMinWorkerThreads, PropertyKey.MASTER_WORKER_THREADS_MAX
            + " can not be less than " + PropertyKey.MASTER_WORKER_THREADS_MIN);

    try {/* w w w . ja v  a 2 s. c om*/
        // Extract the port from the generated socket.
        // When running tests, it is fine to use port '0' so the system will figure out what port to
        // use (any random free port).
        // In a production or any real deployment setup, port '0' should not be used as it will make
        // deployment more complicated.
        if (!Configuration.getBoolean(PropertyKey.TEST_MODE)) {
            Preconditions.checkState(Configuration.getInt(PropertyKey.MASTER_RPC_PORT) > 0,
                    "Alluxio master rpc port is only allowed to be zero in test mode.");
            Preconditions.checkState(Configuration.getInt(PropertyKey.MASTER_WEB_PORT) > 0,
                    "Alluxio master web port is only allowed to be zero in test mode.");
        }
        mTransportProvider = TransportProvider.Factory.create();
        mTServerSocket = new TServerSocket(NetworkAddressUtils.getBindAddress(ServiceType.MASTER_RPC));
        mPort = NetworkAddressUtils.getThriftPort(mTServerSocket);
        // reset master rpc port
        Configuration.set(PropertyKey.MASTER_RPC_PORT, Integer.toString(mPort));
        mMasterAddress = NetworkAddressUtils.getConnectAddress(ServiceType.MASTER_RPC);

        // Check the journal directory
        String journalDirectory = Configuration.get(PropertyKey.MASTER_JOURNAL_FOLDER);
        if (!journalDirectory.endsWith(AlluxioURI.SEPARATOR)) {
            journalDirectory += AlluxioURI.SEPARATOR;
        }
        Preconditions.checkState(isJournalFormatted(journalDirectory),
                "Alluxio master was not formatted! The journal folder is " + journalDirectory);

        // Create the journals.
        mBlockMasterJournal = new ReadWriteJournal(BlockMaster.getJournalDirectory(journalDirectory));
        mFileSystemMasterJournal = new ReadWriteJournal(FileSystemMaster.getJournalDirectory(journalDirectory));
        mLineageMasterJournal = new ReadWriteJournal(LineageMaster.getJournalDirectory(journalDirectory));

        mBlockMaster = new BlockMaster(mBlockMasterJournal);
        mFileSystemMaster = new FileSystemMaster(mBlockMaster, mFileSystemMasterJournal);
        if (LineageUtils.isLineageEnabled()) {
            mLineageMaster = new LineageMaster(mFileSystemMaster, mLineageMasterJournal);
        }

        mAdditionalMasters = new ArrayList<>();
        List<? extends Master> masters = Lists.newArrayList(mBlockMaster, mFileSystemMaster);
        for (MasterFactory factory : getServiceLoader()) {
            Master master = factory.create(masters, journalDirectory);
            if (master != null) {
                mAdditionalMasters.add(master);
            }
        }
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        throw Throwables.propagate(e);
    }
}

From source file:alluxio.master.DefaultAlluxioMaster.java

License:Apache License

protected DefaultAlluxioMaster() {
    mMinWorkerThreads = Configuration.getInt(PropertyKey.MASTER_WORKER_THREADS_MIN);
    mMaxWorkerThreads = Configuration.getInt(PropertyKey.MASTER_WORKER_THREADS_MAX);

    Preconditions.checkArgument(mMaxWorkerThreads >= mMinWorkerThreads, PropertyKey.MASTER_WORKER_THREADS_MAX
            + " can not be less than " + PropertyKey.MASTER_WORKER_THREADS_MIN);

    try {//from   ww  w.j a  v  a2  s.c o m
        // Extract the port from the generated socket.
        // When running tests, it is fine to use port '0' so the system will figure out what port to
        // use (any random free port).
        // In a production or any real deployment setup, port '0' should not be used as it will make
        // deployment more complicated.
        if (!Configuration.getBoolean(PropertyKey.TEST_MODE)) {
            Preconditions.checkState(Configuration.getInt(PropertyKey.MASTER_RPC_PORT) > 0,
                    "Alluxio master rpc port is only allowed to be zero in test mode.");
            Preconditions.checkState(Configuration.getInt(PropertyKey.MASTER_WEB_PORT) > 0,
                    "Alluxio master web port is only allowed to be zero in test mode.");
        }
        mTransportProvider = TransportProvider.Factory.create();
        mTServerSocket = new TServerSocket(NetworkAddressUtils.getBindAddress(ServiceType.MASTER_RPC));
        mPort = NetworkAddressUtils.getThriftPort(mTServerSocket);
        // reset master rpc port
        Configuration.set(PropertyKey.MASTER_RPC_PORT, Integer.toString(mPort));
        mRpcAddress = NetworkAddressUtils.getConnectAddress(ServiceType.MASTER_RPC);

        // Create the journals.
        createMasters(new JournalFactory.ReadWrite(getJournalDirectory()));
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}

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

License:Apache License

/**
 * Sets up the server before running a test.
 *
 * @throws Exception thrown when the {@link TServerSocket} cannot be constructed
 *///w ww .  j a v  a 2s. co  m
@Before
public void before() throws Exception {
    mConfiguration = new Configuration();
    // Use port 0 to assign each test case an available port (possibly different)
    String localhost = NetworkAddressUtils.getLocalHostName(new Configuration());
    mServerTSocket = new TServerSocket(new InetSocketAddress(localhost, 0));
    int port = NetworkAddressUtils.getThriftPort(mServerTSocket);
    mServerAddress = new InetSocketAddress(localhost, port);
    mClientTSocket = AuthenticationUtils.createTSocket(mServerAddress,
            mConfiguration.getInt(Constants.SECURITY_AUTHENTICATION_SOCKET_TIMEOUT_MS));
}

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

License:Apache License

/**
 * Sets up the server before running a test.
 *//* w  w w . j av  a 2 s. c om*/
@Before
public void before() throws Exception {
    // Use port 0 to assign each test case an available port (possibly different)
    String localhost = NetworkAddressUtils.getLocalHostName();
    mServerTSocket = new TServerSocket(new InetSocketAddress(localhost, 0));
    int port = NetworkAddressUtils.getThriftPort(mServerTSocket);
    mServerAddress = new InetSocketAddress(localhost, port);
}

From source file:alluxio.worker.AlluxioJobWorkerProcess.java

License:Apache License

/**
 * Helper method to create a {@link TServerSocket} for the RPC server.
 *
 * @return a thrift server socket//www.j a va2  s. c  o  m
 */
private TServerSocket createThriftServerSocket() {
    try {
        return new TServerSocket(NetworkAddressUtils.getBindAddress(ServiceType.JOB_WORKER_RPC));
    } catch (TTransportException e) {
        LOG.error(e.getMessage(), e);
        throw Throwables.propagate(e);
    }
}