Java Code Examples for org.apache.thrift.server.TServer#serve()

The following examples show how to use org.apache.thrift.server.TServer#serve() . 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: DKMLServer.java    From dk-fitting with Apache License 2.0 6 votes vote down vote up
/**
 * 阻塞式、多线程处理
 *
 * @param args
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String[] args) {
    try {
        int port = Integer.valueOf(prop.get("dkml.port"));
        //设置传输通道,普通通道
        TServerTransport serverTransport = new TServerSocket(port);
        //使用高密度二进制协议
        TProtocolFactory proFactory = new TCompactProtocol.Factory();
        //设置处理器
        TProcessor processor = new DKML.Processor(new DKMLImpl());
        //创建服务器
        TServer server = new TThreadPoolServer(
                new TThreadPoolServer.Args(serverTransport)
                        .protocolFactory(proFactory)
                        .processor(processor)
        );

        System.out.println("Start server on port "+ port +"...");
        server.serve();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 3
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 4
Source File: ThriftServerApplication.java    From spring-boot-demo with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    SpringApplication.run(ThriftServerApplication.class, args);

    try {
        System.out.println("服务端开启....");
        TProcessor tprocessor = new Hello.Processor<Hello.Iface>(new HelloServiceImpl());
        // 简单的单线程服务模型
        TServerSocket serverTransport = new TServerSocket(9898);
        TServer.Args tArgs = new TServer.Args(serverTransport);
        tArgs.processor(tprocessor);
        tArgs.protocolFactory(new TBinaryProtocol.Factory());
        TServer server = new TSimpleServer(tArgs);
        server.serve();
    } catch (TTransportException e) {
        e.printStackTrace();
    }
}
 
Example 5
Source File: DKSearchService.java    From dk-fitting with Apache License 2.0 6 votes vote down vote up
/**
 *  线程池服务模型,使用标准的阻塞式IO,预先创建一组线程处理请求。
 */
public void startTThreadPoolServer() {
    try {
        System.out.println("UserInfoServiceDemo TThreadPoolServer start ....");
        TMultiplexedProcessor processor = new TMultiplexedProcessor();
        processor.registerProcessor( "DKSearchOutput",new DKSearchOutput.Processor<DKSearchOutput.Iface>( new DKSearchOutputImpl() ) );
        processor.registerProcessor( "DKSearchInput",new DKSearchInput.Processor<DKSearchInput.Iface>(new DKSearchInputImpl()  ) );
        processor.registerProcessor( "DKSearchService",new SearchService.Processor<SearchService.Iface>(new SearchServiceImpl() ) );
        //TProcessor tprocessor = new UserInfoService.Processor<UserInfoService.Iface>(new UserInfoServiceImpl());
        TServerSocket serverTransport = new TServerSocket( Integer.valueOf(prop.get("dkSearch.port")));
        TThreadPoolServer.Args ttpsArgs = new TThreadPoolServer.Args(serverTransport);
        ttpsArgs.processor(processor);
        ttpsArgs.protocolFactory(new TBinaryProtocol.Factory());
        // 线程池服务模型,使用标准的阻塞式IO,预先创建一组线程处理请求。
        TServer server = new TThreadPoolServer(ttpsArgs);
        server.serve();
    } catch (Exception e) {
        System.out.println("Server start error!!!");
        e.printStackTrace();

    }

}
 
Example 6
Source File: DKSearchService.java    From dk-fitting with Apache License 2.0 6 votes vote down vote up
/**
 *
 * 简单的单线程模式
 *
 */
public  void  startService(){
    System.out.println( " start ...." );
    //单接口声明
    //TProcessor tProcessor=new DKSearchOutput.Processor<DKSearchOutput.Iface>( new DKSearchOutputImpl() );
    //多接口声明
    TMultiplexedProcessor processor = new TMultiplexedProcessor();
    processor.registerProcessor( "DKSearchOutput",new DKSearchOutput.Processor<DKSearchOutput.Iface>( new DKSearchOutputImpl() ) );
    processor.registerProcessor( "DKSearchInput",new DKSearchInput.Processor<DKSearchInput.Iface>(new DKSearchInputImpl()  ) );
    processor.registerProcessor( "DKSearchService",new SearchService.Processor<SearchService.Iface>(new SearchServiceImpl() ) );
    try {
        TServerSocket serverSocket=new TServerSocket( Integer.valueOf(prop.get("dkSearch.port")) );
        TServer.Args targs=new TServer.Args( serverSocket );
        targs.processor( processor );
        //二进制TBinaryProtocol
        //二进制高密度TCompactProtocol
        targs.protocolFactory( new TCompactProtocol.Factory(  ) );
        TServer tServer=new TSimpleServer( targs );
        tServer.serve();
    } catch (TTransportException e) {
        System.err.println("Service Start error");
        e.printStackTrace();
    }
}
 
