它可以编写你自己的定制序列,让他们做各种奇怪的事情,那么你就可以在配置水平和往常一样将它们绑定:
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)?这是一个更好的选择,当谈到保护消息的不变性,并且在你的业务逻辑变化不大(只是把一个包装过的共享状态储存单元)
更多信息,请参阅the documentation
该文档说明了如何编写自定义序列化器/反序列化器(http://doc.akka.io/docs/akka/current/java/serialization.html#customization)。去与方法2 imho。你可以通过使用静态东西“编程”来实现 –
谢谢!我遇到了[SerializationSetup](http://doc.akka.io/api/akka/current/akka/serialization/SerializationSetup$.html)对象,我认为这是“以编程方式”添加串行器的正确方式在运行时。但是,在文档中找不到那个。 –
我是否应该在答案中转换评论,并且关闭该问题或者是否需要更多信息? –