回应TestProbe收到的消息

时间:2016-12-19 14:19:08

标签: scala akka

我想模仿收到消息的Actor:

GetAllJobs()

并回复一条消息:

ReturnAllJobs(jobHash: Map[Int, Job])

我想用TestProbe做这样的事情:

resultReceiver.onMessage(GetAllJobs()).reply(ReturnAllJobs(jobHash))

那么如何使用TestProbe模拟actor?

2 个答案:

答案 0 :(得分:0)

Autopilot可能有所帮助。请参阅以下示例:

  val probe = TestProbe()
  probe.setAutoPilot(new TestActor.AutoPilot {
    def run(sender: ActorRef, msg: Any): TestActor.AutoPilot = {
      msg match {
        case GetAllJobs => 
          sender ! ReturnAllJobs(jobHash)
          TestActor.KeepRunning
        case x => TestActor.KeepRunning
      }
  })

有关详细信息,请参阅here

答案 1 :(得分:0)

对于使用Akka的Java粉丝

    final TestProbe simpleActor = TestProbe.apply(akkaSystem);

        simpleActor.setAutoPilot(new TestActor.AutoPilot() {
            public TestActor.AutoPilot run(ActorRef sender, Object msg) {
                if (msg instanceof DoAction) {
                    sender.tell(new Response(), ActorRef.noSender());
                    log.info("Respond to {}", sender);
                }
                return noAutoPilot();
            }
        });
相关问题