簡體   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