2

Updated my code as per @Ryan Emerson suggestion but still i don't see any auto-generation of Impl file and proto file

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


}

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

Book.class

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

context-initialzer class with over-ride methods

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) {

    }
}

Then ClassA that instantiates context-initializer

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>

I am still not seeing any auto-generated proto file. Can someone tell me what am i doing wrong ?

Jon Abraham
  • 851
  • 3
  • 14
  • 27

2 Answers2

4

You also need to add the org.infinispan.protostream:protostream-processor artifact as a dependency in order for code to be generated:

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

Once that's present, a DummyInitializerImpl.java class will be generated that automatically registers the proto file and marshallers for the Book and Author classes. Remember that these classes must also have protostream annotations in order for the schema and marshallers to be generated. Please see the documentation for code examples.

There are two issues with your current code:

  1. You have provided a DummyInitializerImpl class, but that is what should be generated by @AutoProtoSchemaBuilder.
  2. In your DummyInitializerImpl you're trying to register the Infinispan UUIDMarshaller for the Book and Author types. This won't work as that marshaller is designed for the java UUID class.

I suspect that the two issues are due to a missunderstanding of how the code generation works. If you just required a SerializationContextInitializer for the Author and Book classes, it's not necessary to create the DummyInitializerImpl manually and you definitely don't need to utilise the UUIDMarshaller.

Ryan Emerson
  • 381
  • 2
  • 4
  • I have updated my post based on the suggestion you have provided. I have removed DummyInitializerImpl and directly initializing DummyInitializer in classA. I am still not seeing DummyInitializerImpl and library.proto getting generated. May i know if i am initializing DummyInitializer in classA correctly ? Also do i need to add a empty proto folder in my package so that i gets added during compile time ? I have already tried that but i am still not seeing anything getting auto-generated. – Jon Abraham May 28 '20 at 13:34
  • I am little confused as i would need to instantiate the iterface some way. Lets say i create a classB implements DummyInitializer now classB would have all the methods that are implemented in SerializationContextInitializer. Now coming to ClassA i would do something like this - DummyInitializer myInterface= new classB(); and then in my startCache method i would do builder.serialization().addContextInitializers(myInterface);. I am still not seeing any proto file getting created. – Jon Abraham May 28 '20 at 18:19
  • I think my main concern is how do i initialize dummyInitializer without creating another class that has override methods. Let's say if i skip creating dummyInitializerImpl and directly go to classA then how do i initialize dummyInitializer without creating methods ? It will always create @override methods when i initialize this in another class – Jon Abraham May 28 '20 at 22:31
  • 1
    You shouldn't create the `contextInitializer` class. The protostream-processor generates a `DummyInitializerImpl.class` for you. This can then be instantiated via `new DummyInitializerImpl()` in order to pass it to the global configuration builder e.g. `builder.serialization().addContextInitializers(new DummyInitializerImpl())`. If you require a different name for the generated implementation class, you can specify `className=` in the `@AutoProtoSchemaBuilder`. – Ryan Emerson May 29 '20 at 08:22
  • Awsome my issue was resolve by simply doing clean + install my maven project. One of the starter depedency was failing due to which i was never able to get impl file. But now i am able to see the DummyInitializerImpl file also proto file is generated. You guys rock ! – Jon Abraham May 29 '20 at 13:45
  • @JonAbraham after mvn clean install command looks at the target/classes path, you should find the proto folder name and the file .proto – Stefano Dec 31 '21 at 11:29
1

You are not saying which build system is used. maven maybe ? Did you add the protostream annotation processor as a dependency? Having a definite answer to these questions would help unriddle the issue of code generation. And after, we still need to find out who is supposed to initialize that dummyInitializer field.

  • I have updated my post based on your suggestion but i still don't see proto file getting created by context initializer. I am using the same Book.class and Author.Class files from ininispan project. – Jon Abraham May 28 '20 at 01:51
  • Also to help others here is the link that you gave me - https://github.com/tristantarrant/infinispan-demo-remote-query/blob/master/src/main/java/org/infinispan/remotequerydemo/infinispan/Query.java#L20 – Jon Abraham May 29 '20 at 13:49
  • I have a related question: How to perform the code generation without Maven? But with the java command via cmd. line? In case Maven is not your project type but you still want code generation... – extremecoder85 Apr 22 '21 at 13:55
  • @JonAbraham, did you resolved ? I have the same issue described at the followed link https://stackoverflow.com/questions/70519415/how-to-implement-protostream-with-autoprotoschemabuilder-for-data-grid-red-hat-r – Stefano Dec 29 '21 at 14:29