Example usage for org.apache.thrift TMultiplexedProcessor registerProcessor

List of usage examples for org.apache.thrift TMultiplexedProcessor registerProcessor

Introduction

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

Prototype

public void registerProcessor(String serviceName, TProcessor processor) 

Source Link

Document

'Register' a service with this TMultiplexedProcessor.

Usage

From source file:alluxio.master.AlluxioJobMasterProcess.java

License:Apache License

private void registerServices(TMultiplexedProcessor processor, Map<String, TProcessor> services) {
    for (Map.Entry<String, TProcessor> service : services.entrySet()) {
        processor.registerProcessor(service.getKey(), service.getValue());
        LOG.info("registered service {}", service.getKey());
    }//  w  ww .  j a  va  2s  .co m
}

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;//w  w w.  j  av  a2s .  co m
    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

private void registerServices(TMultiplexedProcessor processor, Map<String, TProcessor> services) {
    for (Map.Entry<String, TProcessor> service : services.entrySet()) {
        processor.registerProcessor(service.getKey(), service.getValue());
    }/* ww w  . jav  a  2 s. c  om*/
}

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 a  v  a2s .c o m
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:com.cottsoft.thrift.framework.server.ThriftMultiBinaryServerFactory.java

License:Apache License

/**
 * ?Processor//  w  w  w  .j a v  a 2 s .c o m
 * @return
 * @throws ThriftException
 */
public TProcessor getProcessor() throws ThriftException {
    try {
        TMultiplexedProcessor processor = new TMultiplexedProcessor();

        Set<Class<?>> thriftServiceImplClassList = ScanPackageHander
                .getPackageAllClasses(baseServiceImplPackage, true);

        for (Class<?> thriftServiceImplClass : thriftServiceImplClassList) {
            if (thriftServiceImplClass.isAnnotationPresent(ThriftService.class)) {
                ThriftService thriftServiceAnnotation = (ThriftService) thriftServiceImplClass
                        .getAnnotation(ThriftService.class);
                String thriftServiceName = thriftServiceAnnotation.service();

                Constructor<?> constructor = Class.forName(thriftServiceName + "$Processor")
                        .getConstructor(Class.forName(thriftServiceName + "$Iface"));

                Object service = SpringApplicationContext
                        .getBean(getServiceImplBeanName(thriftServiceImplClass));

                processor.registerProcessor(thriftServiceName, (TProcessor) constructor.newInstance(service));

                if (logger.isDebugEnabled()) {
                    logger.debug(">>> Thrift Service implements class: " + thriftServiceImplClass.getName());
                }
            }
        }
        return processor;
    } catch (NoSuchMethodException e) {
        if (logger.isInfoEnabled()) {
            e.printStackTrace();
        }
        throw new ThriftException(e);
    } catch (SecurityException e) {
        if (logger.isInfoEnabled()) {
            e.printStackTrace();
        }
        throw new ThriftException(e);
    } catch (ClassNotFoundException e) {
        if (logger.isInfoEnabled()) {
            e.printStackTrace();
        }
        throw new ThriftException(e);
    } catch (InstantiationException e) {
        if (logger.isInfoEnabled()) {
            e.printStackTrace();
        }
        throw new ThriftException(e);
    } catch (IllegalAccessException e) {
        if (logger.isInfoEnabled()) {
            e.printStackTrace();
        }
        throw new ThriftException(e);
    } catch (IllegalArgumentException e) {
        if (logger.isInfoEnabled()) {
            e.printStackTrace();
        }
        throw new ThriftException(e);
    } catch (InvocationTargetException e) {
        if (logger.isInfoEnabled()) {
            e.printStackTrace();
        }
        throw new ThriftException(e);
    } catch (Exception e) {
        if (logger.isInfoEnabled()) {
            e.printStackTrace();
        }
        throw new ThriftException(e);
    }
}

From source file:com.vmware.photon.controller.chairman.ChairmanServer.java

License:Open Source License

