Service Multiplexing using Apache Thrift

SHUVA JYOTI KAR picture SHUVA JYOTI KAR · Oct 27, 2013 · Viewed 7.5k times · Source

Server code:

            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();

Client Code:

    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));

But when I am running the server(listening on port 7911) and the client, the client doesnt process the last two calls to the add/multiply functions.

I could debug that the arguments have been sent to the server, but the server is not able to process them.

Any pointers as to what i am missing?

Answer

JensG picture JensG · Oct 29, 2013

Not tested, but it looks very much like as if you run two different protocol stacks on server and client. Try this:

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();