如何開發一個簡單的Akka Java應用
Akka是一個免費的開源工具包和運行時,用于在JVM上構建高度并發,分布式和彈性消息驅動的應用程序。除Akka之外,您還具有Akka-streams模塊,該模塊使流的提取和處理變得容易,Alpakka是基于Reactive Streams和Akka的Java和Scala的Reactive Enterprise Integration庫。這里重點介紹如何使用Java創建Akka項目并將其打包。
您已經知道Akka是基于Scala構建的,因此為什么要使用Java而不是Scala?選擇Java有多種原因。
Akka是在JVM上運行的工具包,因此您無需精通Scala即可使用它。 您可能已經有一個精通Java的團隊,但沒有Scala的團隊。 如果您已經具有基于Java的代碼庫和各種構建工具(Maven等),則進行評估要容易得多。這里采用簡單的方法,并從lightbend quickstart下載應用程序。
經過一些調整后,maven文件將如下所示,請注意,我們將使用lombok。
<project> <modelVersion>4.0.0</modelVersion> <groupId>com.gkatzioura</groupId> <artifactId>akka-java-app</artifactId> <version>1.0</version> <properties> <akka.version>2.6.10</akka.version> </properties> <dependencies><dependency> <groupId>com.typesafe.akka</groupId> <artifactId>akka-actor-typed_2.13</artifactId> <version>${akka.version}</version></dependency><dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version></dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.16</version> <scope>provided</scope></dependency> </dependencies> <build><plugins> <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.0</version><configuration> <source>11</source> <target>11</target></configuration> </plugin> <plugin><groupId>org.codehaus.mojo</groupId><artifactId>exec-maven-plugin</artifactId><version>1.6.0</version><configuration> <executable>java</executable> <arguments><argument>-classpath</argument><classpath /><argument>com.gkatzioura.Application</argument> </arguments></configuration> </plugin></plugins> </build></project>
現在有一個Actor負責管理您的其他Actor。這是稱為“守衛Acotr”的頂級Actor。它與ActorSystem一起創建,并且當它停止時,ActorSystem也將停止。
為了創建一個actor,您定義該actor將會收到的消息,并指定它會對這些消息響應什么。
package com.gkatzioura; import akka.actor.typed.Behavior;import akka.actor.typed.javadsl.AbstractBehavior;import akka.actor.typed.javadsl.ActorContext;import akka.actor.typed.javadsl.Behaviors;import akka.actor.typed.javadsl.Receive;import lombok.AllArgsConstructor;import lombok.Getter; public class AppGuardian extends AbstractBehavior<AppGuardian.GuardianMessage> { public interface GuardianMessage {} static Behavior<GuardianMessage> create() {return Behaviors.setup(AppGuardian::new); } @Getter @AllArgsConstructor public static class MessageToGuardian implements GuardianMessage {private String message; } private AppGuardian(ActorContext<GuardianMessage> context) {super(context); } @Override public Receive<GuardianMessage> createReceive() {return newReceiveBuilder().onMessage(MessageToGuardian.class, this::receiveMessage).build(); } private Behavior<GuardianMessage> receiveMessage(MessageToGuardian messageToGuardian) {getContext().getLog().info('Message received: {}',messageToGuardian.getMessage());return this; } }
Akka是消息驅動的,因此這個“守衛Acotr”接受到發送給它的消息。這樣,那些實現GuardianMessage接口的消息將在這里receiveMessage()方法中處理。
當這個actor被創建時,createReceive方法用于指示如何處理接到的消息,這里是委托給receiveMessage()方法。
請注意,在進行日志記錄時,不要在類中使用記錄器,而應使用getContext().getLog()
在幕后,日志消息將自動添加actor的路徑作為akkaSource映射診斷上下文(MDC)值。
最后一步是添加Main類。
package com.gkatzioura; import java.io.IOException; import akka.actor.typed.ActorSystem;import lombok.extern.slf4j.Slf4j; @Slf4jpublic class Application { public static final String APP_NAME = 'akka-java-app'; public static void main(String[] args) {final ActorSystem<AppGuardian.GuardianMessage> appGuardian = ActorSystem.create(AppGuardian.create(), APP_NAME);appGuardian.tell(new AppGuardian.MessageToGuardian('First Akka Java App')); try { System.out.println('>>> Press ENTER to exit <<<'); System.in.read();}catch (IOException ignored) {}finally { appGuardian.terminate();} } }
這里希望實現的效果是:讓我們的“守衛Acotr”打印提交的消息。按下Enter鍵,Akka應用程序將通過監護人終止。與往常一樣,您可以在github上找到源代碼。
以上就是如何開發一個簡單的Akka Java應用 的詳細內容,更多關于Akka Java應用 的資料請關注好吧啦網其它相關文章!
相關文章: