Can generate .thrift files from existing java / scala interfaces and data types?

Is there an easy way to use existing Java / scala data types and APIs and create corresponding .thrift files? Creating Thrift using server - side data structures is overly invasive because it has consequences:

  • I cannot annotate my data structures (e.g. for XML, JSON, hibernation, ...)
  • this template conflicts with other serialization structures that want to own or require changes to my source files.

As a result, it seems that it takes on the role of an exclusive persistence format for my server - unless I create a wrapper to sort the data around Thrift or my other save formats that deal with these data structures (hibernate, Jackson, scala BeanProperty, ...). However, this aims at an automated tool for collecting data, such as frugality, and leads directly to a world prone to errors, to maintaining identical but shared interfaces and data structures (= waste of talented engineering time and energy).

I am completely satisfied with the client code creating the Thrift auto-generation. However, I (strongly) believe that I need the freedom to edit the data structures that my server works with in the API.

+4
source share
3 answers

You can use Swift .

Shortly speaking; annotate your classes and interfaces (structures and services in Trrift). You can then either run the Swift client / server code, or use the swift2thrift generator to create the equivalent IDL and use the Thrift compiler to generate the clients (the latter is what I recommend for what you are describing).

Once this is done to create a TProcessor that you can use in TServlet with regular TProtocol / TTransport objects, do something similar in your init () servlet:

protected void addProcessor(String name, Object svc) {
    ThriftCodecManager codecManager = new ThriftCodecManager(
        new CompilerThriftCodecFactory(false)
    );
    List<ThriftEventHandler> eventList = Collections.emptyList();
    ThriftServiceProcessor proc = new ThriftServiceProcessor(codecManager, eventList, svc);
    this.processors.put(name, proc);
    this.multiplex.registerProcessor(name, NiftyProcessorAdapters.processorToTProcessor(proc));
}

The multiplex instance variable in this example is an instance TMultiplexedProcessorof libthrift.jar.

Then just do it in your doPost ():

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    getServletContext().log("entering doPost()");
    TTransport inTransport = null;
    TTransport outTransport = null;
    try {

        InputStream in = request.getInputStream();
        OutputStream out = response.getOutputStream();

        TTransport transport = new TIOStreamTransport(in, out);
        inTransport = transport;
        outTransport = transport;

        TProtocol inProtocol = getInProtocolFactory().getProtocol(inTransport);
        TProtocol outProtocol = getOutProtocolFactory().getProtocol(outTransport);

        if (multiplex.process(inProtocol, outProtocol)) {
            out.flush();
        } else {
            throw new ServletException("multiplex.process() returned false");
        }
    } catch (TException te) {
        throw new ServletException(te);
    } finally {
        if (inTransport != null) {
            inTransport.close();
        }
        if (outTransport != null) {
            outTransport.close();
        }
    }
}

FYI - TJSONProtocol Swift 0.14, , .

... Swift final... JPA , final... , Eclipselink, YMMV

+5

Java: - Xtext, TRLIT IDL DSL. Xtext/Xtend Java, , . , , .

, , [...]

, .

() , , API.

. , : 1). , , . , , .

. / , , PITA . 2) , . 3)

, " ", .


(1) . (2) , DSL, . (3) , , YAGNI. , .

+3

. Thrift , : , ?

0

All Articles