Search code examples
thriftoutputstream

Writing to output stream in Thrift outside TServer?


I am struggling to find Thrift documentation that explains the process of writing to a socket, outside of going through an interface method.

If I want a server to write out data without requiring an initiating request by the client, which components of the Thrift Library should I be looking at? Are there good examples of how to do this somewhere that I'm missing? Everything seems to rely on using the TServer class.

Instinct from looking at http://thrift.apache.org/docs/concepts/ suggests it's the TProcessor class I'm interested in? But having trouble finding examples demonstrating good use of it.


Solution

  • As soon as you have a TProtocol instance established over TTransport, you can write any Thrift structure in it. This is only a couple lines of code:

    TTransport trans = ....;
    TProtocol prot = ....;
    MyStruct myStruct = ....; //Any thrift structure that extends TBase
    myStruct.write(prot);
    trans.flush();
    

    Reading serializied structure is easy too:

    TProtocol prot = ....;
    MyStruct myStruct = new MyStruct();
    myStruct.read(prot);
    

    The names of classes/method semantic might change depending on the implementation language, but the general idea is the same for all languages.

    For the examples I'd suggest to look on the server code generated by Thrift compiler and TSerializer java class.