在下面的代码片段中,ControllerActor向BackgroundActor请求多条消息。稍后询问的消息将在先前询问的消息之前处理。但有些如何不交付并导致DeadLetter异常。请在下面找到代码段: -
package pack
import scala.concurrent.duration._
import akka.actor.Actor
import akka.actor.ActorRef
import akka.actor.ActorSystem
import akka.actor.Props
import akka.pattern.ask
import akka.util.Timeout
import pack.ControllerActor.Msg
object ActorAskingMultipleMsgs {
def main( args: Array[String] ): Unit = {
val system = ActorSystem( "test-system" )
val testActor = system.actorOf( Props[BackgroundActor], "testActor" )
val controller = system.actorOf( ControllerActor.props( testActor ), "controller" )
//second argument in Msg is the time
//that is used in BackgroundActor to simulate delay
controller ! Msg( "A", 8000 )
controller ! Msg( "B", 6000 )
controller ! Msg( "C", 1000 )
Thread.sleep(Integer.MAX_VALUE)
}
}
object ControllerActor {
case class Msg( msg: String, a: Int )
def props( testActor: ActorRef ) = Props( new ControllerActor( testActor ) )
}
class ControllerActor( val af: ActorRef ) extends Actor {
import scala.concurrent.ExecutionContext.Implicits.global
implicit val timeout = Timeout( 10 seconds )
def receive = {
case msg: Msg =>
println( s"starting processing for msg $msg" )
af ? msg map {
case str: String =>
println( s"data returned is $str" )
case _ => println( "Invalid data is returned" )
}
println( s"finished processing for msg $msg" )
case _ => println( "Unknown msg type in controller" )
}
}
class BackgroundActor extends Actor {
def receive = {
case Msg( str, a ) =>
println( s"processing of msg($str, $a) starts here" )
Thread.sleep( a )
println( s"processing of msg($str, $a) ends here" )
sender ! str.toUpperCase()
case _ =>
println( "Unknown msg" )
sender ! false
}
}
请建议,我在这里做错了什么?
答案 0 :(得分:1)
这是ControllerActor中的超时。将其设置为:
require(["dijit/registry", 'dojo/domReady!'], function(registry){ console.log(registry.byId('my-id')) });
原因是ControllerActor将立即发送所有三条消息。因此,将创造三个未来。由于actor一次处理一个消息,因此BackgroundActor至少需要8 + 6 = 14秒才能向第二条消息发送响应。这大于你设置超时的时间。
超时异常将终止ControllerActor。这就是你死信的原因。