akka演员没有按预期工作

时间:2015-03-27 19:19:09

标签: java akka

这里有akka新手。目前正在构建一个系统来调用一些Web服务并更新数据库......但是akka actor并没有完全按照预期工作...我的代码示例......

Application Runner

public class Application
{
  public static void main(String[] args) throws InterruptedException
  {
    ActorSystem system = ActorSystem.create("system");
    ActorRef master = system.actorOf(Props.create(MasterActor.class));
    String url = "http://some-web-service-url";
    master.tell(url, ActorRef.noSender());
    system.shutdown();
  }
}

MasterActor

public class MasterActor extends UntypedActor
{
  private final LoggingAdapter log = Logging.getLogger(getContext().system(), getSelf());
  private final ActorRef childActor = getContext().actorOf(Props.create(ChildActor.class));    

  @Override
  public void onReceive(Object message) throws Exception
  {
    if(message instanceof String)
    {
      childActor.tell(message, getSelf());
    }else if(message instanceof Boolean){
      log.info("all done");
    }else {
      unhandled(message);
    }
  }
}

ChildActor

public class ChildActor extends UntypedActor
{
  private final LoggingAdapter log = Logging.getLogger(getContext().system(), getSelf());

  @Override
  public void onReceive(Object message) throws Exception
  {
    if (message instanceof String) {
      String url = (String) message;
      Integer result = getWebServiceResult(url);
      log.info("result: {}", result);
      getSender().tell(true, getSelf());
    }else {
      unhandled(message);
    }
  }

  private Integer getWebServiceResult(final String url) throws Exception
  {
    ExecutionContextExecutor executor = getContext().dispatcher();
    Future<Integer> future = Futures.future(new Callable<Integer>()
    {
      @Override
      public Integer call() throws Exception
      {
        return new HttpClient().fetchData(url); //some web service call
      }
    }, executor);
    return (Integer) Await.result(future, Duration.create(7000, TimeUnit.SECONDS));
  }
}

但是儿童演员无法将消息发送给发件人,主人......收到此错误堆栈...

[INFO] [03/28/2015 01:02:45.521] [system-akka.actor.default-dispatcher-3] [akka://system/user/$a/$a] result: TheWebservice Result
[INFO] [03/28/2015 01:02:45.528] [system-akka.actor.default-dispatcher-4] [akka://system/user/$a] Message [java.lang.Boolean] from Actor[akka://system/user/$a/$a#1601481298] to Actor[akka://system/user/$a#1257171720] was not delivered. [1] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.

我无法找到错误(花了3天)...在我看来,这段代码应该有效......你能告诉我我做错了什么吗?

提前致谢...

2 个答案:

答案 0 :(得分:3)

你有竞争条件。您正在关闭ActorSystem

system.shutdown();

在儿童演员有机会发送回复之前。请记住,akka中的所有内容都是异步的。

例如,添加

Thread.sleep(someTime);

shutdown之前看到发送和接收的消息。

答案 1 :(得分:0)

只是处理退出部分: 您可以注册一个如下所示的关闭钩子并关闭其中的akka​​系统

   Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                system.shutdown();
            }
        });

然后在等待处理完成时进入一个永不停止的循环,如下所示,当完成处理后,在终端上按Ctrl + C或在代码中引发System.exit(0)。

while(false == shutdownFlag){
  Thread.sleep(sometime);
}