【问题标题】:How to build an akka project without a Behavior create() in java?java - 如何在java中构建一个没有行为create()的akka​​项目?
【发布时间】:2021-05-25 15:45:42
【问题描述】:

我是 java 中 akka 的初学者。我对如何让演员在后端和前端进行交流很感兴趣。 我正在通过 akka 文档学习,我正在使用 this page 的后端和前端。 在这些示例中,没有主要内容,我正在尝试这样做以了解代码的工作原理。 由于我不是很先进,我只知道如何使用 Behavior create 创建和使用 Actor,但这些代码没有扩展 Abstractactor 并且没有方法 Behavior create()。

我编写了这段代码,但我必须初始化演员系统才能使其工作。你能帮我理解一下命令和响应和请求接口是如何工作的,因为它们是空的,以及如何使用后端和前端吗?

public class Main {
    @SuppressWarnings("unchecked")
    final static ActorSystem<Frontend.Command> system;
    //final static ActorRef<Frontend.Command> User = context.spawn(Frontend.WrappedBackendResponse(), "User");
    public static void main(String[] args) {
        Backend.Response response = new Backend.JobStarted(5);
        
        /*
         * final ActorSystem<Translator.Frontend> system =
         * ActorSystem.create(Translator.create(), "hello");
         */
        system.tell(new Frontend.WrappedBackendResponse(response));

    } 

}

【问题讨论】:

    标签: java akka actor


    【解决方案1】:

    您可以通过创建两个参与者系统(后端和前端)来设置该示例中前端/后端代码的简单版本,如下所示:

       public static void main(final String[] args)
       {    
          // Setup a simple backend functionally
          final ActorSystem<Backend.Request> backend = ActorSystem.create(Behaviors.setup(ctx -> simpleBackend(ctx)), "backend");
    
          // Setup the frontend via the given class, using the backend actorSystem as a reference
          final ActorSystem<Frontend.Command> frontend = ActorSystem.create(Behaviors.setup(ctx -> new Frontend.Translator(ctx, backend)), "frontend");
    
       }
    
       // Simple function to create a behavior as the backend 
       private static Behavior<Backend.Request> simpleBackend(final ActorContext<Backend.Request> ctx)
       {
          return Behaviors.receive(Backend.Request.class)
                // fill in here with more stuff to actually do the backend handling
                .onAnyMessage(any -> {
                   ctx.getLog().info("Received {}", any);
                   return Behaviors.same();
                })
                .build();
       }
    

    这并不能解决所有问题,但它会给你一个实验的基础,并尝试如果你添加背景行为和合作会发生什么。

    【讨论】:

      猜你喜欢
      • 2014-01-10
      • 2016-01-26
      • 2020-09-16
      • 2020-08-08
      • 1970-01-01
      • 2020-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多