public static void main(String[] args) { int port = 9090; try { TServerTransport serverTransport = new TServerSocket(port); Args processor = new TThreadPoolServer.Args(serverTransport) .inputTransportFactory(new TFramedTransport.Factory()) .outputTransportFactory(new TFramedTransport.Factory()) .processor(new Processor<>(new TestThriftServiceHandler())); // processor.maxWorkerThreads = 20; TThreadPoolServer server = new TThreadPoolServer(processor); System.out.println("Starting the server..."); server.serve(); } catch (Exception e) { e.printStackTrace(); } }
@BeforeClass public static void setUp() { int port = 9090; try { TServerTransport serverTransport = new TServerSocket(port); Args processor = new TThreadPoolServer.Args(serverTransport) .inputTransportFactory(new TFramedTransport.Factory()) .outputTransportFactory(new TFramedTransport.Factory()) .processor(new Processor<>(new TestThriftServiceHandler())); // processor.maxWorkerThreads = 20; TThreadPoolServer server = new TThreadPoolServer(processor); logger.info("Starting test server..."); new Thread(server::serve).start(); Thread.sleep(1000); // waiting server init } catch (Exception e) { e.printStackTrace(); } }
public static void main(String[] args) { try { int port = 9119; TServerTransport serverTransport = new TServerSocket(port); Factory proFactory = new TBinaryProtocol.Factory(); Processor<Iface> processor = new Example.Processor<Example.Iface>(new Example.Iface() { @Override public void pong() throws TException { System.out.println("pong"); } @Override public void ping() throws TException { System.out.println("ping"); } }); Args thriftArgs = new Args(serverTransport); thriftArgs.processor(processor); thriftArgs.protocolFactory(proFactory); TServer tserver = new TThreadPoolServer(thriftArgs); System.out.println("启动监听:" + port); tserver.serve(); } catch (TTransportException e) { e.printStackTrace(); } }
/** * Creates the server. * * @param serverTransport the server transport * @param processor the processor * @return the t server */ public TServer createServer(TServerTransport serverTransport, TMultiplexedProcessor processor) { TThreadPoolServer.Args args = new Args(serverTransport).processor(processor); args.stopTimeoutVal = 3; args.stopTimeoutUnit = TimeUnit.SECONDS; SynchronousQueue<Runnable> executorQueue = // NOSONAR new SynchronousQueue<Runnable>(); executorService = new ThreadPoolExecutor(args.minWorkerThreads, args.maxWorkerThreads, 60, TimeUnit.SECONDS, executorQueue); args.executorService = executorService; return new TThreadPoolServer(args); }
@Override public void run() { LOG.info("Initializing Thrift Service for Bootstrap Server...."); LOG.info("thrift host: {}", thriftHost); LOG.info("thrift port: {}", thriftPort); try { TMultiplexedProcessor processor = new TMultiplexedProcessor(); BootstrapThriftService.Processor<BootstrapThriftService.Iface> bootstrapProcessor = new BootstrapThriftService.Processor<BootstrapThriftService.Iface>( bootstrapThriftService); processor.registerProcessor(KaaThriftService.BOOTSTRAP_SERVICE.getServiceName(), bootstrapProcessor); TServerTransport serverTransport = new TServerSocket(new InetSocketAddress(thriftHost, thriftPort)); server = new TThreadPoolServer(new Args(serverTransport).processor(processor)); LOG.info("Bootstrap test Server {}:{} Started.", thriftHost, thriftPort); synchronized (startSync) { startComplete = true; startSync.notify(); } server.serve(); LOG.info("Bootstrap test Server {}:{} Stopped.", thriftHost, thriftPort); } catch (TTransportException e) { LOG.error("TTransportException", e); } finally { synchronized (stopSync) { stopComplete = true; bootstrapThriftService.reset(); stopSync.notify(); } } }
public Server(int _port) throws Exception { this.port = _port; org.apache.thrift.protocol.TBinaryProtocol.Factory protoFactory = new TBinaryProtocol.Factory(true, true); TServerTransport serverTransport = new TServerSocket(port); DemoService.Processor processor = new DemoService.Processor<Iface>(this); tr_server = new TThreadPoolServer(new Args(serverTransport).processor(processor) .protocolFactory(protoFactory)); }
/** * @param args */ @SuppressWarnings("unchecked") public static void main(String[] args) { try{ /* TNonblockingServerSocket socket = new TNonblockingServerSocket(PORT); final UserService.Processor processor = new UserService.Processor<Iface>(new UserServerHandler()); THsHaServer.Args arg = new THsHaServer.Args(socket); */ /* org.apache.thrift.protocol.TBinaryProtocol.Factory protoFactory = new TBinaryProtocol.Factory(true, true); TServerTransport serverTransport = new TServerSocket(PORT); UserService.Processor processor = new UserService.Processor<Iface>(new UserServerHandler()); THsHaServer.Args arg = new THsHaServer.Args((TNonblockingServerTransport) serverTransport); arg.protocolFactory(new TCompactProtocol.Factory()); arg.transportFactory(new TFramedTransport.Factory()); arg.processorFactory(new TProcessorFactory(processor)); */ TServerSocket serverTransport = new TServerSocket(PORT); UserService.Processor processor = new UserService.Processor(new UserServerHandler()); Factory protFactory = new TBinaryProtocol.Factory(true, true); Args arg = new Args(serverTransport); arg.processor(processor); arg.protocolFactory(protFactory); TServer server = new TThreadPoolServer(arg); // TServer server = new THsHaServer(arg); System.out.println("service begin..."); server.serve(); }catch(Exception e) { e.printStackTrace(); System.out.println("UserServer.java main function"); } }
/** * Start thrift server */ @SuppressWarnings("unchecked") private void StartServer() { try{ String DBName = "cadalrectest-77"; TServerSocket serverTransport = new TServerSocket(7911); RecAPI.Processor processor = new RecAPI.Processor(new RecAPIImp(DBName)); Factory factory = new TBinaryProtocol.Factory(true, true); Args args = new Args(serverTransport); args.processor(processor); args.protocolFactory(factory); TServer server = new TThreadPoolServer(args); System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); System.out.println("!!! CADAL new recommendation service is started !!!"); System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); server.serve(); }catch(Exception e){ e.printStackTrace(); System.out.println("-------------------------------------"); System.out.println("--- Thrift service can not start! ---"); System.out.println("-------------------------------------"); } }
public void startServer(){ try { /* serverTransport = new TServerSocket(8585); Args args = new Args(serverTransport); Factory portFactory = new TBinaryProtocol.Factory(true, true); args.protocolFactory(portFactory); Recommend.Processor process=new Processor(new RecommendServer()); args.processor(process); TServer server = new TThreadPoolServer(args); */ TServerSocket serverTransport = new TServerSocket(8585); Recommend.Processor processor = new Recommend.Processor(new RecommendServer()); Factory protFactory = new TBinaryProtocol.Factory(true, true); Args args = new Args(serverTransport); args.processor(processor); args.protocolFactory(protFactory); TServer server = new TThreadPoolServer(args); server.serve(); } catch (TTransportException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
protected ThriftServerInfo startServer() throws Throwable { // 获取一个监听端口 final int port = choseListenPort(); ThriftServerInfo serverInfo = new ThriftServerInfo(LOACLHOST, port); final AtomicReference<Throwable> ex = new AtomicReference<Throwable>(); Thread runner = new Thread("thrift-server-starter") { @Override public void run() { try { TServerTransport serverTransport = new TServerSocket(port); Factory proFactory = new TBinaryProtocol.Factory(); Processor<Iface> processor = new Example.Processor<Example.Iface>(new Example.Iface() { @Override public void pong() throws TException { logger.info("pong"); } @Override public void ping() throws TException { logger.info("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; }
protected ThriftServerInfo startMulitServiceServer() throws Throwable { // 获取一个监听端口 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; }
/** * Before test. * * @throws Exception the exception */ @Before public void beforeTest() throws Exception { if (!thriftServerStarted) { CliThriftService.Processor<CliThriftService.Iface> cliProcessor = new CliThriftService.Processor<CliThriftService.Iface>( new TestCliThriftService(THRIFT_SERVER_SHORT_NAME)); TMultiplexedProcessor processor = new TMultiplexedProcessor(); processor.registerProcessor(KaaThriftService.KAA_NODE_SERVICE.getServiceName(), cliProcessor); TServerTransport serverTransport = new TServerSocket( new InetSocketAddress(HOST, PORT)); server = new TThreadPoolServer( new Args(serverTransport).processor(processor)); thriftServerThread = new Thread(new Runnable() { @Override public void run() { LOG.info("Thrift Server started."); server.serve(); LOG.info("Thrift Server stopped."); } }); thriftServerThread.start(); Thread.sleep(100); thriftServerStarted = true; } cliSession = new CliSessionState(); cliSession.in = System.in; systemOut = new ByteArrayOutputStream(); PrintStream out = new PrintStream(systemOut, true, "UTF-8"); System.setOut(out); systemErr = new ByteArrayOutputStream(); PrintStream err = new PrintStream(systemErr, true, "UTF-8"); System.setErr(err); cliSession.out = System.out; cliSession.err = System.err; CliSessionState.start(cliSession); }
public static void main(String[] args) throws Exception { TServerTransport serverTransport = new TServerSocket(9090); TestSrv.Processor<TestSrvImpl> processor = new TestSrv.Processor<>(new TestSrvImpl()); Args arg = new TThreadPoolServer.Args(serverTransport).processor(processor); TServer server = new TThreadPoolServer(arg); server.serve(); }
@Override public void run() { LOG.info("Initializing Thrift Service for Operations Server...."); LOG.info("thrift host: {}", thriftHost); LOG.info("thrift port: {}", thriftPort); registerZK(); try { TMultiplexedProcessor processor = new TMultiplexedProcessor(); OperationsThriftService.Processor<OperationsThriftService.Iface> operationsProcessor = new OperationsThriftService.Processor<OperationsThriftService.Iface>( operationsThriftService); processor.registerProcessor(KaaThriftService.OPERATIONS_SERVICE.getServiceName(), operationsProcessor); TServerTransport serverTransport = new TServerSocket(new InetSocketAddress(thriftHost, thriftPort)); server = new TThreadPoolServer(new Args(serverTransport).processor(processor)); LOG.info("Operations Server {}:{} Started.", thriftHost, thriftPort); server.serve(); LOG.info("Operations Server {}:{} Stopped.", thriftHost, thriftPort); } catch (TTransportException e) { LOG.error("TTransportException", e); } }