A simple way to Customize Kryo Serializer for Akka Actors in Scala


Kryo is a fastest and efficient way to do serialization. It is more efficient than Java serialization – both in size and speed.

I am working on a Scala_Lift_Akka based project, where I have to implement serializer for Akka Actor’s remoting. Since Kryo is fast, efficient and easy to use, so I decided to implement Kryo in my project for serialization.

I implemented Romix’s kryo-based serialization library, which is excellent and easy to use.

Since above library is not compatible with Scala 2.10, so I used Akka-Kryo-Serializer_with Scala 2.10.

This kryo-based serializer supports all scala classes such as Option, Tuple, Enumeration etc. If you are using any other types except Scala types, such as net.liftweb.common.Box then you need to customize this serializer.

I have added a custom serailizer of net.liftweb.common.Box for my scenario. If you have such scenario, where you need to serialize other data-types except Scala types, then you would have to add serializer for that data type in below manner:

class LiftBoxSerializer extends Serializer[Box[_]] with Loggable {
  locally {
    setImmutable(true)
  }

  override def write(kryo: Kryo, output: Output, obj: Box[_]) = {
    obj match {
      case Full(x) =>
        output.writeBoolean(true)
        kryo.writeClassAndObject(output, x)
      case Empty =>
        output.writeBoolean(false)
      case _: Failure => logger.error("Error: Problem in serializing Box")
    }

  }

  override def read(kryo: Kryo, input: Input, typ: Class[Box[_]]): Box[_] = {
    if (input.readBoolean()) {
      val refResolver = kryo.getReferenceResolver
      val refId = refResolver.nextReadId(typ)
      kryo.reference(Empty)

      val inner = kryo.readClassAndObject(input)
      val res = Full(inner)
      refResolver.addReadObject(refId, res)
      res
    } else {
      Empty
    }
  }
}

After that, you have to give reference of this serializer in ‘getKryo’ method of ‘KryoSerializer.scala’
by adding

kryo.addDefaultSerializer(classOf[net.liftweb.common.Box[_]], classOf[LiftBoxSerializer])


Above approach works well for my scenario. If you have any better approach, please suggest.
Your suggestion would be highly appreciable.

Advertisements

About Ayush Mishra

Ayush is the Sr. Software Consultant @ Knoldus Software LLP. In his 5 years of experience he has become developer with proven experience in architecting and developing web applications. Ayush has a Masters in Computer Application from U.P. Technical University, Ayush is a strong-willed and self-motivated professional who takes deep care in adhering to quality norms within projects. He is capable of managing challenging projects with remarkable deadline sensitivity without compromising code quality. .
This entry was posted in Agile, Akka, Scala, Web. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s