@Override public void start() { logger.info("Starting thrift source"); ExecutorService sourceService; ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("Flume Thrift IPC Thread %d").build(); if (maxThreads == 0) { sourceService = Executors.newCachedThreadPool(threadFactory); } else { sourceService = Executors.newFixedThreadPool(maxThreads, threadFactory); } try { serverTransport = new TNonblockingServerSocket(new InetSocketAddress(bindAddress, port)); } catch (TTransportException e) { throw new FlumeException("Failed to start Thrift Source.", e); } THsHaServer.Args thhsArgs = new THsHaServer.Args(serverTransport); thhsArgs.processor(new ThriftSourceProtocol.Processor(new ThriftSourceHandler())); // thhsArgs.transportFactory(new TFramedTransport.Factory()); thhsArgs.protocolFactory(new TBinaryProtocol.Factory()); thhsArgs.executorService(sourceService); server = new THsHaServer(thhsArgs); // 半同步半异步的服务模型 servingExecutor = Executors.newSingleThreadExecutor( new ThreadFactoryBuilder().setNameFormat("Flume Thrift Source I/O Boss").build()); /** Start serving. */ servingExecutor.submit( new Runnable() { @Override public void run() { server.serve(); } }); long timeAfterStart = System.currentTimeMillis(); while (!server.isServing()) { try { if (System.currentTimeMillis() - timeAfterStart >= 10000) { throw new FlumeException("Thrift server failed to start!"); } TimeUnit.MILLISECONDS.sleep(1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new FlumeException("Interrupted while waiting for Thrift server" + " to start.", e); } } sourceCounter.start(); logger.info("Started Thrift source."); super.start(); }
public void startServer(final Integer port) throws Exception { TNonblockingServerSocket socket = new TNonblockingServerSocket(port); final PluggableSecurityTest.Processor<PluggableSecurityTest.Iface> processor = new PluggableSecurityTest.Processor<PluggableSecurityTest.Iface>(this); THsHaServer.Args args = new THsHaServer.Args(socket); args.processor(processor); args.transportFactory(new TFramedTransport.Factory()); args.protocolFactory(new TCompactProtocol.Factory()); server = new THsHaServer(args); Thread t = new Thread("thrift server") { public void run() { System.out.println("Starting server on port " + port + " ..."); server.serve(); } }; t.start(); }