使用Apache Thrift进行服务多路复用

时间:2013-10-27 04:32:34

标签: java service thrift multiplexing

服务器代码:

            TMultiplexedProcessor processor = new TMultiplexedProcessor();

            processor.registerProcessor(
                "AddService",
                new AddService.Processor(new AddHandler()));

            processor.registerProcessor(
                "MultiplyService",
                new MultiplyService.Processor(new MultiplyHandler()));

            TServerTransport serverTransport = new TServerSocket(7911);



            TSimpleServer server = new TSimpleServer(new TSimpleServer.Args(serverTransport).
                   processor(processor));

            System.out.println("Starting server on port 7911 ...");
            server.serve();

客户代码:

    TFramedTransport transport;

   transport = new TFramedTransport(new TSocket("localhost", 7911));  
   transport.open();  


   TProtocol protocol = new TBinaryProtocol(transport);

   System.out.println("1");
   TMultiplexedProtocol mp = new TMultiplexedProtocol(protocol, "AddService");
   AddService.Client service = new AddService.Client(mp);

   System.out.println("2");
   TMultiplexedProtocol mp2 = new TMultiplexedProtocol(protocol, "MultiplyService");
   MultiplyService.Client service2 = new MultiplyService.Client(mp2);

   System.out.println("3");

   System.out.println(service.add(2,2));
   System.out.println(service2.multiply(2000,200));

但是当我运行服务器(侦听端口7911)和客户端时,客户端不处理对add / multiply函数的最后两次调用。

我可以调试参数已经发送到服务器,但是服务器无法处理它们。

关于我缺少什么的任何指示?

1 个答案:

答案 0 :(得分:3)

未经测试,但它看起来非常像在服务器和客户端上运行两个不同的协议栈。试试这个:

TMultiplexedProcessor processor = new TMultiplexedProcessor();

processor.registerProcessor(
    "AddService",
    new AddService.Processor(new AddHandler()));

processor.registerProcessor(
    "MultiplyService",
    new MultiplyService.Processor(new MultiplyHandler()));

TServerTransport serverTransport = new TServerSocket(7911);

TTransportFactory factory = new TFramedTransport.Factory();

TServer.Args args = new TServer.Args(serverTransport);
args.processor(processor);
args.transportFactory(factory);
TSimpleServer server = new TSimpleServer(args);

System.out.println("Starting server on port 7911 ...");
server.serve();