Class SerializerWithStringManifest
- All Implemented Interfaces:
Serializer
- Direct Known Subclasses:
AsyncSerializerWithStringManifest
,ClusterMessageSerializer
,MessageSerializer
,MiscMessageSerializer
,ReplicatedDataSerializer
,ReplicatorMessageSerializer
,ServiceKeySerializer
,TestSerializer
For serialization of data that need to evolve over time the SerializerWithStringManifest
is recommended instead
of Serializer
because the manifest (type hint) is a String
instead of a Class
. That means
that the class can be moved/removed and the serializer can still deserialize old data by matching
on the String
. This is especially useful for Pekko Persistence.
The manifest string can also encode a version number that can be used in fromBinary
to
deserialize in different ways to migrate old data to new domain objects.
If the data was originally serialized with Serializer
and in a later version of the
system you change to SerializerWithStringManifest
the manifest string will be the full class name if
you used includeManifest=true
, otherwise it will be the empty string.
Serializers are loaded using reflection during pekko.actor.ActorSystem
start-up, where two constructors are tried in order:
- taking exactly one argument of type
pekko.actor.ExtendedActorSystem
, and - without arguments
Be sure to always use the pekko.actor.DynamicAccess
for loading classes! This is necessary to
avoid strange match errors and inequalities which arise from different class loaders loading
the same class.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract Object
fromBinary
(byte[] bytes, String manifest) Produces an object from an array of bytes, with an optional type-hint.final Object
fromBinary
(byte[] bytes, scala.Option<Class<?>> manifest) Produces an object from an array of bytes, with an optional type-hint; the class should be loaded using ActorSystem.dynamicAccess.abstract int
Completely unique value to identify this implementation of Serializer, used to optimize network traffic.final boolean
Returns whether this serializer needs a manifest in the fromBinary methodabstract String
Return the manifest (type hint) that will be provided in the fromBinary method.abstract byte[]
Serializes the given object into an Array of Byte.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.apache.pekko.serialization.Serializer
fromBinary, fromBinary
-
Constructor Details
-
SerializerWithStringManifest
public SerializerWithStringManifest()
-
-
Method Details
-
fromBinary
Produces an object from an array of bytes, with an optional type-hint.It's recommended to throw
java.io.NotSerializableException
infromBinary
if the manifest is unknown. This makes it possible to introduce new message types and send them to nodes that don't know about them. This is typically needed when performing rolling upgrades, i.e. running a cluster with mixed versions for while.NotSerializableException
is treated as a transient problem in the TCP based remoting layer. The problem will be logged and message is dropped. Other exceptions will tear down the TCP connection because it can be an indication of corrupt bytes from the underlying transport.- Throws:
NotSerializableException
-
fromBinary
Description copied from interface:Serializer
Produces an object from an array of bytes, with an optional type-hint; the class should be loaded using ActorSystem.dynamicAccess.- Specified by:
fromBinary
in interfaceSerializer
-
identifier
public abstract int identifier()Completely unique value to identify this implementation of Serializer, used to optimize network traffic. Values from 0 to 40 are reserved for Pekko internal usage.- Specified by:
identifier
in interfaceSerializer
-
includeManifest
public final boolean includeManifest()Description copied from interface:Serializer
Returns whether this serializer needs a manifest in the fromBinary method- Specified by:
includeManifest
in interfaceSerializer
-
manifest
Return the manifest (type hint) that will be provided in the fromBinary method. Use""
if manifest is not needed. -
toBinary
Serializes the given object into an Array of Byte.Note that the array must not be mutated by the serializer after it has been returned.
- Specified by:
toBinary
in interfaceSerializer
-