org.apache.thrift.transport.TNonblockingServerSocket Java Examples

The following examples show how to use org.apache.thrift.transport.TNonblockingServerSocket. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AsyncServer.java    From ThriftBook with Apache License 2.0 7 votes vote down vote up
public static void main(String[] args)
        throws TTransportException, IOException, InterruptedException {

    FloorBroker floor = new FloorBroker();
    (new Thread(floor)).start();

    TProcessor proc = new TradeReporting.TradeHistory.AsyncProcessor(
        new AsyncTradeHistoryHandler(floor.getQ()));
    TNonblockingServerSocket trans_svr = new TNonblockingServerSocket(9090);
    TServer server = 
        new THsHaServer(new THsHaServer.Args(trans_svr)
            .processor(proc)
            .protocolFactory(new TBinaryProtocol.Factory())
            .minWorkerThreads(4)
            .maxWorkerThreads(4));
    System.out.println("[Server] listening of port 9090");
    server.serve();
}
 
Example #2
Source File: AsyncEchoTestServer.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
public static AsyncEchoTestServer<TNonblockingServer> nonblockingServer(final TestEnvironment environment)
        throws TTransportException {
    TNonblockingServer server = new TNonblockingServer(new TNonblockingServer.Args(
            new TNonblockingServerSocket(environment.getPort())).processor(getAsyncProcessor())
            .inputProtocolFactory(environment.getProtocolFactory())
            .outputProtocolFactory(environment.getProtocolFactory()));
    return new AsyncEchoTestServer<TNonblockingServer>(server, environment) {
        @Override
        public SyncEchoTestClient getSynchronousClient() throws TTransportException {
            return new SyncEchoTestClient.ClientForNonblockingServer(environment);
        }

        @Override
        public AsyncEchoTestClient getAsynchronousClient() throws IOException {
            return new AsyncEchoTestClient.Client(environment);
        }
    };
}
 
Example #3
Source File: SimpleTransportPlugin.java    From jstorm with Apache License 2.0 6 votes vote down vote up
@Override
public TServer getServer(TProcessor processor) throws IOException, TTransportException {
    int port = type.getPort(storm_conf);
    TNonblockingServerSocket serverTransport = new TNonblockingServerSocket(port);
    int numWorkerThreads = type.getNumThreads(storm_conf);
    int maxBufferSize = type.getMaxBufferSize(storm_conf);
    Integer queueSize = type.getQueueSize(storm_conf);

    THsHaServer.Args server_args =
            new THsHaServer.Args(serverTransport).processor(new SimpleWrapProcessor(processor)).workerThreads(numWorkerThreads)
                    .protocolFactory(new TBinaryProtocol.Factory(false, true, maxBufferSize, -1));

    if (queueSize != null) {
        server_args.executorService(new ThreadPoolExecutor(numWorkerThreads, numWorkerThreads, 60, TimeUnit.SECONDS, new ArrayBlockingQueue(queueSize)));
    }

    // construct THsHaServer
    return new THsHaServer(server_args);
}
 
Example #4
Source File: NimbusServer.java    From jstorm with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("rawtypes")
private void initThrift(Map conf) throws TTransportException {
    Integer thrift_port = JStormUtils.parseInt(conf.get(Config.NIMBUS_THRIFT_PORT));
    TNonblockingServerSocket socket = new TNonblockingServerSocket(thrift_port);

    Integer maxReadBufSize = JStormUtils.parseInt(conf.get(Config.NIMBUS_THRIFT_MAX_BUFFER_SIZE));

    THsHaServer.Args args = new THsHaServer.Args(socket);
    args.workerThreads(ServiceHandler.THREAD_NUM);
    args.protocolFactory(new TBinaryProtocol.Factory(false, true, maxReadBufSize, -1));

    args.processor(new Nimbus.Processor<Iface>(serviceHandler));
    args.maxReadBufferBytes = maxReadBufSize;

    thriftServer = new THsHaServer(args);

    LOG.info("Successfully started nimbus: started Thrift server...");
    thriftServer.serve();
}
 
