Example usage for org.apache.thrift TProcessorFactory TProcessorFactory

List of usage examples for org.apache.thrift TProcessorFactory TProcessorFactory

Introduction

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

Prototype

public TProcessorFactory(TProcessor processor) 

Source Link

Usage

From source file:JavaHsHaServer.java

License:Apache License

public static void main(String[] args) {
    try {//from  ww  w.  jav  a2  s . c o  m
        handler = new MyserviceHandler();
        processor = new Myservice.Processor(handler);

        TNonblockingServerSocket socket = new TNonblockingServerSocket(1357);
        THsHaServer.Args arg = new THsHaServer.Args(socket);
        arg.protocolFactory(new TBinaryProtocol.Factory());
        arg.transportFactory(new TFramedTransport.Factory());
        arg.processorFactory(new TProcessorFactory(processor));
        arg.workerThreads(5);

        TServer server = new THsHaServer(arg);
        server.serve();
        System.out.println("HsHa server started.");
    } catch (TTransportException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:bidiDemo.server.Server.java

public void init() {
    System.out.println("Initializing server...");

    final MessageDistributorAndHandler mdh = new MessageDistributorAndHandler();
    //new Thread(mdh.new Messenger()).start();

    // Using our own TProcessorFactory gives us an opportunity to get
    // access to the transport right after the client connection is
    // accepted.//from   w  w w.j  a  va  2s . c om
    TProcessorFactory processorFactory = new TProcessorFactory(null) {
        @Override
        public TProcessor getProcessor(TTransport trans) {
            MessageServiceClient msgClient = new MessageServiceClient(trans);
            mdh.addClient(msgClient);
            System.out.println("Client added to list.");
            return new MessageService.Processor(mdh.new MessageServiceHandler(msgClient));
        }
    };

    TThreadPoolServer.Args serverArgs = new TThreadPoolServer.Args(transport);
    serverArgs.processorFactory(processorFactory);
    TServer server = new TThreadPoolServer(serverArgs);
    System.out.println("Server started.");
    server.serve();
}

From source file:bidiSimpleDemo.server.Server.java

public static void main(String[] args) {
    try {//from www .j a  v  a 2  s .  c  o  m
        final MessageDistributor msgDistributor = new MessageDistributor();
        new Thread(msgDistributor).start();

        TProcessorFactory processorFactory = new TProcessorFactory(null) {
            @Override
            public TProcessor getProcessor(TTransport trans) {
                MessageService.Client msgSrvClient = new MessageService.Client(new TBinaryProtocol(trans));
                msgDistributor.addClient(msgSrvClient);
                System.out.println("Client added to list.");
                return new MessageService.Processor(new MessageServiceHandler(msgSrvClient, msgDistributor));
            }
        };

        TServerTransport serverTransport = new TServerSocket(9095);
        TThreadPoolServer.Args serverArgs = new TThreadPoolServer.Args(serverTransport);
        serverArgs.processorFactory(processorFactory);
        TServer server = new TThreadPoolServer(serverArgs);
        System.out.println("Java server started.");
        server.serve();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:com.bigdata.dastor.thrift.server.DastorMain.java

License:Apache License

private void setup() throws IOException, TTransportException {
    // BIGDATA add a configurable switch.
    if (DatabaseDescriptor.isTryLockMemoryEnabled()) {
        CLibrary.tryMlockall();/*w w w.j  ava 2  s. co  m*/
    }

    // log4j
    String file = System.getProperty("bigdata.conf.dir") + File.separator + "log4j.properties";
    PropertyConfigurator.configure(file);

    int listenPort = DatabaseDescriptor.getThriftPort();
    InetAddress listenAddr = DatabaseDescriptor.getThriftAddress();

    /* 
     * If ThriftAddress was left completely unconfigured, then assume
     * the same default as ListenAddress
     */
    if (listenAddr == null)
        listenAddr = FBUtilities.getLocalAddress();

    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        public void uncaughtException(Thread t, Throwable e) {
            logger.error("Uncaught exception in thread " + t, e);
            if (e instanceof OutOfMemoryError) {
                System.exit(100);
            }
        }
    });

    // check the system table for mismatched partitioner.
    try {
        SystemTable.checkHealth();
    } catch (IOException e) {
        logger.error("Fatal exception during initialization", e);
        System.exit(100);
    }

    // initialize keyspaces
    for (String table : DatabaseDescriptor.getTables()) {
        if (logger.isDebugEnabled())
            logger.debug("opening keyspace " + table);
        Table.open(table);
    }

    // replay the log if necessary and check for compaction candidates
    CommitLog.recover();
    CompactionManager.instance.checkAllColumnFamilies();

    // start server internals
    try {
        StorageService.instance.initServer();
    } catch (ConfigurationException e) {
        logger.error("Fatal error: " + e.getMessage());
        System.err.println("Bad configuration; unable to start server");
        System.exit(1);
    }

    // BIGDATA: init storage proxy
    StorageProxy.init();

    // now we start listening for clients
    final DastorThriftServer dastorServer = new DastorThriftServer();
    Dastor.Processor processor = new Dastor.Processor(dastorServer);

    // Transport
    TServerSocket tServerSocket = new TServerSocket(new InetSocketAddress(listenAddr, listenPort));

    // BIGDATA:
    logger.info(String.format("Starting up thrift server @ %s:%s", listenAddr, listenPort));

    // Protocol factory
    TProtocolFactory tProtocolFactory = new TBinaryProtocol.Factory();

    // Transport factory
    TTransportFactory inTransportFactory, outTransportFactory;
    if (DatabaseDescriptor.isThriftFramed()) {
        inTransportFactory = new TFramedTransport.Factory();
        outTransportFactory = new TFramedTransport.Factory();

    } else {
        inTransportFactory = new TTransportFactory();
        outTransportFactory = new TTransportFactory();
    }

    // ThreadPool Server
    CustomTThreadPoolServer.Options options = new CustomTThreadPoolServer.Options();
    options.minWorkerThreads = 64;

    SynchronousQueue<Runnable> executorQueue = new SynchronousQueue<Runnable>();

    ExecutorService executorService = new ThreadPoolExecutor(options.minWorkerThreads, options.maxWorkerThreads,
            60, TimeUnit.SECONDS, executorQueue) {
        @Override
        protected void afterExecute(Runnable r, Throwable t) {
            super.afterExecute(r, t);
            dastorServer.logout();
        }
    };
    serverEngine = new CustomTThreadPoolServer(new TProcessorFactory(processor), tServerSocket,
            inTransportFactory, outTransportFactory, tProtocolFactory, tProtocolFactory, options,
            executorService);
}

From source file:com.facebook.infrastructure.service.CassandraServer.java

License:Apache License

private static TThreadPoolServer thriftEngine(CassandraServer server) throws TTransportException {
    Cassandra.Processor processor = new Cassandra.Processor(server);
    // Transport/*from ww  w  . jav  a2s.  c om*/
    TServerSocket tServerSocket = new TServerSocket(9160);
    // Protocol factory
    TProtocolFactory tProtocolFactory = new TBinaryProtocol.Factory();
    // ThreadPool Server
    TThreadPoolServer.Options options = new TThreadPoolServer.Options();
    options.minWorkerThreads = 64;
    return new TThreadPoolServer(new TProcessorFactory(processor), tServerSocket, new TTransportFactory(),
            new TTransportFactory(), tProtocolFactory, tProtocolFactory, options);
}

From source file:com.facebook.nifty.core.ThriftServerDefBuilder.java

License:Apache License

/**
 * Specify the TProcessor./*from www  .j a  v  a  2 s.c o m*/
 */
public ThriftServerDefBuilder withProcessor(TProcessor p) {
    this.processorFactory = new TProcessorFactory(p);
    return this;
}

From source file:com.facebook.nifty.core.ThriftServerDefBuilderBase.java

License:Apache License

public T withProcessor(TProcessor processor) {
    this.thriftProcessorFactory = new TProcessorFactory(processor);
    return (T) this;
}

From source file:com.facebook.nifty.processor.NiftyProcessorAdapters.java

License:Apache License

/**
 * Create a standard thrift {@link TProcessorFactory} that always returns the same
 * {@link TProcessor} adapted from the given {@link NiftyProcessor}
 *///ww w.ja v  a 2  s. co m
public static TProcessorFactory processorToTProcessorFactory(final NiftyProcessor niftyProcessor) {
    return new TProcessorFactory(processorToTProcessor(niftyProcessor));
}

From source file:com.facebook.nifty.processor.NiftyProcessorAdapters.java

License:Apache License

/**
 * Create a standard thrift {@link TProcessorFactory} that delegates to a
 * {@link NiftyProcessorFactory} to construct an instance, then adapts each instance to a
 * standard Thrift {@link TProcessor}/*from  w w  w.  j  a  v a 2 s.  c o m*/
 */
public static TProcessorFactory processorFactoryToTProcessorFactory(
        final NiftyProcessorFactory niftyProcessorFactory) {
    return new TProcessorFactory(null) {
        @Override
        public TProcessor getProcessor(TTransport trans) {
            return processorToTProcessor(niftyProcessorFactory.getProcessor(trans));
        }
    };
}

From source file:com.ning.metrics.collector.endpoint.servers.CollectorTHsHaServer.java

License:Apache License

/**
 * Create server with given processor, server transport, and server options
 * using TBinaryProtocol for the protocol, and TFramedTransport.Factory on
 * both input and output transports. A TProcessorFactory will be created that
 * always returns the specified processor.
 *//*from   ww w  .j a va 2s .co  m*/
public CollectorTHsHaServer(TProcessor processor, TNonblockingServerTransport serverTransport,
        Options options) {
    this(new TProcessorFactory(processor), serverTransport, options);
}