Akka:部分功能链接和行为发送者 [英] Akka: partial function chaining and sender for behaviours

查看:87
本文介绍了Akka:部分功能链接和行为发送者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I try to implement different behaviours using Akka actors. In my approach, traits define message handling behaviours. The concrete actors mix in those traits and then pick which behaviours they want when building out their receive function by using partial function chaining.

不幸的是,似乎在考虑一些麻烦发件人
的消息实例化。如以下控制台消息中所示,爱丽丝无法使消息好的发件人
变成bob。

Unfortunately, it seems there is some trouble considering the instanciation of the sender of messages. As shown in the following console message alice is unable to indetify the sender of the message "Good" which is bob.

alice send可以吗? bob
bob回复演员好[akka:// StrategiesSystem / user / alice]
爱丽丝从演员获得好[akka:// StrategiesSystem / deadLetters]

alice send Fine? to bob bob replies Good to Actor[akka://StrategiesSystem/user/alice] alice receives Good from Actor[akka://StrategiesSystem/deadLetters]

正如您将在我的代码中看到的那样,预期结果是应该停止bob,但实际情况并非如此。

As you will see in my code the expected result is that bob should be stopped which is not the case.

您的帮助将不胜感激。

在此先感谢

最大值

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
}


推荐答案

不使用

self: MyActor =>

使用

this: MyActor =>

相反。

这篇关于Akka:部分功能链接和行为发送者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