简体   繁体   English

如何在 Akka 类型中从 ActorSystem 传递自我引用

[英]How to Pass self reference from ActorSystem in Akka Typed

I am trying to get replies from the AKKA actor but i am able to get response with the Help of Ask method我正在尝试从 AKKA 演员那里得到回复,但我可以通过 Ask 方法的帮助得到回复

object mainTest {

  val TestTypedSystem: ActorSystem[FuncTest.TypedFuncTest] = ActorSystem(FuncTest(),"FuncTest")

  implicit val sc: Scheduler = TestTypedSystem.scheduler

  implicit val ex: ExecutionContext = TestTypedSystem.executionContext

  import scala.concurrent.duration._

  import scala.language.postfixOps
  implicit val timeout: Timeout = Timeout(3 seconds)

  import FuncTest._

  //passing actorContext to the immutable Case class directly

  val testList = List (
    AskSomeoneTyped("Jay", 32, 'c',ActorRef[FuncTest.TypedFuncTest]),
    AskSomeoneTyped("Jay1", 32, 'a',ActorRef[FuncTest.TypedFuncTest]),
    AskSomeoneTyped("Jay2", 32, 'b',ActorRef[FuncTest.TypedFuncTest])
  )

  testList.foreach {
    player => TestTypedSystem ! TestaddSomeone(player)
  }

def main(args: Array[String]): Unit = {

 //Ask pattern to get responses by passing self context

 val sendingResp = TestTypedSystem.ask{
    f: ActorRef[FuncTest.TypedFuncTest] => AskSomeoneTyped("Jay",32,'c',f)
 }


  sendingResp.onComplete{
    case Success(value) => TestTypedSystem.log.info(s"Successfully completed : $value")
    case Failure(exception) => TestTypedSystem.log.warn(s"Got an exception $exception")

    }
  }
}

above Ask pattern from main method helps me get results back by passing 1 arg at a time but when using list.foreach I am not able to pass context of ActorSystem to get responses for a List of Actors.以上来自 main 方法的 Ask 模式通过一次传递 1 个 arg 帮助我获取结果,但是在使用 list.foreach 时,我无法传递 ActorSystem 的上下文来获取 Actor 列表的响应。

messages are going to DeadLetters消息将发送到 DeadLetters

Any Suggestions?有什么建议么?

Still a lot of the code is missing, so I can't verify my answer.仍然缺少很多代码,所以我无法验证我的答案。 However, an educated guess would be:但是,有根据的猜测是:

The fourth argument of the each of the three case class instances in the list列表中三个案例 class 实例的第四个参数

val testList = List (
    AskSomeoneTyped("Jay", 32, 'c',ActorRef[FuncTest.TypedFuncTest]),
    AskSomeoneTyped("Jay1", 32, 'a',ActorRef[FuncTest.TypedFuncTest]),
    AskSomeoneTyped("Jay2", 32, 'b',ActorRef[FuncTest.TypedFuncTest])
  )

reference the type of the actor reference, not the actual actor references.引用actor引用的类型而不是实际的actor引用。 If the behavior of FuncTest.apply() (missing in the above) tries to send a reply to these 'references', none will arive.如果FuncTest.apply()的行为(上面缺少)试图发送对这些“引用”的回复,则不会到达。

The problem is that the maintest object is not an actor, thus has no actor reference.问题是maintest object 不是演员,因此没有演员参考。 Akka is not able to send it messages. Akka 无法向其发送消息。 Hence the f in the ask pattern因此,询问模式中的f

val sendingResp = TestTypedSystem.ask{
    f: ActorRef[FuncTest.TypedFuncTest] => AskSomeoneTyped("Jay",32,'c',f)
 }

which provides this reference.它提供了这个参考。 See the docs .请参阅文档

You might want to try something more along these lines:您可能想尝试更多这些方面的东西:

case class Someone( name: String, age: Int, grade: Char)

val people = List (
    Someone("Jay",  32, 'c'),
    Someone("Jay1", 32, 'a'),
    Someone("Jay2", 32, 'b')
)

people.map{
    case Someone( name, age, grade ) => TestTypedSystem.ask( actorRef => AskSomeoneTyped( name, age, grade, actorRef ))
}.foreach( _.onComplete
{
    case Success(value) => TestTypedSystem.log.info(s"Successfully completed : $value")
    case Failure(exception) => TestTypedSystem.log.warn(s"Got an exception $exception")
})

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM