+all #16632 Make serialization identifiers configurable in reference.conf
This commit is contained in:
parent
064eea6180
commit
6332f888ce
17 changed files with 76 additions and 44 deletions
|
|
@ -33,8 +33,8 @@ import akka.serialization.JavaSerializer.CurrentSystem
|
|||
trait Serializer {
|
||||
|
||||
/**
|
||||
* Completely unique value to identify this implementation of Serializer, used to optimize network traffic
|
||||
* Values from 0 to 16 is reserved for Akka internal usage
|
||||
* Completely unique value to identify this implementation of Serializer, used to optimize network traffic.
|
||||
* Values from 0 to 16 are reserved for Akka internal usage.
|
||||
*/
|
||||
def identifier: Int
|
||||
|
||||
|
|
@ -65,6 +65,33 @@ trait Serializer {
|
|||
final def fromBinary(bytes: Array[Byte], clazz: Class[_]): AnyRef = fromBinary(bytes, Option(clazz))
|
||||
}
|
||||
|
||||
/**
|
||||
* Base serializer trait with serialization identifiers configuration contract,
|
||||
* when globally unique serialization identifier is configured in the `reference.conf`.
|
||||
*/
|
||||
trait BaseSerializer extends Serializer {
|
||||
/**
|
||||
* Actor system which is required by most serializer implementations.
|
||||
*/
|
||||
val system: ExtendedActorSystem
|
||||
/**
|
||||
* Configuration namespace of serialization identifiers in the `reference.conf`.
|
||||
*
|
||||
* Each serializer implementation must have an entry in the following format:
|
||||
* `akka.actor.serialization-identifiers."FQCN" = ID`
|
||||
* where `FQCN` is fully qualified class name of the serializer implementation
|
||||
* and `ID` is globally unique serializer identifier number.
|
||||
*/
|
||||
final val SerializationIdentifiers = "akka.actor.serialization-identifiers"
|
||||
/**
|
||||
* Globally unique serialization identifier configured in the `reference.conf`.
|
||||
*
|
||||
* See [[Serializer#identifier()]].
|
||||
*/
|
||||
final override val identifier: Int =
|
||||
system.settings.config.getInt(s"""${SerializationIdentifiers}."${getClass.getName}"""")
|
||||
}
|
||||
|
||||
/**
|
||||
* Java API for creating a Serializer: make sure to include a constructor which
|
||||
* takes exactly one argument of type [[akka.actor.ExtendedActorSystem]], because
|
||||
|
|
@ -117,12 +144,10 @@ object JavaSerializer {
|
|||
/**
|
||||
* This Serializer uses standard Java Serialization
|
||||
*/
|
||||
class JavaSerializer(val system: ExtendedActorSystem) extends Serializer {
|
||||
class JavaSerializer(val system: ExtendedActorSystem) extends BaseSerializer {
|
||||
|
||||
def includeManifest: Boolean = false
|
||||
|
||||
def identifier = 1
|
||||
|
||||
def toBinary(o: AnyRef): Array[Byte] = {
|
||||
val bos = new ByteArrayOutputStream
|
||||
val out = new ObjectOutputStream(bos)
|
||||
|
|
@ -154,9 +179,8 @@ class NullSerializer extends Serializer {
|
|||
* This is a special Serializer that Serializes and deserializes byte arrays only,
|
||||
* (just returns the byte array unchanged/uncopied)
|
||||
*/
|
||||
class ByteArraySerializer extends Serializer {
|
||||
class ByteArraySerializer(val system: ExtendedActorSystem) extends BaseSerializer {
|
||||
def includeManifest: Boolean = false
|
||||
def identifier = 4
|
||||
def toBinary(o: AnyRef) = o match {
|
||||
case null ⇒ null
|
||||
case o: Array[Byte] ⇒ o
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue