繁体   English   中英

如何在 java 中构建没有行为 create() 的 akka 项目?

[英]How to build an akka project without a Behavior create() in java?

我是 java 中 akka 的初学者。 我对如何让演员在后端和前端进行交流很感兴趣。 我正在通过 akka 文档进行学习,并且我正在使用此页面中的后端和前端。 在这些示例中,没有 main ,我正在尝试这样做以了解代码的工作原理。 由于我不是很先进,我只知道如何使用 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));

    } 

}

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM