metaphori February 2016

Akka Remote and interception of unknown classes through custom deserialization

Problem/Context. I need to send some messages to a remote actor. These messages may contain objects of a class that is unknown on the recipient side. And I need to intercept such a situation in order to avoid a ClassNotFoundException.

One solution may consist in intercepting unknown classes at the time of message deserialization. Then, the message may be replaced by a different application-level message so that the remote actor can communicate to the sender that it doesn't have the required classes.

I don't know if such an interception is possible, because custom de/serializators must implement akka.serialization.Serializer which has the following method

def fromBinary(bytes: Array[Byte],
               clazz: Option[Class[_]]): AnyRef

Now, the problem stems from the construction of the Class objects (which is done from Akka) for objects of an unknown class.

Is there a way to customize Akka deserialization at a lower-level point in order to accomodate my needs?

Other solutions.

  • The problem is similar to the one depicted in the following SO question, where a different solution has been proposed: Deserialize remote object to the narrowest accessible class It would still be useful to have an answer there. However, that solution wouldn't be sufficient for me, because while it would be ok to restrict the interface, I would still need a class implementation with extra methods.

Answers


metaphori February 2016

FACT 1. Actually I discovered that there is an alternative to deserialization with Class type hint, namely deserialization with string manifest. Reference: http://doc.akka.io/docs/akka/2.4.0/scala/serialization.html#Serializer_with_String_Manifest

NOTE: it is NOT available for Akka < 2.4.0. It might matter as from Akka 2.4.0 support for Java 1.6/1.7 is dropped. This means that to use Akka 2.4 you are required to have Java 8.

FACT 2. I also discovered that you Akka does not necessarily provide you the type hint. You can disable the type hint in the following way

class MyOwnSerializer extends akka.serialization.Serializer {
   override def includeManifest: Boolean = false // !!!

Post Status

Asked in February 2016
Viewed 2,552 times
Voted 14
Answered 1 times

Search




Leave an answer