Este contenido no está disponible en el idioma seleccionado.
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 Copiar enlaceEnlace copiado en el portapapeles!
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 Copiar enlaceEnlace copiado en el portapapeles!
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 Copiar enlaceEnlace copiado en el portapapeles!
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 Copiar enlaceEnlace copiado en el portapapeles!
Data Grid provides an protostream-processor artifact that can generate .proto schemas and SerializationContextInitializer implementations from annotated Java classes.
Procedure
Add the
protostream-processordependency to yourpom.xml.Copy to Clipboard Copied! Toggle word wrap Toggle overflow Annotate the Java objects that you want to marshall with
@ProtoFieldand@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
SerializationContextInitializerand 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 Copiar enlaceEnlace copiado en el portapapeles!
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
.protoschema with Protobuf messages.Copy to Clipboard Copied! Toggle word wrap Toggle overflow Use the
org.infinispan.protostream.MessageMarshallerinterface 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
SerializationContextInitializerimplementation that registers the.protoschema and the ProtoStream marshaller implementations with aSerializationContext.ManualSerializationContextInitializer.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow