可以从现有的Java/scala接口和数据类型生成.thrift文件吗? [英] Can generate .thrift files from existing java/scala interfaces and data types?

查看:41
本文介绍了可以从现有的Java/scala接口和数据类型生成.thrift文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法来获取现有的Java/scala数据类型和API接口并生成相应的.thrift文件?让Thrift生成服务器数据结构具有过度侵入性,因为它具有以下后果:

Is there an easy way to take existing Java/scala datatypes and API interfaces and produce corresponding .thrift files? Having Thrift generate server data structures is excessively invasive as it has the consequences:

  • 我无法注释我的数据结构(例如,用于XML,JSON,休眠持久性等)
  • 此模式与其他想要拥有或需要修改我的源文件的序列化框架相冲突.

因此,它似乎像节俭一样将自己变成了我的服务器的专有持久性格式-除非,也就是说,我围绕Thrift或其他处理这些数据结构的持久性格式创建了数据编组包装器(休眠,杰克逊,scala BeanProperty等).但是,这违背了诸如节俭之类的自动化数据编组工具的目的,并直接导致易于出错的世界,即必须维护相同但分开的界面和数据结构(=浪费了有才华的工程师时间和精力).

As a result, its seems like thrift forces itself into being the exclusive persistence format for my server -- unless, that is, I create a data-marshalling wrapper around Thrift or the other my persistence formats that deal with these data structures (hibernate, Jackson, scala BeanProperty, ...). However, this defeats the purpose of an automated data-marshalling tool such as thrift and leads straight to the error-prone world of having to maintain identical-but-separate interfaces and data-structures (= waste of talented engineer time and energy).

我对Thrift自动生成客户端代码完全满意.但是,我(强烈)觉得我需要自由地编辑服务器在API中处理的数据结构.

I'm totally happy with Thrift auto-generating client code. However, I (strongly) feel that I need the freedom to edit the data structures my server deals with in the APIs.

推荐答案

您可以使用 Swift .

简而言之:注释您的类和接口(用Thrift的话来说就是结构和服务).然后,您可以运行Swift的客户端/服务器代码,也可以使用swift2thrift生成器生成等效的IDL,并使用Thrift编译器生成客户端(后者是我所建议的描述).

To make a long story short; annotate your classes and interfaces (structs and services in Thrift parlance). Then you can either run Swift's client/server code or you can use the swift2thrift generator to produce equivalent IDL and use the Thrift compiler to generate clients (the latter is what I recommend for what you're describing).

完成创建可在带有普通TProtocol/TTransport对象的TServlet中使用的TProcessor之后,请在servlet的init()中执行以下操作:

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

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

此示例中的Multiplex实例变量是 libthrift.jar 中的 TMultiplexedProcessor 的实例.

The multiplex instance variable in this example is an instance of TMultiplexedProcessor from libthrift.jar.

然后只需在您的doPost()中执行此操作:

Then just do this 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();
        }
    }
}

仅供参考-TJSONProtocol不适用于0.14之前的Swift版本,因此,如果需要使用TJSONProtocol,则需要从源代码进行构建.

FYI - TJSONProtocol doesn't work with the version of Swift prior to version 0.14 so at this time you'll need to build from source if you need to use that.

此外... Swift强制将结构标记为 final ... JPA规范说实体不能为 final ...似乎可以与Eclipselink一起正常使用无论如何,除了YMMV

Also... Swift forces your structs to be marked final... JPA spec says entities can't be final... seems to work ok with Eclipselink anyhow but YMMV

这篇关于可以从现有的Java/scala接口和数据类型生成.thrift文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