Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 阿克卡的演员们没有按预期工作_Java_Akka - Fatal编程技术网

Java 阿克卡的演员们没有按预期工作

Java 阿克卡的演员们没有按预期工作,java,akka,Java,Akka,这里是阿卡新手。目前正在构建一个系统来调用一些web服务和更新数据库。。。但是akka演员并没有完全按照预期工作…我的代码示例 应用程序运行程序 public class Application { public static void main(String[] args) throws InterruptedException { ActorSystem system = ActorSystem.create("system"); ActorRef master =

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

应用程序运行程序

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();
  }
}
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);
    }
  }
}
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));
  }
}
主角

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();
  }
}
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);
    }
  }
}
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));
  }
}
儿童演员

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();
  }
}
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);
    }
  }
}
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));
  }
}
我找不到问题所在(花了3天时间)。。。在我看来,这个代码应该可以工作…你能告诉我我做错了什么吗


提前感谢…

您有比赛条件。您正在关闭
ActorSystem

system.shutdown();
在儿童演员有机会回复之前。请记住,akka中的所有内容或多或少都是异步的

例如,添加一个

Thread.sleep(someTime);

关闭之前
,查看发送和接收的消息

你有比赛条件。您正在关闭
ActorSystem

system.shutdown();
在儿童演员有机会回复之前。请记住,akka中的所有内容或多或少都是异步的

例如,添加一个

Thread.sleep(someTime);

关闭之前
,查看发送和接收的消息

你有比赛条件。您正在关闭
ActorSystem

system.shutdown();
在儿童演员有机会回复之前。请记住,akka中的所有内容或多或少都是异步的

例如,添加一个

Thread.sleep(someTime);

关闭之前
,查看发送和接收的消息

你有比赛条件。您正在关闭
ActorSystem

system.shutdown();
在儿童演员有机会回复之前。请记住,akka中的所有内容或多或少都是异步的

例如,添加一个

Thread.sleep(someTime);

关闭之前
,查看发送和接收的消息

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

   Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                system.shutdown();
            }
        });
然后,在等待处理完成的同时,进入如下所示的永无止境的循环,以便在处理完成后,按终端上的Ctrl+C或启动系统。在代码中退出(0)

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

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

   Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                system.shutdown();
            }
        });
然后,在等待处理完成的同时,进入如下所示的永无止境的循环,以便在处理完成后,按终端上的Ctrl+C或启动系统。在代码中退出(0)

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

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

   Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                system.shutdown();
            }
        });
然后,在等待处理完成的同时,进入如下所示的永无止境的循环,以便在处理完成后,按终端上的Ctrl+C或启动系统。在代码中退出(0)

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

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

   Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                system.shutdown();
            }
        });
然后,在等待处理完成的同时,进入如下所示的永无止境的循环,以便在处理完成后,按终端上的Ctrl+C或启动系统。在代码中退出(0)

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

只是一个初步猜测,但您是否尝试过在发送第一条消息后不立即关闭所有内容?给他们一秒钟左右。只是一个初步的猜测,但你有没有尝试过在发送第一条消息后不立即关闭所有东西?给他们一秒钟左右。只是一个初步的猜测,但你有没有尝试过在发送第一条消息后不立即关闭所有东西?给他们一秒钟左右。只是一个初步的猜测,但你有没有尝试过在发送第一条消息后不立即关闭所有东西?给他们一秒钟左右。线程。睡眠(2000);作品但是我不知道完成web服务调用和数据库调用需要多长时间。。系统不应该等待所有的孩子完成吗?@Shakil系统不能确切地知道孩子们在做什么。如果一个孩子在10小时内产生一个线程来发送一条消息呢?系统是如何知道这一点的?不,不能。你需要一个外部信号来关闭系统。(基本上,系统应该永远存在。)我同意@SotiriosDelimanolis,但是这听起来像是未来/管道模式()的一个很好的用例,例如,
akka.pattern.Patterns.pipe(future,System.dispatcher())。to(actor)。然后,您可以将future稍微更改为返回boolean,或者将master更改为处理Integer类型的消息。一切都将以非阻塞方式处理,只有当web服务结果为future completesThread.sleep(2000)时,系统才会开始关闭过程;作品但是我不知道完成web服务调用和数据库调用需要多长时间。。系统不应该等待所有的孩子完成吗?@Shakil系统不能确切地知道孩子们在做什么。如果一个孩子在10小时内产生一个线程来发送一条消息呢?系统是如何知道这一点的?不,不能。你需要一个外部信号来关闭系统。(基本上,系统应该永远存在。)我同意@SotiriosDelimanolis,但是这听起来像是未来/管道模式()的一个很好的用例,例如,
akka.pattern.Patterns.pipe(future,System.dispatcher())。to(actor)。然后,您可以将future稍微更改为返回boolean,或者将master更改为处理Integer类型的消息。一切都将以非阻塞方式处理,只有当web服务结果为future completesThread.sleep(2000)时,系统才会开始关闭过程;作品但是我不知道完成web服务调用和数据库调用需要多长时间。。系统不应该等待所有的孩子完成吗?@Shakil系统不能确切地知道孩子们在做什么。如果一个孩子在10小时内产生一个线程来发送一条消息呢?系统是如何知道这一点的?不,不能。你需要一个外部信号来关闭系统。(基本上,这个系统应该永远有效