public void serve() throws TTransportException, IOException {
    InetAddress registrationIpAddress = InetAddress.getByName(registrationAddress);
    if (registrationIpAddress.isAnyLocalAddress()) {
        logger.error("Using a wildcard registration address will not work with service registry: {}",
                registrationAddress);//  w  w  w.  ja  va 2  s  . c  om
        throw new IllegalArgumentException("Wildcard registration address");
    }

    InetAddress bindIpAddress = InetAddress.getByName(bind);
    InetSocketAddress bindSocketAddress = new InetSocketAddress(bindIpAddress, port);
    TServerSocket transport = new TServerSocket(bindSocketAddress);

    Chairman.Processor<ChairmanService> chairmanProcessor = new Chairman.Processor<>(chairmanService);
    TMultiplexedProcessor processor = new TMultiplexedProcessor();
    processor.registerProcessor("Chairman", chairmanProcessor);

    // TODO(vspivak): add configurable executor
    server = new TThreadPoolServer(new TThreadPoolServer.Args(transport).processor(processor)
            .protocolFactory(protocolFactory).transportFactory(transportFactory));

    // Need to re-fetch local port in case it was 0
    InetSocketAddress registrationSocketAddress = new InetSocketAddress(registrationIpAddress,
            transport.getServerSocket().getLocalPort());
    serviceNode = serviceNodeFactory.createLeader("chairman", registrationSocketAddress);

    ServiceNodeUtils.joinService(serviceNode, retryIntervalMsec, manager);

    logger.info("Starting chairman ({})", buildInfo);
    logger.info("Listening on: {}", bindSocketAddress);
    logger.info("Registering address: {}", registrationSocketAddress);
    server.serve();
}

From source file:com.vmware.photon.controller.deployer.DeployerServer.java

License:Open Source License

public void serve() throws UnknownHostException, TTransportException {
    InetAddress registrationIpAddress = InetAddress.getByName(registrationAddress);
    if (registrationIpAddress.isAnyLocalAddress()) {
        logger.error("Using a wildcard registration address will not work with service registry: {}",
                registrationAddress);/*from www . ja  va 2s . co m*/
        throw new IllegalArgumentException("Wildcard registration address");
    }

    InetAddress bindIpAddress = InetAddress.getByName(bind);
    InetSocketAddress bindSocketAddress = new InetSocketAddress(bindIpAddress, port);
    TServerSocket transport = new TServerSocket(bindSocketAddress);

    Deployer.Processor<DeployerService> deployerProcessor = new Deployer.Processor<>(deployerService);
    TMultiplexedProcessor processor = new TMultiplexedProcessor();
    processor.registerProcessor(SERVICE_NAME, deployerProcessor);

    server = new TThreadPoolServer(new TThreadPoolServer.Args(transport).processor(processor)
            .protocolFactory(protocolFactory).transportFactory(transportFactory));

    // Need to re-fetch local port in case it was 0
    InetSocketAddress registrationSocketAddress = new InetSocketAddress(registrationIpAddress,
            transport.getServerSocket().getLocalPort());
    serviceNode = serviceNodeFactory.createSimple("deployer", registrationSocketAddress);

    server.setServerEventHandler(getThriftEventHandler());

    logger.info("Starting deployer ({})", buildInfo);
    logger.info("Listening on: {}", bindSocketAddress);
    logger.info("Registering address: {}", registrationSocketAddress);
    logger.info("HttpClient is: {}", httpClient);
    server.serve();
}

From source file:com.vmware.photon.controller.housekeeper.HousekeeperServer.java

License:Open Source License

public void serve() throws UnknownHostException, TTransportException {
    InetAddress registrationIpAddress = InetAddress.getByName(registrationAddress);
    if (registrationIpAddress.isAnyLocalAddress()) {
        logger.error("Using a wildcard registration address will not work with service registry: {}",
                registrationAddress);/*from w ww.j av  a2  s  . co  m*/
        throw new IllegalArgumentException("Wildcard registration address");
    }

    InetAddress bindIpAddress = InetAddress.getByName(bind);
    InetSocketAddress bindSocketAddress = new InetSocketAddress(bindIpAddress, port);
    TServerSocket transport = new TServerSocket(bindSocketAddress);

    Housekeeper.Processor<HousekeeperService> housekeeperProcessor = new Housekeeper.Processor<>(
            housekeeperService);
    TMultiplexedProcessor processor = new TMultiplexedProcessor();
    processor.registerProcessor(SERVICE_NAME, housekeeperProcessor);

    server = new TThreadPoolServer(new TThreadPoolServer.Args(transport).processor(processor)
            .protocolFactory(protocolFactory).transportFactory(transportFactory));

    // Need to re-fetch local port in case it was 0
    InetSocketAddress registrationSocketAddress = new InetSocketAddress(registrationIpAddress,
            transport.getServerSocket().getLocalPort());
    serviceNode = serviceNodeFactory.createSimple("housekeeper", registrationSocketAddress);

    server.setServerEventHandler(getThriftEventHandler());

    logger.info("Starting housekeeper ({})", buildInfo);
    logger.info("Listening on: {}", bindSocketAddress);
    logger.info("Registering address: {}", registrationSocketAddress);
    server.serve();
}