Example #5
Source File: Drpc.java    From jstorm with Apache License 2.0 6 votes vote down vote up
private THsHaServer initInvokeServer(Map conf, final Drpc service) throws Exception {
    int port = JStormUtils.parseInt(conf.get(Config.DRPC_INVOCATIONS_PORT));

    LOG.info("Begin to init DRPC invoke server at port: " + port);

    TNonblockingServerSocket socket = new TNonblockingServerSocket(port);
    THsHaServer.Args targsInvoke = new THsHaServer.Args(socket);
    targsInvoke.workerThreads(64);
    targsInvoke.protocolFactory(new TBinaryProtocol.Factory());
    targsInvoke.processor(new DistributedRPCInvocations.Processor<DistributedRPCInvocations.Iface>(service));

    THsHaServer invokeServer = new THsHaServer(targsInvoke);

    LOG.info("Successfully inited DRPC invoke server at port: " + port);
    return invokeServer;
}
 
Example #6
Source File: Drpc.java    From jstorm with Apache License 2.0 6 votes vote down vote up
private THsHaServer initHandlerServer(Map conf, final Drpc service) throws Exception {
    int port = JStormUtils.parseInt(conf.get(Config.DRPC_PORT));
    int workerThreadNum = JStormUtils.parseInt(conf.get(Config.DRPC_WORKER_THREADS));
    int queueSize = JStormUtils.parseInt(conf.get(Config.DRPC_QUEUE_SIZE));

    LOG.info("Begin to init DRPC handler server at port: " + port);

    TNonblockingServerSocket socket = new TNonblockingServerSocket(port);
    THsHaServer.Args targs = new THsHaServer.Args(socket);
    targs.workerThreads(64);
    targs.protocolFactory(new TBinaryProtocol.Factory());
    targs.processor(new DistributedRPC.Processor<DistributedRPC.Iface>(service));

    ThreadPoolExecutor executor = new ThreadPoolExecutor(
            workerThreadNum, workerThreadNum, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(queueSize));
    targs.executorService(executor);

    THsHaServer handlerServer = new THsHaServer(targs);
    LOG.info("Successfully inited DRPC handler server at port: " + port);

    return handlerServer;
}
 
Example #7
Source File: ThriftQueueServer.java    From bigqueue with Apache License 2.0 6 votes vote down vote up
public void start() {
	try {
		System.out.println("Thrift queue server start ...");
		
		BigQueueService.Iface bigQueueSerivce = new ThriftQueueServiceImpl(QUEUE_DIR);
		TProcessor tprocessor = new BigQueueService.Processor(bigQueueSerivce);
		
		TNonblockingServerSocket tnbSocketTransport = new TNonblockingServerSocket(SERVER_PORT);
		TNonblockingServer.Args tnbArgs = new TNonblockingServer.Args(tnbSocketTransport);
		tnbArgs.processor(tprocessor);
		// Nonblocking server mode needs TFramedTransport
		tnbArgs.transportFactory(new TFramedTransport.Factory());
		tnbArgs.protocolFactory(new TBinaryProtocol.Factory());
		
		TServer server = new TNonblockingServer(tnbArgs);
		System.out.println("Thrift queue server started on port " + SERVER_PORT);
		server.serve();
	} catch (Exception e) {
		System.err.println("Server start error!!!");
		e.printStackTrace();
	}
}
 
Example #8
Source File: SuroServer4Test.java    From suro with Apache License 2.0 6 votes vote down vote up
public  void start() throws Exception {
    transport = new TNonblockingServerSocket(port);
    processor =  new SuroServer.Processor(this);

    THsHaServer.Args serverArgs = new THsHaServer.Args(transport);
    serverArgs.processor(processor);
    serverArgs.workerThreads(2);

    server = new THsHaServer(serverArgs);
    System.out.println("Server started on port:" + port);

    Thread t = new Thread() {

        @Override
        public void run() {
            server.serve();
        }

    };
    t.start();

    Field serverSocketField = TNonblockingServerSocket.class.getDeclaredField("serverSocket_");
    serverSocketField.setAccessible(true);
    ServerSocket serverSocket = (ServerSocket) serverSocketField.get(transport);
    port = serverSocket.getLocalPort();
}
 
Example #9
Source File: ThriftServer.java    From luxun with Apache License 2.0 6 votes vote down vote up
public ThriftServer(QueueService.Iface queueService, ServerConfig serverConfig, ThriftServerStats stats) throws TTransportException {
		this.queueService = queueService;
		this.serverConfig = serverConfig;
        this.stats = stats;
        
        // assemble thrift server
        TProcessor tprocessor = new QueueService.Processor(this.queueService);
        TNonblockingServerSocket tnbSocketTransport = new TNonblockingServerSocket(serverConfig.getPort());
        TNonblockingServer.Args tnbArgs = new TNonblockingServer.Args(tnbSocketTransport);
        tnbArgs.processor(tprocessor);
        // Nonblocking server mode must use TFramedTransport
        tnbArgs.transportFactory(new TFramedTransport.Factory());
        tnbArgs.protocolFactory(new TBinaryProtocol.Factory());
        
        this.server = new TNonblockingServer(tnbArgs);
        
//        THsHaServer.Args thhArgs = new THsHaServer.Args(tnbSocketTransport);
//        thhArgs.processor(tprocessor);
//        // Nonblocking server mode must use TFramedTransport
//        thhArgs.transportFactory(new TFramedTransport.Factory());
//        thhArgs.protocolFactory(new TBinaryProtocol.Factory());
//
//        this.server = new THsHaServer(thhArgs);
    
        this.serverThread = new ServerThread(this.server);
	}
 
Example #10
Source File: ScribeSource.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
public void run() {
  try {
    Scribe.Processor processor = new Scribe.Processor(new Receiver());
    TNonblockingServerTransport transport = new TNonblockingServerSocket(port);
    THsHaServer.Args args = new THsHaServer.Args(transport);
    
    args.workerThreads(workers);
    args.processor(processor);
    args.transportFactory(new TFramedTransport.Factory());
    args.protocolFactory(new TBinaryProtocol.Factory(false, false));

    server = new THsHaServer(args);

    LOG.info("Starting Scribe Source on port " + port);

    server.serve();
  } catch (Exception e) {
    LOG.warn("Scribe failed", e);
  }
}
 
Example #11
Source File: SyncEchoTestServer.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
public static SyncEchoTestServer<THsHaServer> halfSyncHalfAsyncServer(final TestEnvironment environment)
        throws TTransportException {
    THsHaServer server = new THsHaServer(new THsHaServer.Args(new TNonblockingServerSocket(
            environment.getPort())).processor(getProcessor())
            .inputProtocolFactory(environment.getProtocolFactory())
            .outputProtocolFactory(environment.getProtocolFactory()));
    return new SyncEchoTestServer<THsHaServer>(server, environment) {
        @Override
        public SyncEchoTestClient getSynchronousClient() throws TTransportException {
            return new SyncEchoTestClient.ClientForNonblockingServer(environment);
        }

        @Override
        public AsyncEchoTestClient getAsynchronousClient() throws IOException {
            return new AsyncEchoTestClient.Client(environment);
        }
    };
}
 
Example #12
Source File: SyncEchoTestServer.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
public static SyncEchoTestServer<TNonblockingServer> nonblockingServer(final TestEnvironment environment)
        throws TTransportException {
    TNonblockingServer server = new TNonblockingServer(new TNonblockingServer.Args(
            new TNonblockingServerSocket(environment.getPort())).processor(getProcessor())
            .inputProtocolFactory(environment.getProtocolFactory())
            .outputProtocolFactory(environment.getProtocolFactory()));
    return new SyncEchoTestServer<TNonblockingServer>(server, environment) {
        @Override
        public SyncEchoTestClient getSynchronousClient() throws TTransportException {
            return new SyncEchoTestClient.ClientForNonblockingServer(environment);
        }

        @Override
        public AsyncEchoTestClient getAsynchronousClient() throws IOException {
            return new AsyncEchoTestClient.Client(environment);
        }
    };
}
 
Example #13
Source File: SyncEchoTestServer.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
public static SyncEchoTestServer<TThreadedSelectorServer> threadedSelectorServer(
        final TestEnvironment environment) throws TTransportException {
    TThreadedSelectorServer server = new TThreadedSelectorServer(new TThreadedSelectorServer.Args(
            new TNonblockingServerSocket(environment.getPort())).processor(getProcessor())
            .inputProtocolFactory(environment.getProtocolFactory())
            .outputProtocolFactory(environment.getProtocolFactory()));
    return new SyncEchoTestServer<TThreadedSelectorServer>(server, environment) {
        @Override
        public SyncEchoTestClient getSynchronousClient() throws TTransportException {
            return new SyncEchoTestClient.ClientForNonblockingServer(environment);
        }

        @Override
        public AsyncEchoTestClient getAsynchronousClient() throws IOException {
            return new AsyncEchoTestClient.Client(environment);
        }
    };
}
 
Example #14
Source File: AsyncEchoTestServer.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
public static AsyncEchoTestServer<THsHaServer> halfSyncHalfAsyncServer(final TestEnvironment environment)
        throws TTransportException {
    THsHaServer server = new THsHaServer(new THsHaServer.Args(new TNonblockingServerSocket(
            environment.getPort())).processor(getAsyncProcessor())
            .inputProtocolFactory(environment.getProtocolFactory())
            .outputProtocolFactory(environment.getProtocolFactory()));
    return new AsyncEchoTestServer<THsHaServer>(server, environment) {
        @Override
        public SyncEchoTestClient getSynchronousClient() throws TTransportException {
            return new SyncEchoTestClient.ClientForNonblockingServer(environment);
        }

        @Override
        public AsyncEchoTestClient getAsynchronousClient() throws IOException {
            return new AsyncEchoTestClient.Client(environment);
        }
    };
}
 
Example #15
Source File: AsyncEchoTestServer.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
public static AsyncEchoTestServer<TThreadedSelectorServer> threadedSelectorServer(
        final TestEnvironment environment) throws TTransportException {
    TThreadedSelectorServer server = new TThreadedSelectorServer(new TThreadedSelectorServer.Args(
            new TNonblockingServerSocket(environment.getPort())).processor(getAsyncProcessor())
            .inputProtocolFactory(environment.getProtocolFactory())
            .outputProtocolFactory(environment.getProtocolFactory()));
    return new AsyncEchoTestServer<TThreadedSelectorServer>(server, environment) {
        @Override
        public SyncEchoTestClient getSynchronousClient() throws TTransportException {
            return new SyncEchoTestClient.ClientForNonblockingServer(environment);
        }

        @Override
        public AsyncEchoTestClient getAsynchronousClient() throws IOException {
            return new AsyncEchoTestClient.Client(environment);
        }
    };
}
 
Example #16
Source File: ThriftServer.java    From plow with Apache License 2.0 6 votes vote down vote up
@PostConstruct
public void start() {
    logger.info("Starting thift server " + name + " on port " + port);

    try {
        transport = new TNonblockingServerSocket(port);
        server = new TThreadedSelectorServer(
                new TThreadedSelectorServer.Args(transport)
            .processor(processor)
            .workerThreads(threads)
            .selectorThreads((threads / 16) + 1)
            .protocolFactory(protocolFactory)
            .transportFactory(new TFramedTransport.Factory()));
        thread.start();

    } catch (TTransportException e) {
        throw new RuntimeException("Unable to start thrift server " + e, e);
    }
}
 
Example #17
Source File: MultiServiceServer.java    From ThriftBook with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws TTransportException, IOException, InterruptedException {
    TNonblockingServerSocket trans_svr = new TNonblockingServerSocket(9090);
    TMultiplexedProcessor proc = new TMultiplexedProcessor();
    proc.registerProcessor("Message", new Message.Processor<>(new MessageHandler()));
    proc.registerProcessor("ServerTime", new ServerTime.Processor<>(new ServerTimeHandler()));

    TServer server = new TThreadedSelectorServer(
            new TThreadedSelectorServer.Args(trans_svr)
            .processor(proc)
            .protocolFactory(new TJSONProtocol.Factory())
            .workerThreads(6)
            .selectorThreads(3));
    Thread server_thread = new Thread(new RunnableServer(server), "server_thread");
    server_thread.start();

    System.out.println("[Server] press enter to shutdown> ");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    br.readLine();
    System.out.println("[Server] shutting down...");
    server.stop();
    server_thread.join();
    System.out.println("[Server] down, exiting");
}
 
