简单的远程演员/服务器:本地客户端不会停止

时间:2012-12-10 17:57:30

标签: scala actor remote-actors

开始使用scala并且非常喜欢这种语言,但我似乎很难理解演员的工作方式。我之前使用NIO做了很多远程的东西,但现在我想尝试scala的远程演员。

我正在尝试创建一个非常简单的服务器和客户端(请参阅下面的代码)。

  1. 服务器已启动
  2. 制作远程演员代理(遵循此示例:http://www.scala-lang.org/docu/files/actors-api/actors_api_guide_6.html
  3. 我通过代理向服务器发送消息,然后通过
  4. 我想关闭我的申请.....但是如何!? :)。它永远持续下去
  5. 代码:

    package testactors
    
    import scala.collection.mutable.ArrayBuffer
    import scala.actors._
    import scala.actors.Actor._
    import scala.actors.remote._
    import scala.actors.remote.RemoteActor._
    
    class Server extends Actor {
        start
    
        @volatile private var toLive = true
    
        def act() {
    
            alive(9000)
            register('server, self)
    
            while (toLive) {
                receiveWithin(100) {
                    case TIMEOUT =>
                    case e => println(e)
                }
            }
    
            println("Server died")
        }
    
        def killActor() {
            toLive = false
        }
    
    }
    
    object Main {
    
        def main(args: Array[String]): Unit = {
    
            val remoteSrc = new Server
    
            println("Connecting to remote actor")
            var localSrc = select(Node("localhost", 9000), 'server)
            localSrc ! "hello"
            Thread.sleep(1000)
    
            println("Killing remote actor")
            remoteSrc.killActor
    
        }
    
    }
    

    输出结果为:

    Connecting to remote actor
    hello
    Killing remote actor
    Server died
    

    但它一直在继续......

    更新

    SORRY!重复,第3次搜索找到答案: Is it bad practice to send an actor a message from something which is not an actor?

0 个答案:

没有答案