【问题标题】:AutoProtoSchemaBuilder is not generating proto fileAutoProtoSchemaBuilder 没有生成原型文件
【发布时间】:2020-09-14 00:30:59
【问题描述】:

根据@Ryan Emerson 的建议更新了我的代码,但我仍然没有看到任何自动生成的 Impl 文件和 proto 文件

@AutoProtoSchemaBuilder(
        includeClasses = { Book.class,  Author.class },
        schemaFileName = "library.proto",
        schemaFilePath = "proto/")
        interface DummyInitializer extends SerializationContextInitializer {


}

作者.class

public class Author {
    private final String name;
    private final String surname;

    @ProtoFactory
    public Author(String name, String surname) {
        this.name = (String)Objects.requireNonNull(name);
        this.surname = (String)Objects.requireNonNull(surname);
    }

    @ProtoField(
        number = 1
    )
    public String getName() {
        return this.name;
    }

    @ProtoField(
        number = 2
    )
    public String getSurname() {
        return this.surname;
    }

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        } else if (o != null && this.getClass() == o.getClass()) {
            Author author = (Author)o;
            return this.name.equals(author.name) && this.surname.equals(author.surname);
        } else {
            return false;
        }
    }

    public int hashCode() {
        return Objects.hash(new Object[]{this.name, this.surname});
    }
}

书籍类

public class Book {
    private final String title;
    private final String description;
    private final int publicationYear;
    private final Set<Author> authors;

    @ProtoFactory
    public Book(String title, String description, int publicationYear, Set<Author> authors) {
        this.title = (String)Objects.requireNonNull(title);
        this.description = (String)Objects.requireNonNull(description);
        this.publicationYear = publicationYear;
        this.authors = (Set)Objects.requireNonNull(authors);
    }

    @ProtoField(
        number = 1
    )
    public String getTitle() {
        return this.title;
    }

    @ProtoField(
        number = 2
    )
    public String getDescription() {
        return this.description;
    }

    @ProtoField(
        number = 3,
        defaultValue = "-1"
    )
    public int getPublicationYear() {
        return this.publicationYear;
    }

    @ProtoField(
        number = 4
    )
    public Set<Author> getAuthors() {
        return this.authors;
    }

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        } else if (o != null && this.getClass() == o.getClass()) {
            Book book = (Book)o;
            return this.publicationYear == book.publicationYear && this.title.equals(book.title) && this.description.equals(book.description) && this.authors.equals(book.authors);
        } else {
            return false;
        }
    }

    public int hashCode() {
        return Objects.hash(new Object[]{this.title, this.description, this.publicationYear, this.authors});
    }
}

具有覆盖方法的上下文初始化器类

import org.infinispan.protostream.SerializationContext;

import java.io.UncheckedIOException;

public class contextInitializer implements DummyInitializer {
    @Override
    public String getProtoFileName() {
        return null;
    }

    @Override
    public String getProtoFile() throws UncheckedIOException {
        return null;
    }

    @Override
    public void registerSchema(SerializationContext serCtx) {

    }

    @Override
    public void registerMarshallers(SerializationContext serCtx) {

    }
}

然后是实例化上下文初始化器的 ClassA

public class classA {


  DummyInitializer myInterface= new contextInitializer();



    //Create a new cache instance


    public void startCache() {
        {

          try {
            manager = new DefaultCacheManager("src/main/resources/infinispan.xml");
            GlobalConfigurationBuilder builder= new GlobalConfigurationBuilder();
            builder.serialization().addContextInitializers(myInterface);
            System.out.println("------------------>"+ builder.serialization().addContextInitializers(myInterface));
            cache = manager.getCache();
            System.out.println(cache.getName()+" is initialized ");
          } catch (IOException e) {
            throw new IllegalArgumentException("Failed to initialize cache due to IO error",e);
          }
        }

        }

Maven

