可以编写自己的自定义序列化程序并让它们做各种奇怪的事情,然后你可以像往常一样在配置级别绑定它们:
class MyOwnSerializer extends Serializer {
// If you need logging here, introduce a constructor that takes an ExtendedActorSystem.
// class MyOwnSerializer(actorSystem: ExtendedActorSystem) extends Serializer
// Get a logger using:
// private val logger = Logging(actorSystem, this)
// This is whether "fromBinary" requires a "clazz" or not
def includeManifest: Boolean = true
// Pick a unique identifier for your Serializer,
// you've got a couple of billions to choose from,
// 0 - 40 is reserved by Akka itself
def identifier = 1234567
// "toBinary" serializes the given object to an Array of Bytes
def toBinary(obj: AnyRef): Array[Byte] = {
// Put the code that serializes the object here
//#...
Array[Byte]()
//#...
}
// "fromBinary" deserializes the given array,
// using the type hint (if any, see "includeManifest" above)
def fromBinary(
bytes: Array[Byte],
clazz: Option[Class[_]]): AnyRef = {
// Put your code that deserializes here
//#...
null
//#...
}
}
但这提出了一个重要的问题:如果您的消息都引用了已经在机器上共享的数据,为什么要在消息中放入指向对象的指针(非常糟糕!消息应该是不可变的,而指针不是!),而不是某种不可变的字符串objectId(有点你的选择1)?在保持消息的不变性方面,这是一个更好的选择,并且您的业务逻辑几乎没有变化(只需在共享状态存储上放置一个包装器)
有关更多信息,请参阅文档