演员示例不编译

时间:2013-12-13 15:15:00

标签: scala akka actor

我正在尝试运行此代码来学习actor(在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
}

1 个答案:

答案 0 :(得分: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))  
  }    
}