Example 7
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 8
Source File: ThriftExample.java    From pragmatic-java-engineer with GNU General Public License v3.0 5 votes vote down vote up
public static void testRpcServer() throws TTransportException {
    TProcessor tprocessor = new TestService.Processor<TestService.Iface>(new TestServiceImpl());

    TServerSocket serverTransport = new TServerSocket(8088);
    TServer.Args tArgs = new TServer.Args(serverTransport);
    tArgs.processor(tprocessor);
    tArgs.protocolFactory(new TBinaryProtocol.Factory());

    // 简单的单线程服务模型
    TServer server = new TSimpleServer(tArgs);
    server.serve();
}
 
Example 9
Source File: ServiceProvider.java    From ourea with Apache License 2.0 5 votes vote down vote up
/**
 * 启动thrift server,如果存在server容器或者多个service,则一些需要设置为daemon模式
 */
private void startServer() throws Exception {

    TServerSocket serverTransport = new TServerSocket(serverConfig.getPort());
    TThreadPoolServer.Args args = new TThreadPoolServer.Args(serverTransport);
    args.maxWorkerThreads = serverConfig.getMaxWorkerThreads();
    args.minWorkerThreads = serverConfig.getMinWorkerThreads();
    args.protocolFactory(new TBinaryProtocol.Factory());

    TProcessor tProcessor = getProcessorIface(getIfaceClass());
    args.processor(tProcessor);
    final TServer server = new TThreadPoolServer(args);
    server.setServerEventHandler(serverConfig.getServerEventHandler());
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            server.serve();
        }
    });
    thread.setDaemon(serverConfig.isDaemonRun());
    thread.start();
    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        @Override public void run() {
            if (!serverConfig.isDirectInvoke()) {
                unZkRegister(providerInfo, serviceInfo);
            }
            server.stop();
        }
    }));

    LOGGER.info("----------------start thrift server--------------");
}
 
Example 10
Source File: ExampleServer.java    From Thrift-Connection-Pool with Apache License 2.0 5 votes vote down vote up
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();
	}
}
 
Example 11
Source File: AMServer.java    From jstorm with Apache License 2.0 5 votes vote down vote up
public void simple(JstormAM.Processor processor) {
    try {
        TServerTransport serverTransport = new TServerSocket(port);
        TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));
        LOG.info("Starting the simple server...");

        server.serve();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 12
Source File: FactoryServer.java    From ThriftBook with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws TTransportException {
  TServerSocket trans_svr = new TServerSocket(9090);
  TProcessor proc = new Message.Processor<>(new MessageHandler());
  
  TServer server = new TThreadPoolServer(
          new TThreadPoolServer.Args(trans_svr)
            .processor(proc)
            .protocolFactory(new TJSONProtocol.Factory())
            .inputTransportFactory(new TFramedTransport.Factory())
            .outputTransportFactory(new TWritelogTransportFactory(100)));
  server.serve();
}
 
Example 13
Source File: HelloServer.java    From ThriftBook with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws TTransportException {
    TServerSocket trans_svr = new TServerSocket(9090);
    TProcessor proc = new helloSvc.Processor<>(new MessageHandler());
    TServer server =
            new TSimpleServer(
                    new TSimpleServer.Args(trans_svr)
                            .processor(proc)
            );
    System.out.println("[Server] waiting for connections");
    server.serve();
}
 