Example #18
Source File: rpcServer.java    From leaf-snowflake with Apache License 2.0 6 votes vote down vote up
public static void startRPCServer2(leafServer leafserver , String ip , int port) throws Exception
{
	//关联处理器leafrpc的实现
	TProcessor processor = new leafrpc.Processor<leafrpc.Iface>(new RPCService(leafserver));
	//传输通道,非阻塞模式
	InetSocketAddress address = new InetSocketAddress(InetAddress.getByName(ip),port);
	TNonblockingServerSocket serverTransport = new TNonblockingServerSocket(address,10000);
	//多线程半同步半异步
	TThreadedSelectorServer.Args tArgs = new TThreadedSelectorServer.Args(serverTransport);
	tArgs.processor(processor);
	//二进制协议
	tArgs.protocolFactory(new TBinaryProtocol.Factory());
	//多线程半同步半异步的服务模型
	TServer server = new TThreadedSelectorServer(tArgs);
	LOG.info("leaf RPCServer(type:TThreadedSelectorServer) start at ip:port : "+ ip +":" + port );
	server.serve();
}
 
Example #19
Source File: ThriftServerTest.java    From ThriftJ with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args){
	ExecutorService es = Executors.newFixedThreadPool(2);
	for(int i=0; i<ports.length; i++){
		final int index = i;
		es.execute(new Runnable() {
			@Override
			public void run() {
				try{
					TNonblockingServerSocket socket = new TNonblockingServerSocket(ports[index]);
					TestThriftJ.Processor processor = new TestThriftJ.Processor(new QueryImp());
					TNonblockingServer.Args arg = new TNonblockingServer.Args(socket);
					arg.protocolFactory(new TBinaryProtocol.Factory());
					arg.transportFactory(new TFramedTransport.Factory());
					arg.processorFactory(new TProcessorFactory(processor));
					TServer server = new TNonblockingServer(arg);
					
					logger.info("127.0.0.1:" + ports[index] + " start");
					server.serve();
				}catch(Exception e){
					logger.error("127.0.0.1:" + ports[index] + " error");
				}
			}
		});
	}
}
 
Example #20
Source File: ThriftServerTest2.java    From ThriftJ with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args){
	ExecutorService es = Executors.newFixedThreadPool(2);
	for(int i=0; i<ports.length; i++){
		final int index = i;
		es.execute(new Runnable() {
			@Override
			public void run() {
				try{
					TNonblockingServerSocket socket = new TNonblockingServerSocket(ports[index]);
					TestThriftJ.Processor processor = new TestThriftJ.Processor(new QueryImp());
					TNonblockingServer.Args arg = new TNonblockingServer.Args(socket);
					arg.protocolFactory(new TBinaryProtocol.Factory());
					arg.transportFactory(new TFramedTransport.Factory());
					arg.processorFactory(new TProcessorFactory(processor));
					TServer server = new TNonblockingServer (arg);
					
					logger.info("127.0.0.1:" + ports[index] + " start");
					server.serve();
				}catch(Exception e){
					logger.error("127.0.0.1:" + ports[index] + " error");
				}
			}
		});
	}
}
 
