Coxer
Coxer

Reputation: 1704

Akka shutdown TCP actor

In the following code

GSM mock unbound

is never logged, even though the "disable" message was sent to the server. How can I properly unbind an akka tcp server?

class GsmRouter extends Actor {
  import Tcp._
  import context.system

  val name = this.getClass().getName()
  val logger = LoggerFactory.getLogger(name)

  def receive = {
    case "enable" => IO(Tcp) ! Bind(self, ConfigurationUtils.gsmRouterAddress)
    case "disable" => IO(Tcp) ! Unbind
    case Unbound =>
      logger.info("GSM mock unbound")
    case Bound(localAddress) =>
      logger.info("GSM mock bound to " + localAddress.getHostName() + ":" + localAddress.getPort())
    case CommandFailed(Bind(_,localAddress: InetSocketAddress, _, _)) =>
      logger.info("Could not bind to " + localAddress.getHostName() + ":" + localAddress.getPort())
      context stop self
    case Connected(remote, local) =>
      logger.info("Client connected to GSM mock")
      val handler = context.actorOf(Props[ConnectionHandler])
      val connection = sender
      connection ! Register(handler)
  }
}

Upvotes: 1

Views: 768

Answers (1)

drexin
drexin

Reputation: 24403

The actor that sends you the Bound message is actually the one that also manages the binding. If you want to unbind the socket, just send Unbind to that actor. Just add a socketActor: Option[ActorRef] field to your actor class and change your code to sth. like

class GsmRouter extends Actor {
  import Tcp._
  import context.system

  val name = this.getClass().getName()
  val logger = LoggerFactory.getLogger(name)
  var socketActor: Option[ActorRef] = None

  def receive = {
    // ...
    case "disable" =>
      socketActor.foreach(_ ! Unbind)
    case Bound(localAddress) =>
      socketActor = Some(sender)
      logger.info("GSM mock bound to " + localAddress.getHostName() + ":" + localAddress.getPort())
    // ...
  }
}

Upvotes: 2

Related Questions