【问题标题】:Akka shutdown TCP actorAkka 关闭 TCP 演员
【发布时间】:2014-02-10 12:56:26
【问题描述】:

在下面的代码中

GSM 模拟未绑定

永远不会记录,即使“禁用”消息已发送到服务器。如何正确解绑 akka tcp 服务器?

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)
  }
}

【问题讨论】:

    标签: scala tcp akka


    【解决方案1】:

    向您发送Bound 消息的参与者实际上也是管理绑定的参与者。如果你想解除绑定,只需发送Unbind 给那个actor。只需将socketActor: Option[ActorRef] 字段添加到您的演员类并将您的代码更改为某事。喜欢

    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())
        // ...
      }
    }
    

    【讨论】:

    • 你能更精确一点吗?我不知道在哪里添加 socketActor 字段。谢谢!
    • 啊!其实那是我已经想通了。无论如何谢谢:)
    猜你喜欢
    • 2012-10-09
    • 2018-04-20
    • 1970-01-01
    • 2014-03-09
    • 2019-01-16
    • 2015-03-25
    • 2012-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多