Search code examples
akkaakka-remote-actor

Detect remote akka connection error


My project contains of 2 parts - one is akka server , and the other is play framework. Both can be sometimes restarted. For receiving logging from akka server i am using Websocket handler

  def ws = WebSocket.acceptWithActor[JsValue, JsValue] { request => out =>
    CheckerActor.props(out)
  }

with actor that subscribes for changes on some remote actor.

class CheckerActor(out: ActorRef) extends Actor {
...
  override def preStart() = {
    context.actorSelection("akka.tcp://[email protected]:2553/user/logger") ! Subscribe()
  }

  override def postStop() {
    context.actorSelection("akka.tcp://[email protected]:2553/user/logger") ! Unsubscribe()
  }

  def receive = {
    case msg: LogMessage => out ! Json.toJson(msg)
...
  }
}

Remote logger actor reacts to subscribe() unsubscribe() events and send logging message to subscribed clients. and it works fine until remoting server not hang's up or just restarted . What is the most cheapest way to detekt that remote akka server has broken connection ( after that I can make subscription again to new started logger ) . I can build some pinger actor and if I receive timeout than try to make resubscription again but simple pings can't give me guarantee that restart has happend between them and they make the system more complicated. May be there exists the other solution.


Solution

  • You can watch remote actors in the same way you watch local actors. Internally Akka uses heartbeat messages to check for termination which I think is similar to your 'ping' idea.

    Listening for Terminated messages will cover the event of a remote actor failing but I'm not sure this covers disconnection events. For that you can subscribe to DisassociatedEvent.

    Look at the Remote Events section in the Akka docs: http://doc.akka.io/docs/akka/snapshot/scala/remoting.html