简体   繁体   English

带有macWire DI的Akka actoreRef,未设置actoreRef

[英]Akka actoreRef with macWire DI, actoreRef is not set

I have a 2 actors, Actor1 and Actor2 . 我有2个演员, Actor1Actor2 Actor1 wants to send MyMsg1 to Actor2 , and Actor2 after doing some work and getting Future[MyMsg2] wants to send MyMsg2 to Actor1 . Actor1希望将MyMsg1发送给Actor2Actor2在完成一些工作并获得Future[MyMsg2]希望将MyMsg2发送给Actor1 I have got this working one way but it fails with DI. 我已经以一种方式进行了此工作,但是DI失败了。

  1. Scenario 1 - Working scenario 方案1-工作方案

    • Actor1 -->MyMsg1-->Actor2 Actor1-> MyMsg1-> Actor2
    • Actor2 MyMsgHandler - Processes message(with Future), does pipeTo to sender with MyMsg2 . Actor2 MyMsgHandler -进程消息(具有未来),莫非pipeTo发件人与MyMsg2 Works fine, Actor1 recvs MyMsg2 工作正常,Actor1 MyMsg2
  2. Scenario2 - Failing scenario 方案2-失败的方案

    • Actor1 has a bean injected via MacWire - myBean . Actor1有一个通过MacWire- myBean注入的bean。
    • myBean has Actor2 injected as a bean and sends MyMsg1 to Actor2 myBeanActor2作为bean注入,并将MyMsg1发送到Actor2
    • Actor2 MyMsgHandler processes message(with Future), does pipeTo to sender and tries sending MyMsg2 - Goes to deadLetter. Actor2 MyMsgHandler处理消息(使用Future),将MyMsg2发送到发件人,然后尝试发送MyMsg2转到deadLetter。 The actor Ref for sender is never set. 从未设置发送者的演员Ref。

How do I fix this? 我该如何解决?

Pasting code also 也粘贴代码

class Actor1(failedService: FailedService, actor2: ActorRef @@ Actor2) extends Actor{
  override def receive: Receive = {
    case TriggerActor1() =>
      println("Actor1 triggered from REST controller. Send msg to actor 2")
      failedService.testSend()
      //actor2 ! Msg1()
    case Msg2() => println("got msg2 from actor 1")
  }

class Actor2 extends Actor {
  override def receive: Receive = {
    case Msg1() => {
      println("send without future")
      val origsender = sender()
      origsender ! Msg2()
    }
  }

class FailedService(actor2: ActorRef@@Actor2) {
  def testSend() = {
    actor2 ! Msg1()
  }
}

With my current code as shared above, Actor1 is able to send Msg1 to Actor2 and actor2 responds with Msg2 but Msg2 goes to deadletter. 以我目前的代码共享以上, Actor1能够发送Msg1Actor2actor2与响应Msg2 ,但Msg2去死信。 I get the below error akka.actor.DeadLetterActorRef - Message [backup.failedakka.Msg2] from Actor[akka://application/user/actor2#-662746578] to Actor[akka://application/deadLetters] was not delivered. [1] dead letters encountered. 我收到以下错误akka.actor.DeadLetterActorRef - Message [backup.failedakka.Msg2] from Actor[akka://application/user/actor2#-662746578] to Actor[akka://application/deadLetters] was not delivered. [1] dead letters encountered. akka.actor.DeadLetterActorRef - Message [backup.failedakka.Msg2] from Actor[akka://application/user/actor2#-662746578] to Actor[akka://application/deadLetters] was not delivered. [1] dead letters encountered.

However, if insted of using the line failedService.testSend() in my Actor1, I uncomment the line below it and use that to communicate, things work fine. 但是,如果想在failedService.testSend()中使用行failedService.testSend() ,则取消注释其下方的行并使用该行进行通信,一切就可以正常进行。 Is the Q clear now? Q现在清楚了吗? I am injecting dependencies with MacWire 我正在使用MacWire注入依赖项

! is defined as: 定义为:

def !(message: Any)(implicit sender: ActorRef = Actor.noSender): Unit

You can see the problem now, there's no implicit sender in scope of FailedService , although it is in scope "inside" an actor implementation. 您现在可以看到问题,尽管FailedService范围内没有隐式发件人,但它在actor实现的“内部”范围内。

I think you want to do: 我想你想做:

class FailedService(actor2: ActorRef@@Actor2) {
  def testSend(implicit sender: ActorRef) = {
    actor2 ! Msg1()
  }
}

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

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