【问题标题】:Thrift: using an external java class in list definitionThrift:在列表定义中使用外部 java 类
【发布时间】:2014-03-27 03:03:11
【问题描述】:

我是 Thrift 的新手,非常感谢在编写 Thrift 生成器文件时提供的帮助。我想使用具有多种语言的许多客户端的 Java 服务器。我正在使用 Thrift 自动生成这些文件。

这是我的 Thrift 文件:

namespace php example
namespace py example
namespace csharp example
namespace cpp example
namespace perl example
namespace d example
namespace java javaobjectmethods

struct ExternalLibraryItem {
    1: required string name
}

service ExampleService {
    list<ExternalLibraryItem> javaObjectMethod(1:i32 count)
}

我在包 javaObject 中有一组单独的 Java 文件,而 javaObjectMethod 是包中的方法之一。但是,此方法返回一个由外部库实例化的对象。我如何在 Thrift 文件中写这个没有:

struct ExternalLibraryItem {
    1: required string name
}

它目前不会让我生成没有此行的服务器文件。

这是我的 Java 文件:

package javaobjectmethods;

import externalLibrary.ExternalLibaryItem;
import externalLibrary.ExternalLibraryClass;

public class javaObject {
    private String file;

    public javaObject(String file) {
        this.file = file;
    }

    public List<ExternalLibraryItem> javaObjectMethod(int count) {
        // this method returns List<ExternalLibraryItem>
        return ExternalLibraryClass.doThis(count, this.file);
    }
}

【问题讨论】:

    标签: java thrift thrift-protocol


    【解决方案1】:

    简短的回答:你不能。

    所有 Thrift structs 都应生成代码。对于某些语言,创建部分类(例如 C#)可以在这种情况下提供帮助,但并非对所有语言都有帮助。此外,在某些情况下(C++),基类可以在一定程度上进行定制。对于 Java,

    struct Foo {
      1: i32 bar
    }
    

    产生以

    开头的类
    public class foo implements org.apache.thrift.TBase<foo, foo._Fields>, 
                                java.io.Serializable, 
                                Cloneable, 
                                Comparable<foo>
    

    这是周围的代码和结构所需要和期望的。

    针对您的特定情况,一种可能的解决方案是将内部类转换为 Thrift 类,反之亦然。

    当然,如果你有一个很棒的想法如何更好地做到这一点 - 我们愿意接受高质量的补丁。

    【讨论】:

    • 很酷,谢谢。我想我可以解决这个问题,只是想看看是否可以不实施解决方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多