java Thrift TThreadPoolServer 多个processor 的实现

当我们使用Thrift 通信的时候,服务端有时候需要注册多个类,去实现通信,这时候我们就不能再使用单一Processor的方式,就要使用多个Processor,那么如何去实现呢?

多个Process

服务端

public static void main(String[] args) {
        try {
            AImpl aService = new AImpl();
            BImpl bService=new BImpl();
            TMultiplexedProcessor multiplexedProcessor = new TMultiplexedProcessor();


            AService.Processor<AImpl> aProcessor = new AService.Processor<>(aService);
            multiplexedProcessor.registerProcessor("aService", aProcessor);


            BService.Processor<BImpl> bProcessor = new BService.Processor<>(bService);
            multiplexedProcessor.registerProcessor("bService", bProcessor);


            TServerSocket serverTransport = new TServerSocket(80000);
            TThreadPoolServer.Args serverArgs = new TThreadPoolServer.Args(serverTransport);
            serverArgs.processor(multiplexedProcessor);


            TServer server = new TThreadPoolServer(serverArgs);


            System.out.println("Starting the multi-processor server...");
            server.serve();

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }

客户端

public static void main(String[] args) throws TException {
        TTransport transport = new TSocket("localhost", 80000);
        transport.open();

       // AService
        TMultiplexedProtocol multiplexedProtocol = new TMultiplexedProtocol(new TBinaryProtocol(transport), "aService");
        AService.Client aClient = new AService.Client(multiplexedProtocol);
        aClient.method();
        System.out.println("Calling AService method...");


        // BService
        multiplexedProtocol = new TMultiplexedProtocol(new TBinaryProtocol(transport), "bService");
        BService.Client bClient = new BService.Client(multiplexedProtocol);
        BClient.method();
        System.out.println("Calling SystemLogService method...");

        transport.close();

    }

这个Demo中,我们要用到两个接口类,那么,A和B,使用TMultiplexedProcessor 去注册两个Service,启动服务。

单个Process

服务端

            AImpl aService = new AImpl();
            TServerSocket serverSocket = new TServerSocket(90000);

            AService.Processor<AImpl> aProcessor
                    = new AService.Processor<>(aService);
            TThreadPoolServer.Args serverArg = new TThreadPoolServer.Args(serverSocket);
            serverArg.processor(aProcessor);
          
            TThreadPoolServer server = new TThreadPoolServer(serverArg);
            server.serve();

客户端

 TTransport transport = new TSocket("localhost", 90000);
        transport.open();
        TBinaryProtocol protocol = new TBinaryProtocol(transport);
        AService.Client aClient = new AService.Client(protocol);
        aclient.method();

附单个process的方式。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

草莓味少女vv

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值