Doctor, I ended up using thrift + twitter's scrooge for messaging and
snapshoting. Scrooge generated pretty nice case classes for thrift structs.
The downside is that I have to list all the generated classes in my
serializer and the serialization config file, as show below.
If anyone knows how to write a generalized serializer for thrift+scrooge,
please share.
import akka.serialization.Serializer
import com.twitter.bijection.scrooge.BinaryScalaCodec
import autogen.data._
class EventSerializer extends Serializer {
val includeManifest: Boolean = true
val identifier = 870725
def toBinary(obj: AnyRef): Array[Byte] = obj match {
case m: Message1 => BinaryScalaCodec(Message1)(m)
case m: Message2 => BinaryScalaCodec(Message2)(m)
case m => throw new IllegalArgumentException("Cannot serialize object:
" + m)
}
def fromBinary(bytes: Array[Byte],
clazz: Option[Class[_]]): AnyRef = clazz match {
case Some(c) if c == classOf[Message1.Immutable] =>
BinaryScalaCodec(Message1).invert(bytes).get
case Some(c) if c == classOf[Message2.Immutable] =>
BinaryScalaCodec(Message2).invert(bytes).get
case Some(c) => throw new IllegalArgumentException("Cannot deserialize
class: " + c.getCanonicalName)
case None => throw new IllegalArgumentException("No class found in
EventSerializer when deserializing array: " + bytes.mkString(""))
}
}
The config is like:
akka {
actor {
serializers {
event = "com.my.EventSerializer"
}
serialization-bindings {
"com.coinport.coinex.data.Message1" = event
"com.coinport.coinex.data.Message2" = event
}
}
}
On Friday, March 14, 2014 5:32:13 AM UTC+8, rkuhn wrote:
>
> Hi Daniel,
>
> 11 mar 2014 kl. 09:13 skrev Daniel Wang <[email protected] <javascript:>
> >:
>
> Akka core developers, congratulations for the 2.3.0 release. My team are
> using it to develop our product right now. Thank you.
>
> One thing we are discussing (and still haven't come to an conclusion) is
> how to make sure all persistent events (commands if we use
> command-sourcing) will be backward compatible. We can certainly use
> protobuf WDL to defined all events/commands (and I did try), but it turns
> out working with Java POJO classes with Akka is so unnature as we are all
> spoiled by pattern matching. I also tried to use Twitter's scrooge to
> compile thrift files into scala code (so all thrift structs are case
> classes), but I didn't manage to come up with a general serializer (by
> 'general' I mean there is no need to serialized each event/command type
> case-by-case as the protobuf serializer). I'm going to try Kryo serializer
> soon. I wish I can have some luck.
>
>
> What we do with our own messages is to use the generated protobuf Java
> classes only as an intermediate step during serialization/deserialization
> so that we can work with case classes (for the reasons you state as well).
> This conversion will also help you decouple the versioning logic from the
> business logic, i.e. when you deserialize an old version of an event the
> conversion into a case class can synthesize defaults for those fields which
> are missing.
>
> Processor's in-memory state snapshots are also need to be backward
> compatible, otherwise each upgrade of events/commands will reply all
> history events/commands, given events/commands persistence are backward
> copyable. The idea on my mind is to 1) convert the scala-collection based
> memory snapshot into a huge thrift/protobuf strutter, then 2) serialize the
> thrift/protobuf structure to byte arrays. To make the conversion async (not
> to block processor event loop), I may have to write a customer serializer.
>
>
> Yes, this sounds about right.
>
> Regards,
>
> Roland
>
>
> It will be great if someone from the Akka dev team can comment and share
> their thoughts.
>
>
>
> --
> >>>>>>>>>> Read the docs: http://akka.io/docs/
> >>>>>>>>>> Check the FAQ:
> http://doc.akka.io/docs/akka/current/additional/faq.html
> >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
> ---
> You received this message because you are subscribed to the Google Groups
> "Akka User List" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected] <javascript:>.
> To post to this group, send email to [email protected]<javascript:>
> .
> Visit this group at http://groups.google.com/group/akka-user.
> For more options, visit https://groups.google.com/d/optout.
>
>
>
>
> *Dr. Roland Kuhn*
> *Akka Tech Lead*
> Typesafe <http://typesafe.com/> – Reactive apps on the JVM.
> twitter: @rolandkuhn
> <http://twitter.com/#!/rolandkuhn>
>
>
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ:
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.