2013-12-13 111 views
0

我想运行此代码来学习演员(在Eclipse上使用Scala),但它告诉我,值Ping和Pong未找到。演员示例不编译

任何想法我做错了什么?

我安装了akka。 任何帮助表示赞赏。

感谢


import scala.actors.Actor 
import scala.actors.Actor._ 

class Ping(count: int, pong:Actor) extends Actor{ // type int here is not found as well 
    def act(){ 
    var pingsLeft= count-1 
    pong! Ping 
     while(true){ 
     receive { 
     case Pong => 
      if (pingsLeft % 1000 ==0) 
      Console.println("Ping : pong ") 
     if (pingsLeft > 0){ 
      pong ! Ping 
      pingsleft -=1 
     } else { 
      Console.println("Ping : stop") 
      pong ! Stop 
      exit() 
     } 
     } 
    } 
    } 
} 

class Pong extends Actor { 
    def act(){ 
    var pongCount =0 
    while (true){ 
     receive { 
     case Ping => 
      if(pongCount % 1000 ==0) 
      Console.println("Pong : ping " + pongCount) 
      sender ! Pong 
      pongCount = pongCount + 1 
     case Stop => 
      Console.println("Pong : stop") 
      exit() 
     } 
    } 
    } 
} 

object pingpong extends Application { 
    val pong = new Pong 
    val ping = new Ping(100000, pong) 
    ping.start 
    pong.start 
} 
+4

就像一个人站起来一样,scala参与者已被弃用。如果您计划使用Actor,则应更改您的示例以使用akka.actor中的actor而不是scala.actor。您还需要重构代码以使用akka约定。 – cmbaxter

回答

2

正如我在我的评论说,你应该到Akka切换你的榜样。这里就是你们的榜样的粗略近似重构使用Akka

import akka.actor._ 

class Ping(count: Int, pong:ActorRef) extends Actor{ // type int here is not found as well 
    pong! Ping 
    var pingsLeft = count - 1 

    def receive = { 
    case Pong => 
     if (pingsLeft % 1000 ==0) 
     Console.println("Ping : pong ") 
     if (pingsLeft > 0){ 
     pong ! Ping 
     pingsLeft -=1 
     } else { 
     Console.println("Ping : stop") 
     pong ! Stop 
     context stop self  
    } 
    } 
} 

class Pong extends Actor { 
    var pongCount =0 

    def receive = { 
     case Ping => 
      if(pongCount % 1000 ==0) 
      Console.println("Pong : ping " + pongCount) 
      sender ! Pong 
      pongCount = pongCount + 1 
     case Stop => 
      Console.println("Pong : stop") 
      exit()  
    } 
} 

case object Ping 
case object Pong 
case object Stop 

object pingpong { 
    def main(args: Array[String]) { 
    val system = ActorSystem("pingpong") 
    val pong = system.actorOf(Props[Pong]) 
    val ping = system.actorOf(Props(classOf[Ping], 100000, pong)) 
    } 

} 

,他是一个稍微重构版本,清理一些可变的状态,并且还设置了Pong实例作为Ping实例的一个孩子,这样,当Ping它也会自动停止Pong实例:

import akka.actor._ 

class Ping(count: Int) extends Actor{ // type int here is not found as well 
    val pong = context.actorOf(Props[Pong]) 
    pong! Ping 

    def receive = pingReceive(count - 1) 

    def pingReceive(pingsLeft:Int):Receive = { 
    case Pong => 
     if (pingsLeft % 1000 ==0) 
     Console.println("Ping : pong ") 
     if (pingsLeft > 0){ 
     pong ! Ping 
     context.become(pingReceive(pingsLeft - 1)) 
     } 
     else { 
     Console.println("Ping : stop") 
     context stop self  
     }   
    } 
} 

class Pong extends Actor { 
    override def postStop{ 
    Console.println("Pong : stop") 
    } 

    def receive = pongReceive(0) 

    def pongReceive(pongCount:Int):Receive = { 
    case Ping => 
     if(pongCount % 1000 ==0) 
     Console.println("Pong : ping " + pongCount) 

     sender ! Pong 
     context.become(pongReceive(pongCount + 1))   
    } 
} 

case object Ping 
case object Pong 

object PingPong { 
    def main(args: Array[String]) { 
    val system = ActorSystem("pingpong") 
    val ping = system.actorOf(Props(classOf[Ping], 100000)) 
    }  
}