2016-12-10 57 views
0

我是新來斯卡拉演員,我不知道爲什麼我的代碼不編譯。 顯然是'?'不是成員,我已經導入了akka.actors.patern._,但它仍然不起作用。 任何人都可以澄清這個黑暗的事情嗎? 謝謝!斯卡拉,圖書館akka.actor

import akka.actor.{Actor, ActorRef} 
import akka.pattern._ 

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, val regist:Registry, var followers:List[ActorRef]=List()) extends Actor{ 
    import Tweeter._ 
    def receive = { 
     case Follow(user) => { 
     //Here is the error 
     (regist!? Lookup(name)) 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) 
    def apply(name: String, annuaire:Registry) = Registry.system.actorOf(Props(new Tweeter(name,annuaire))) 
} 

回答

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

一般來說,最好使用演員調度員在上,因爲這將有一個綁定到演員系統生命週期的演員來執行期貨 - 它會關機當演員系統呢。 – johanandren