Search code examples
scalaplayframeworkplayframework-2.0akka-httpakka-cluster

Akka/scala-OversizedPayloadException How to handel it?


We have a distributed application on akka cluster. Actor “A” sends message of large size to a remote actor. And we get the following warning:

2016-08-10 23:08:29,737 [EndpointWriter] ERROR - Transient association error (association remains live) akka.remote.OversizedPayloadException: Discarding oversized payload sent to Actor[akka.tcp://[email protected]:51665/temp/$b]: max allowed size 128000 bytes, actual size of encoded class common.data.model.configuration.UserList was 571444 bytes.

We are aware that we can increase the value in the configuration. But we wanted to check if the size exceeds the default limit we want to send a different message. Tried searching but no luck most of them only tell on how to configure it no one talks about on how to handle it and send the remote machine a message. Any suggestion or help would be appreciated.


Solution

  • Actually, we are able to handle it, just need to subscribe for akka.event.Logging.Error, and check, if the cause is OversizedPayloadException:

    import akka.actor.{Actor, Props}
    
    class Listener extends Actor {
      override def receive: Receive = {
        case akka.event.Logging.Error(cause, _, _, _)
          // this internal Akka class is package-private, hence we have to check it's classname :(
          if cause.getClass.getName == "akka.remote.OversizedPayloadException" =>
          // handle it here!
      }
    }
    
    val listener = system.actorOf(Props(new Listener))
    system.eventStream.subscribe(listener, classOf[akka.event.Logging.Error])