[英]Akka actoreRef with macWire DI, actoreRef is not set
I have a 2 actors, Actor1
and Actor2
. 我有2个演员,
Actor1
和Actor2
。 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
发送给Actor2
, Actor2
在完成一些工作并获得Future[MyMsg2]
希望将MyMsg2
发送给Actor1
。 I have got this working one way but it fails with DI. 我已经以一种方式进行了此工作,但是DI失败了。
Scenario 1 - Working scenario 方案1-工作方案
MyMsg2
. MyMsg2
。 Works fine, Actor1 recvs MyMsg2
MyMsg2
Scenario2 - Failing scenario 方案2-失败的方案
myBean
. myBean
注入的bean。 myBean
has Actor2
injected as a bean and sends MyMsg1
to Actor2 myBean
将Actor2
作为bean注入,并将MyMsg1
发送到Actor2 MyMsg2
- Goes to deadLetter. MyMsg2
发送到发件人,然后尝试发送MyMsg2
转到deadLetter。 The actor Ref for sender is never set. 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
能够发送Msg1
到Actor2
和actor2
与响应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.