Akka:部分功能链和发送者的行为

时间:2014-02-12 11:34:26

标签: scala akka

我尝试使用Akka actor实现不同的行为。在我的方法中, traits定义消息处理行为。具体的参与者混合这些特征,然后在使用部分函数链构建其接收函数时选择他们想要的行为。

不幸的是,考虑到发件人的实例化似乎存在一些问题 消息。如以下控制台消息所示,alice无法说明 消息的发送者“好”,这是鲍勃。

爱丽丝发送好吗?鲍勃    鲍勃回复好演员[akka:// StrategiesSystem / user / alice]    爱丽丝从演员那里得到了好处[akka:// StrategiesSystem / deadLetters]

正如您将在我的代码中看到的那样,预期结果是应该停止bob,而不是这种情况。

你的帮助将受到重视。

提前致谢,

最大

import akka.actor._
import scala.util.Random

//The messages which can be exchanged
sealed trait Move
case object StartMessage extends Move
case object StopMessage extends Move
case object Fine extends Move
case object Good extends Move
case object Bad extends Move

//The general class representing my actors
abstract class MyActor(val others: List[String]) extends Actor{ 
    //its name
    val name=self.path.name

    //it knows to choose on interlocutor
    val interlocteur=Random.shuffle(others.filterNot(p=> p==name)).head

    //All the actors are able to interpret the start/stop messages
    def metaReceive : Receive= {
        case StartMessage =>//start to ask question
            println(name+" send Fine? to "+interlocteur)
            context.actorSelection("../"+interlocteur) ! Fine
        case StopMessage =>
            println(name+" stops")
            context.stop(self)
  }
}

//An optimistic actor says it is fine
trait Optimistic{
  self: MyActor =>
  def handleFine:Receive = {
    case Fine => 
        println(self.name+" replies Good to "+sender)
        sender ! Good   
  }  
}

//A pessimistic actor says it is not fine
trait Pessimistic{
  self: MyActor =>
  def handleFine:Receive = {
    case Fine => 
        println(self.name+" replies Bad to "+sender)
        sender ! Bad
  }  
}

//An interpretor is an actor which is able to understand the reply
trait Interpretor{
  self: MyActor =>
  def handleAnswer:Receive = {
        case Good =>
            println(name+" receives Good from "+sender)
            sender ! StopMessage
        case Bad =>
            println(name+" receives Bad from "+sender)
            sender ! StopMessage
  }  
}

//My basic classes
class MyOptimisticActor(others: List[String]) extends  MyActor(others) with Optimistic{
  override def receive = metaReceive orElse handleFine //orElse ...
}

class MyPessimisticActor(others: List[String]) extends  MyActor(others) with Pessimistic{
  override def receive = metaReceive orElse handleFine //orElse ...
}
class MyInterpretorActor(others: List[String]) extends  MyActor(others) with Interpretor{
  override def receive = metaReceive orElse handleAnswer
}

//My application
object TestStrategies extends Application {
  val system = ActorSystem("StrategiesSystem")
  val names= List("alice","bob","carla")
  val alice = system.actorOf(Props(new MyInterpretorActor(names)), name = "alice")// alice is able to ask question and interpret answer
  val bob = system.actorOf(Props(new MyOptimisticActor(names)), name = "bob") // bob is able to reply and it is fine
  val carla = system.actorOf(Props(new MyPessimisticActor(names)), name = "carla") //carla is able to reply and it is not fine
  alice ! StartMessage //alice must ask a question
}

1 个答案:

答案 0 :(得分:1)

不要使用

self: MyActor =>

使用

this: MyActor =>

代替。