Example #21
Source File: AppServer.java    From rpcx-benchmark with Apache License 2.0 6 votes vote down vote up
public static void simple(com.colobu.thrift.Greeter.Processor processor) {
        try {
            TNonblockingServerSocket serverTransport = new TNonblockingServerSocket(8972);
            //TServer server = new TSimpleServer(new TServer.Args(serverTransport).processor(processor));

            // Use this for a multithreaded server
            //TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));

            //https://github.com/rpcx-ecosystem/rpcx-benchmark/issues/1
//            TThreadedSelectorServer server = new TThreadedSelectorServer(
//                new TThreadedSelectorServer.Args(serverTransport).processor(processor).
//                selectorThreads(2).workerThreads(512));

            TThreadedSelectorServer server = new TThreadedSelectorServer(
                    new TThreadedSelectorServer.Args(serverTransport).processor(processor).
                            selectorThreads(2).workerThreads(512));

            System.out.println("Starting the simple server...");
            server.serve();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
Example #22
Source File: DasServer.java    From das with Apache License 2.0 5 votes vote down vote up
public static void startServer(int port) throws TTransportException, UnknownHostException, SQLException {
    final long start = System.currentTimeMillis();
    
    String address = InetAddress.getLocalHost().getHostAddress();
    
    DasServerContext serverContext = new DasServerContext(address, port);

    DasServer server = new DasServer(address, serverContext.getWorkerId());

    TBinaryProtocol.Factory protocolFactory = new TBinaryProtocol.Factory();

    DasService.Processor<DasService.Iface> processor = new DasService.Processor<>(
            server);

    int selector = Integer.parseInt(serverContext.getServerConfigure().get(SELECTING_NUMBER));
    int worker   = Integer.parseInt(serverContext.getServerConfigure().get(WORKING_NUMBER));
    TThreadedSelectorServer ttServer = new TThreadedSelectorServer(
            new TThreadedSelectorServer.Args(new TNonblockingServerSocket(port))
                    .selectorThreads(selector)
                    .processor(processor)
                    .workerThreads(worker)
                    .protocolFactory(protocolFactory));

    startupHSServer();
    logger.info("Start server duration on port [" + port + "]: [" +
            TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - start) + "] seconds.");
    ttServer.serve();
}
 
Example #23
Source File: PacketStreamerServer.java    From floodlight_with_topoguard with Apache License 2.0 5 votes vote down vote up
/** 
 * The function to create a thrift Half-Sync and Half-Async Server.
 * @param processor
 */
public static void hshaServer(PacketStreamer.Processor<PacketStreamerHandler> processor) {
    try {
        TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(port);
        THsHaServer.Args args = new THsHaServer.Args(serverTransport);
        args.processor(processor);
        args.transportFactory(new TFramedTransport.Factory());
        args.protocolFactory(new TBinaryProtocol.Factory(true, true));
        TServer server = new THsHaServer(args);

        log.info("Starting the packetstreamer hsha server on port {} ...", port);
        server.serve();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #24
Source File: NonblockingThriftServerImpl.java    From ikasoa with MIT License 5 votes vote down vote up
/**
 * 初始化Thrift服务
 * 
 * @param serverTransport
 *            服务传输类型
 */
@Override
protected void initServer(TServerTransport serverTransport) {
	ThriftServerConfiguration configuration = getServerConfiguration();
	// 使用多线程半同步半异步方式
	TThreadedSelectorServer.Args args = new TThreadedSelectorServer.Args((TNonblockingServerSocket) serverTransport)
			.transportFactory(configuration.getTransportFactory())
			.protocolFactory(configuration.getProtocolFactory());
	if (ObjectUtil.isNotNull(configuration.getExecutorService()))
		args.executorService(configuration.getExecutorService());
	server = new TThreadedSelectorServer(
			configuration.getServerArgsAspect().tThreadedSelectorServerArgsAspect(args).processor(getProcessor()));
	if (ObjectUtil.isNotNull(configuration.getServerEventHandler()))
		server.setServerEventHandler(configuration.getServerEventHandler());
}
 
Example #25
Source File: ThriftTestingSource.java    From mt-flume with Apache License 2.0 5 votes vote down vote up
public ThriftTestingSource(String handlerName, int port) throws Exception {
  TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(new
    InetSocketAddress("0.0.0.0", port));
  ThriftSourceProtocol.Iface handler = null;
  if (handlerName.equals(HandlerType.OK.name())) {
    handler = new ThriftOKHandler();
  } else if (handlerName.equals(HandlerType.FAIL.name())) {
    handler = new ThriftFailHandler();
  } else if (handlerName.equals(HandlerType.ERROR.name())) {
    handler = new ThriftErrorHandler();
  } else if (handlerName.equals(HandlerType.SLOW.name())) {
    handler = new ThriftSlowHandler();
  } else if (handlerName.equals(HandlerType.TIMEOUT.name())) {
    handler = new ThriftTimeoutHandler();
  } else if (handlerName.equals(HandlerType.ALTERNATE.name())) {
    handler = new ThriftAlternateHandler();
  }

  server = new THsHaServer(new THsHaServer.Args
    (serverTransport).processor(
    new ThriftSourceProtocol.Processor(handler)).protocolFactory(
    new TCompactProtocol.Factory()));
  Executors.newSingleThreadExecutor().submit(new Runnable() {
    @Override
    public void run() {
      server.serve();
    }
  });
}
 
Example #26
Source File: rpcServer.java    From leaf-snowflake with Apache License 2.0 5 votes vote down vote up
public static void startRPCServer4(leafServer leafserver , String ip , int port) throws Exception
{
	TProcessor processor = new leafrpc.Processor<leafrpc.Iface>(new RPCService(leafserver));
	//传输通道,非阻塞模式
	InetSocketAddress address = new InetSocketAddress(InetAddress.getByName(ip),port);
	TNonblockingServerSocket serverTransport = new TNonblockingServerSocket(address,10000);
	THsHaServer.Args  args = new THsHaServer.Args(serverTransport);
	args.processor(processor);
	args.protocolFactory(new TBinaryProtocol.Factory());
	args.transportFactory(new TFramedTransport.Factory());
	TServer server = new THsHaServer(args);
	LOG.info("leaf RPCServer(type:THsHaServer) start at ip:port : "+ ip +":" + port );
	server.serve();
}
 
Example #27
Source File: rpcServer.java    From leaf-snowflake with Apache License 2.0 5 votes vote down vote up
public static void startRPCServer3(leafServer leafserver , String ip , int port) throws Exception
{
	TProcessor processor = new leafrpc.Processor<leafrpc.Iface>(new RPCService(leafserver));
	//传输通道,非阻塞模式
	InetSocketAddress address = new InetSocketAddress(InetAddress.getByName(ip),port);
	TNonblockingServerSocket serverTransport = new TNonblockingServerSocket(address,10000);
	TNonblockingServer.Args args = new TNonblockingServer.Args(serverTransport);
	args.protocolFactory(new TBinaryProtocol.Factory());
	args.transportFactory(new TFramedTransport.Factory());
	args.processorFactory(new TProcessorFactory(processor));
	TServer server = new TNonblockingServer(args);
	LOG.info("leaf RPCServer(type:TNonblockingServerSocket) start at ip:port : "+ ip +":" + port );
	server.serve();
}
 
Example #28
Source File: OracleServer.java    From fluo with Apache License 2.0 5 votes vote down vote up
private InetSocketAddress startServer() throws TTransportException {
  Preconditions.checkState(
      curatorFramework != null && curatorFramework.getState() == CuratorFrameworkState.STARTED);

  if (env.getConfiguration().containsKey(FluoConfigurationImpl.ORACLE_PORT_PROP)) {
    port = env.getConfiguration().getInt(FluoConfigurationImpl.ORACLE_PORT_PROP);
    Preconditions.checkArgument(port >= 1 && port <= 65535,
        FluoConfigurationImpl.ORACLE_PORT_PROP + " must be valid port (1-65535)");
  } else {
    port = PortUtils.getRandomFreePort();
  }
  InetSocketAddress addr = new InetSocketAddress(port);

  TNonblockingServerSocket socket = new TNonblockingServerSocket(addr);

  THsHaServer.Args serverArgs = new THsHaServer.Args(socket);
  TProcessor processor = new OracleService.Processor<OracleService.Iface>(this);
  serverArgs.processor(processor);
  serverArgs.maxReadBufferBytes = ORACLE_MAX_READ_BUFFER_BYTES;
  serverArgs.inputProtocolFactory(new TCompactProtocol.Factory());
  serverArgs.outputProtocolFactory(new TCompactProtocol.Factory());
  server = new THsHaServer(serverArgs);

  serverThread = new Thread(server::serve);
  serverThread.setDaemon(true);
  serverThread.start();

  return addr;
}
 
Example #29
Source File: Server.java    From rpc-benchmark with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws TTransportException {
	InetSocketAddress serverAddress = new InetSocketAddress("benchmark-server", 8080);

	TNonblockingServerTransport serverSocket = new TNonblockingServerSocket(serverAddress);
	TThreadedSelectorServer.Args serverParams = new TThreadedSelectorServer.Args(serverSocket);
	serverParams.protocolFactory(new TBinaryProtocol.Factory());
	serverParams.processor(new UserService.Processor<Iface>(new UserServiceThriftServerImpl()));
	TServer server = new TThreadedSelectorServer(serverParams);
	server.serve();
}
 
Example #30
Source File: ThriftTest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
private void startAsyncServer() throws Exception {
  final CustomHandler customHandler = new CustomHandler();
  final TProcessor customProcessor = new CustomService.Processor<CustomService.Iface>(customHandler);
  final TNonblockingServerSocket tnbSocketTransport = new TNonblockingServerSocket(port, 30000);
  final TNonblockingServer.Args tnbArgs = new TNonblockingServer.Args(tnbSocketTransport);
  tnbArgs.processor(customProcessor);

  server = new TNonblockingServer(tnbArgs);
  new Thread(new Runnable() {
    @Override
    public void run() {
      server.serve();
    }
  }).start();
}