2014-02-10 191 views
1

在下面的代碼阿卡關閉TCP演員

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

回答

2

向您發送Bound消息的參與者實際上也是管理綁定的參與者。如果你想解除綁定,請發送Unbind給那個演員。只需在您的演員課程中添加一個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()) 
    // ... 
    } 
} 
+0

你能更精確嗎?我不明白它在哪裏添加socketActor字段。謝謝! – Coxer

+0

更新了我的文章。 – drexin

+0

啊!其實這就是我已經想到的。不管怎麼說,還是要謝謝你 :) – Coxer