Chapter 8. Marshalling Custom Java Objects with ProtoStream
Data Grid uses a ProtoStream API to encode and decode Java objects into Protocol Buffers (Protobuf); a language-neutral, backwards compatible format.
8.1. Protobuf Schemas Copy linkLink copied to clipboard!
Protocol Buffers, Protobuf, schemas provide structured representations of your Java objects.
You define Protobuf message types .proto
schema files as in the following example:
The preceding .library.proto
file defines an entity (Protobuf message type) named Book that is contained in the book_sample package. Book declares several fields of primitive types and an array (Protobuf repeatable field) named authors, which is the Author message type.
Protobuf Messages
- You can nest messages but the resulting structure is strictly a tree, never a graph.
- Type inheritance is not possible.
- Collections are not supported but you can emulate arrays with repeated fields.
Reference
8.2. ProtoStream Serialization Contexts Copy linkLink copied to clipboard!
A ProtoStream SerializationContext
contains Protobuf type definitions for custom Java objects, loaded from .proto
schema files, and the accompanying Marshallers for the objects.
The SerializationContextInitializer
interface registers Java objects and marshallers so that the ProtoStream library can encode your custom objects to Protobuf format, which then enables Data Grid to transmit and store your data.
8.3. ProtoStream Types Copy linkLink copied to clipboard!
ProtoStream can handle the following types, as well as the unboxed equivalents in the case of primitive types, without any additional configuration:
-
String
-
Integer
-
Long
-
Double
-
Float
-
Boolean
-
byte[]
-
Byte
-
Short
-
Character
-
java.util.Date
-
java.time.Instant
To marshall any other Java objects, you must generate, or manually create, SerializationContextInitializer
implementations that register .proto
schemas and marshallers with a SerializationContext
.
8.4. Generating Serialization Context Initializers Copy linkLink copied to clipboard!
Data Grid provides an protostream-processor
artifact that can generate .proto
schemas and SerializationContextInitializer
implementations from annotated Java classes.
Procedure
Add the
protostream-processor
dependency to yourpom.xml
.Copy to Clipboard Copied! Toggle word wrap Toggle overflow Annotate the Java objects that you want to marshall with
@ProtoField
and@ProtoFactory
.Book.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Author.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Define an interface that extends
SerializationContextInitializer
and is annotated with@AutoProtoSchemaBuilder
.Copy to Clipboard Copied! Toggle word wrap Toggle overflow
During compile-time, protostream-processor
generates a concrete implementation of the interface that you can use to initialize a ProtoStream SerializationContext
. By default, implementation names are the annotated class name with an "Impl" suffix.
Examples
The following are examples of a generated schema file and implementation:
target/classes/proto/library.proto
LibraryInitializerImpl.java
8.5. Manually Implementing Serialization Context Initializers Copy linkLink copied to clipboard!
In some cases you might need to manually define .proto
schema files and implement ProtoStream marshallers. For example, if you cannot modify Java object classes to add annotations.
Procedure
Create a
.proto
schema with Protobuf messages.Copy to Clipboard Copied! Toggle word wrap Toggle overflow Use the
org.infinispan.protostream.MessageMarshaller
interface to implement marshallers for your classes.BookMarshaller.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow AuthorMarshaller.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a
SerializationContextInitializer
implementation that registers the.proto
schema and the ProtoStream marshaller implementations with aSerializationContext
.ManualSerializationContextInitializer.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow