Class SerializerWithStringManifest
- java.lang.Object
-
- akka.serialization.SerializerWithStringManifest
-
- All Implemented Interfaces:
Serializer
- Direct Known Subclasses:
AsyncSerializerWithStringManifest
,ClusterMessageSerializer
,MessageSerializer
,MiscMessageSerializer
,ReplicatedDataSerializer
,ReplicatorMessageSerializer
,ServiceKeySerializer
,TestSerializer
public abstract class SerializerWithStringManifest extends java.lang.Object implements Serializer
A Serializer represents a bimap between an object and an array of bytes representing that object.For serialization of data that need to evolve over time the
SerializerWithStringManifest
is recommended instead ofSerializer
because the manifest (type hint) is aString
instead of aClass
. That means that the class can be moved/removed and the serializer can still deserialize old data by matching on theString
. This is especially useful for Akka 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 toSerializerWithStringManifest
the manifest string will be the full class name if you usedincludeManifest=true
, otherwise it will be the empty string.Serializers are loaded using reflection during
ActorSystem
start-up, where two constructors are tried in order:- taking exactly one argument of type
ExtendedActorSystem
, and - without arguments
Be sure to always use the
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 Constructor Description SerializerWithStringManifest()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract java.lang.Object
fromBinary(byte[] bytes, java.lang.String manifest)
Produces an object from an array of bytes, with an optional type-hint.java.lang.Object
fromBinary(byte[] bytes, scala.Option<java.lang.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
identifier()
Completely unique value to identify this implementation of Serializer, used to optimize network traffic.boolean
includeManifest()
Returns whether this serializer needs a manifest in the fromBinary methodabstract java.lang.String
manifest(java.lang.Object o)
Return the manifest (type hint) that will be provided in the fromBinary method.abstract byte[]
toBinary(java.lang.Object o)
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 akka.serialization.Serializer
fromBinary, fromBinary
-
-
-
-
Method Detail
-
fromBinary
public abstract java.lang.Object fromBinary(byte[] bytes, java.lang.String manifest) throws java.io.NotSerializableException
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:
java.io.NotSerializableException
-
fromBinary
public final java.lang.Object fromBinary(byte[] bytes, scala.Option<java.lang.Class<?>> manifest)
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 Akka 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
public abstract java.lang.String manifest(java.lang.Object o)
Return the manifest (type hint) that will be provided in the fromBinary method. Use""
if manifest is not needed.
-
toBinary
public abstract byte[] toBinary(java.lang.Object o)
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
-
-