Example 14
Source File: DKGraphxServer.java    From dk-fitting with Apache License 2.0 5 votes vote down vote up
/**
 * 阻塞式、多线程处理
 *
 * @param args
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String[] args) {
    try {
        int port = Integer.valueOf(prop.get("dkgraphx.port"));
        //设置传输通道,普通通道
        TServerTransport serverTransport = new TServerSocket(port);

        //使用高密度二进制协议
        TProtocolFactory proFactory = new TCompactProtocol.Factory();

        //设置处理器
        TProcessor processor = new DKGraphx.Processor(new DKGraphxImpl());

        //创建服务器
        TServer server = new TThreadPoolServer(
                new TThreadPoolServer.Args(serverTransport)
                        .protocolFactory(proFactory)
                        .processor(processor)
        );

        System.out.println("Start server on port "+ port +"...");
        server.serve();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 15
Source File: BasicAbstractTest.java    From Thrift-Connection-Pool with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 16
Source File: BasicAbstractTest.java    From Thrift-Connection-Pool with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 17
Source File: SimpleServer.java    From ThriftBook with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws TTransportException {
    TServerSocket trans_svr = new TServerSocket(9090);
    TProcessor proc = new SocialLookup.Processor<>(new SocialLookupHandler());
    TServer server = new TSimpleServer(new TSimpleServer.Args(trans_svr).processor(proc));
    server.serve();
}
 
Example 18
Source File: ThreadedServer.java    From ThriftBook with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws TTransportException {
    TServerSocket trans_svr = new TServerSocket(9090);
    TProcessor proc = new Message.Processor<>(new MessageHandler());
    TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(trans_svr).processor(proc));
    server.serve();
}
 
Example 19
Source File: DataProcess.java    From dk-fitting with Apache License 2.0 2 votes vote down vote up
public void startServer() {

        try {
            System.out.println("Start server on port "+ SERVER_PORT +"...");

            // 简单的单线程服务模型,一般用于测试
            TServerSocket serverTransport = new TServerSocket(SERVER_PORT);

            TMultiplexedProcessor processor = new TMultiplexedProcessor();


            DataAnalysisService.Processor<DataAnalysisService.Iface> tprocessor =
                    new DataAnalysisService.Processor<>(new DataAnalysisImpl());
            processor.registerProcessor("DataAnalysisService", tprocessor);


            DataStaticKerberosService.Processor<DataStaticKerberosService.Iface> tprocessor1 =
                    new DataStaticKerberosService.Processor<DataStaticKerberosService.Iface>(new DataStaticKerberosImpl());
            processor.registerProcessor("DataStaticKerberosService", tprocessor1);

            DataStaticService.Processor<DataStaticService.Iface> tprocessor2 =
                    new DataStaticService.Processor<DataStaticService.Iface>(new DataStaticImpl());
            processor.registerProcessor("DataStaticService", tprocessor2);


            DedupeKerberosService.Processor<DedupeKerberosService.Iface> tprocessor3 =
                    new DedupeKerberosService.Processor<DedupeKerberosService.Iface>(new DedupeKerberosImpl());
            processor.registerProcessor("DedupeKerberosService", tprocessor3);


            DedupeService.Processor<DedupeService.Iface> tprocessor4 =
                    new DedupeService.Processor<DedupeService.Iface>(new DedupeImpl());
            processor.registerProcessor("DedupeService", tprocessor4);

            FormatFieldService.Processor<FormatFieldService.Iface> tprocessor5 =
                    new FormatFieldService.Processor<FormatFieldService.Iface>(new FormatFieldImpl());
            processor.registerProcessor("FormatFieldService", tprocessor5);


            FormatRecService.Processor<FormatRecService.Iface> tprocessor6 =
                    new FormatRecService.Processor<FormatRecService.Iface>(new FormatRecImpl());
            processor.registerProcessor("FormatRecService", tprocessor6);

            SelectFieldService.Processor<SelectFieldService.Iface> tprocessor7 =
                    new SelectFieldService.Processor<SelectFieldService.Iface>(new SelectFieldImlp());
            processor.registerProcessor("SelectFieldService", tprocessor7);

            SelectRecService.Processor<SelectRecService.Iface> tprocessor8 =
                    new SelectRecService.Processor<SelectRecService.Iface>(new SelectRecImpl());
            processor.registerProcessor("SelectRecService", tprocessor8);


            DataCleanKerberosService.Processor<DataCleanKerberosService.Iface> tprocessor9 =
                    new DataCleanKerberosService.Processor<DataCleanKerberosService.Iface>(new DataCleanKerberosImpl());
            processor.registerProcessor("DataCleanKerberosService", tprocessor9);

            TThreadPoolServer.Args ttpsArgs = new TThreadPoolServer.Args(serverTransport);
            ttpsArgs.processor(processor);
            ttpsArgs.protocolFactory(new TBinaryProtocol.Factory());

            // 线程池服务模型,使用标准的阻塞式IO,预先创建一组线程处理请求。
            TServer server = new TThreadPoolServer(ttpsArgs);
            server.serve();



        } catch (Exception e) {
            System.out.println("Server start error!!!");
            e.printStackTrace();
        }


    }
 
Example 20
Source File: DKStreamDataServer.java    From dk-fitting with Apache License 2.0 2 votes vote down vote up
public void startServer() {

        try {
            System.out.println("Server start ....");


            // 简单的单线程服务模型,一般用于测试
            TServerSocket serverTransport = new TServerSocket(  SERVER_PORT  );
            TMultiplexedProcessor processor = new TMultiplexedProcessor();



            //注册一个服务接口
            DKStreamProducerService.Processor<DKStreamProducerServiceImpl> pro =
                    new DKStreamProducerService.Processor<DKStreamProducerServiceImpl>(new DKStreamProducerServiceImpl());
            processor.registerProcessor("DKStreamProducerService", pro);

            //注册一个服务接口
            JavaKafkaConsumerHighAPIESService.Processor<JavaKafkaConsumerHighAPIESImpl> pro1 =
                    new JavaKafkaConsumerHighAPIESService.Processor<JavaKafkaConsumerHighAPIESImpl>(new JavaKafkaConsumerHighAPIESImpl());
            processor.registerProcessor("ESService", pro1);



            //注册一个服务接口

            JavaKafkaConsumerHighAPIHdfsService.Processor<JavaKafkaConsumerHighAPIHdfsImpl> pro3 =
                    new JavaKafkaConsumerHighAPIHdfsService.Processor<>(new JavaKafkaConsumerHighAPIHdfsImpl());
            processor.registerProcessor("JavaKafkaConsumerHighAPIHdfsService", pro3);


            TThreadPoolServer.Args ttpsArgs = new TThreadPoolServer.Args(serverTransport);
            ttpsArgs.processor(processor);
            ttpsArgs.protocolFactory(new TBinaryProtocol.Factory());
            // 线程池服务模型,使用标准的阻塞式IO,预先创建一组线程处理请求。
            TServer server = new TThreadPoolServer(ttpsArgs);
            server.serve();


        } catch (Exception e) {
            System.out.println("Server start error!!!");
            e.printStackTrace();
        }


    }