Scala,图书馆akka.actor

时间:2016-12-10 10:34:06

标签: scala akka

我是Scala演员的新手,我不知道为什么我的代码没有编译。 显然'?'我不是会员,我已经导入了akka.actors.patern._但它仍然无法正常工作。 有人可以澄清一下这个黑暗的东西吗? 谢谢!

 if(rows.contains(1))
    doAnalysis1(param1);

 if(rows.contains(2))
    doAnalysis2(param1);


 if(rows.contains(3))
    doAnalysis3(param1);

   }
 }

1 个答案:

答案 0 :(得分:1)

我刚刚更改了代码以使其编译。我添加了一些注释来澄清:

import akka.actor.{Actor, ActorRef}
import akka.pattern._
import scala.concurrent.duration._
import akka.util.Timeout

import scala.concurrent.ExecutionContext.Implicits.global

class Registry extends Actor {
  import Registry._
  var registry:Map[String, Any] = Map()
  def receive = {
    case Lookup(name) => sender ! registry.get(name)
  }
}

object Registry {
  import akka.actor.{ActorSystem, Props}
  case class Lookup(name: String)  
  // create dedicated actor infrastructure
  val system = ActorSystem("myActorInfrastructure")
  // create actor with a name and register it with the infrastructure
  def apply(name: String) = system.actorOf(Props(new Registry()), name) 
}

class Tweeter(val name: String, regist: ActorRef, var followers:List[ActorRef]=List()) extends Actor {
    import Tweeter._
    import Registry._

    implicit val timeout: Timeout = 3 seconds

    def receive = {
      case Follow(user) => {
        // regist must be an actor ref
        // Ask returns future, you have to map for pattern matching
        (regist ? Lookup(name)).map { _
          match {
            case Some(a:ActorRef) => a ! AddFollower(sender)
          }
        }
      }
      case AddFollower(follower) => followers = follower::followers 
  }
}

object Tweeter {

  import akka.actor.{ActorSystem, Props}
  case class Follow(user: String)
  case class AddFollower(follower: ActorRef)

  // Create the actor and use your reference
  val annuaire = Registry("registry")
  def apply(name: String, annuaire: ActorRef) = Registry.system.actorOf(Props(new Tweeter(name, annuaire))) 
}