From source file:com.vmware.photon.controller.rootscheduler.RootSchedulerServer.java

License:Open Source License

public void serve() throws TTransportException, UnknownHostException {
    InetAddress registrationIpAddress = InetAddress.getByName(registrationAddress);
    if (registrationIpAddress.isAnyLocalAddress()) {
        logger.error("Using a wildcard registration address will not work with service registry: {}",
                registrationAddress);/*  w  w  w  .  j av  a2 s  .c om*/
        throw new IllegalArgumentException("Wildcard registration address");
    }

    InetAddress bindIpAddress = InetAddress.getByName(bind);
    InetSocketAddress bindSocketAddress = new InetSocketAddress(bindIpAddress, port);
    TServerSocket transport = new TServerSocket(bindSocketAddress);

    RootScheduler.Processor<RootScheduler.Iface> rootSchedulerProcessor = new RootScheduler.Processor<>(
            rootSchedulerService);
    TMultiplexedProcessor processor = new TMultiplexedProcessor();
    processor.registerProcessor("RootScheduler", rootSchedulerProcessor);

    server = new TThreadPoolServer(new TThreadPoolServer.Args(transport).processor(processor)
            .protocolFactory(protocolFactory).transportFactory(transportFactory));

    // Need to re-fetch local port in case it was 0
    InetSocketAddress registrationSocketAddress = new InetSocketAddress(registrationIpAddress,
            transport.getServerSocket().getLocalPort());
    serviceNode = serviceNodeFactory.createSimple("root-scheduler", registrationSocketAddress);
    if (rootSchedulerService instanceof ServiceNodeEventHandler) {
        server.setServerEventHandler(
                thriftFactory.create((ServiceNodeEventHandler) rootSchedulerService, serviceNode));
    }
    logger.info("Starting root scheduler ({})", buildInfo);
    logger.info("Listening on: {}", bindSocketAddress);
    logger.info("Registering address: {}", registrationSocketAddress);
    server.serve();
}

From source file:com.wmz7year.thrift.pool.BasicAbstractTest.java

License:Apache License

protected ThriftServerInfo startMulitServiceServer() throws Throwable {
    // ???/*from  w w  w  .j  a v a 2s.c o  m*/
    final int port = choseListenPort();
    ThriftServerInfo serverInfo = new ThriftServerInfo(LOACLHOST, port);
    final AtomicReference<Throwable> ex = new AtomicReference<Throwable>();
    // TODO
    Thread runner = new Thread("thrift-server-starter") {
        @Override
        public void run() {
            try {
                TMultiplexedProcessor processor = new TMultiplexedProcessor();
                TServerTransport serverTransport = new TServerSocket(port);
                Factory proFactory = new TBinaryProtocol.Factory();

                processor.registerProcessor("example",
                        new Example.Processor<Example.Iface>(new Example.Iface() {

                            @Override
                            public void pong() throws TException {
                                logger.info("example pong");
                            }

                            @Override
                            public void ping() throws TException {
                                logger.info("example ping");
                            }
                        }));

                processor.registerProcessor("other", new Other.Processor<Other.Iface>(new Other.Iface() {

                    @Override
                    public void pong() throws TException {
                        logger.info("other pong");
                    }

                    @Override
                    public void ping() throws TException {
                        logger.info("other ping");
                    }
                }));
                Args thriftArgs = new Args(serverTransport);
                thriftArgs.processor(processor);
                thriftArgs.protocolFactory(proFactory);
                TServer tserver = new TThreadPoolServer(thriftArgs);
                servers.add(tserver);
                logger.info("???" + port);
                tserver.serve();
            } catch (TTransportException e) {
                logger.error("thrift??", e);
                ex.set(e);
            }
        }
    };

    runner.start();

    Throwable throwable = ex.get();
    if (throwable != null) {
        throw throwable;
    }
    // ??
    Thread.sleep(1000);
    return serverInfo;
}