Search code examples
javathrift

How to use layered thrift transports in a java server


I want to use the following thrift transports on top of each other.

  • layered transport: zlib transport
  • layered transport: framed transport
  • endpoint transport: socket

I can only set 1 transport factory in a server, but actually, I have to set the zlib factory and the framed factory. For instance:

new TServer.Args(new TServerSocket(port)).transportFactory(new TFramedTransport.Factory());

How can I set both transports on the server side in Java?


Solution

  • You could create a custom factory that creates the nested transports:

      public class MyNestedFactory extends TTransportFactory {
        @Override
        public TTransport getTransport(TTransport base) {
          return new TZlibTransport(new TFramedTransport(base));
        }
      }
    

    You can then pass this factory to your server args:

    new TServer.Args(new TServerSocket(port))
                  .transportFactory(new MyNestedFactory())