                <dependency>
                <groupId>org.infinispan</groupId>
                <artifactId>infinispan-bom</artifactId>
                <version>${infinispan.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.infinispan.protostream</groupId>
                <artifactId>protostream-processor</artifactId>
                <scope>provided</scope>
            </dependency>

我仍然没有看到任何自动生成的 proto 文件。谁能告诉我我做错了什么?

【问题讨论】:

    标签: caching protocol-buffers infinispan proto


    【解决方案1】:

    您并不是说使用了哪个构建系统。行家也许?您是否将 protostream 注释处理器添加为依赖项?对这些问题有明确的答案将有助于解开代码生成问题。之后,我们仍然需要找出谁应该初始化该 dummyInitializer 字段。

    【讨论】:

    • 我已根据您的建议更新了我的帖子,但我仍然没有看到上下文初始化程序创建了 proto 文件。我正在使用 ininispan 项目中的相同 Book.class 和 Author.Class 文件。
    • 为了帮助其他人,这里是你给我的链接 - github.com/tristantarrant/infinispan-demo-remote-query/blob/…
    • 我有一个相关的问题:如何在没有 Maven 的情况下执行代码生成?但是通过 cmd 使用 java 命令。线?如果 Maven 不是您的项目类型,但您仍需要代码生成...
    • @JonAbraham,你解决了吗?我在以下链接stackoverflow.com/questions/70519415/… 中描述了相同的问题
    【解决方案2】:

    您还需要添加 org.infinispan.protostream:protostream-processor 工件作为依赖项,以便生成代码:

    <dependency>
        <groupId>org.infinispan.protostream</groupId>
        <artifactId>protostream-processor</artifactId>
        <version>4.3.2.Final</version>
    </dependency>
    

    一旦出现,将生成一个 DummyInitializerImpl.java 类,该类会自动为 BookAuthor 类注册 proto 文件和编组器。请记住,这些类还必须具有 protostream 注释才能生成模式和编组器。代码示例请参见documentation

    您当前的代码有两个问题:

    1. 您提供了一个DummyInitializerImpl 类,但这应该是由@AutoProtoSchemaBuilder 生成的。
    2. 在您的DummyInitializerImpl 中,您尝试为BookAuthor 类型注册Infinispan UUIDMarshaller。这不起作用,因为该编组器是为 java UUID 类设计的。

    我怀疑这两个问题是由于对代码生成方式的误解造成的。如果您只需要 SerializationContextInitializer 用于 AuthorBook 类,则无需手动创建 DummyInitializerImpl 并且您绝对不需要使用 UUIDMarshaller。

    【讨论】:

    • 我已根据您提供的建议更新了我的帖子。我已删除 DummyInitializerImpl 并直接在 classA 中初始化 DummyInitializer。我仍然没有看到 DummyInitializerImpl 和 library.proto 正在生成。我可以知道我是否在 classA 中正确初始化 DummyInitializer 吗?我还需要在我的包中添加一个空的 proto 文件夹,以便在编译时添加吗?我已经尝试过了,但我仍然没有看到任何自动生成的内容。
    • 我有点困惑,因为我需要以某种方式实例化 iterface。假设我创建了一个实现 DummyInitializer 的 classB,现在 classB 将拥有在 SerializationContextInitializer 中实现的所有方法。现在来到 ClassA 我会做这样的事情 - DummyInitializer myInterface= new classB();然后在我的 startCache 方法中,我会执行 builder.serialization().addContextInitializers(myInterface);。我仍然没有看到任何原型文件被创建。
    • 我认为我主要关心的是如何在不创建另一个具有覆盖方法的类的情况下初始化 dummyInitializer。假设我跳过创建 dummyInitializerImpl 并直接转到 classA 那么我如何在不创建方法的情况下初始化 dummyInitializer ?当我在另一个类中初始化它时,它总是会创建 @override 方法
    • 您不应该创建contextInitializer 类。 protostream-processor 为您生成一个DummyInitializerImpl.class。然后可以通过new DummyInitializerImpl() 对其进行实例化,以便将其传递给全局配置构建器,例如builder.serialization().addContextInitializers(new DummyInitializerImpl())。如果生成的实现类需要不同的名字,可以在@AutoProtoSchemaBuilder中指定className=
    • 太棒了,我的问题是通过简单地清理 + 安装我的 maven 项目来解决的。由于我无法获得 impl 文件,其中一个启动器依赖失败了。但现在我可以看到 DummyInitializerImpl 文件也生成了 proto 文件。你们摇滚!
    猜你喜欢
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    • 2017-11-14
    • 2019-02-13
    • 1970-01-01
    相关资源
    最近更新 更多