如何開發(fā)一個簡單的Akka Java應(yīng)用
Akka是一個免費(fèi)的開源工具包和運(yùn)行時,用于在JVM上構(gòu)建高度并發(fā),分布式和彈性消息驅(qū)動的應(yīng)用程序。除Akka之外,您還具有Akka-streams模塊,該模塊使流的提取和處理變得容易,Alpakka是基于Reactive Streams和Akka的Java和Scala的Reactive Enterprise Integration庫。這里重點(diǎn)介紹如何使用Java創(chuàng)建Akka項目并將其打包。
您已經(jīng)知道Akka是基于Scala構(gòu)建的,因此為什么要使用Java而不是Scala?選擇Java有多種原因。
Akka是在JVM上運(yùn)行的工具包,因此您無需精通Scala即可使用它。 您可能已經(jīng)有一個精通Java的團(tuán)隊,但沒有Scala的團(tuán)隊。 如果您已經(jīng)具有基于Java的代碼庫和各種構(gòu)建工具(Maven等),則進(jìn)行評估要容易得多。這里采用簡單的方法,并從lightbend quickstart下載應(yīng)用程序。
經(jīng)過一些調(diào)整后,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>
現(xiàn)在有一個Actor負(fù)責(zé)管理您的其他Actor。這是稱為“守衛(wèi)Acotr”的頂級Actor。它與ActorSystem一起創(chuàng)建,并且當(dāng)它停止時,ActorSystem也將停止。
為了創(chuàng)建一個actor,您定義該actor將會收到的消息,并指定它會對這些消息響應(yīng)什么。
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是消息驅(qū)動的,因此這個“守衛(wèi)Acotr”接受到發(fā)送給它的消息。這樣,那些實(shí)現(xiàn)GuardianMessage接口的消息將在這里receiveMessage()方法中處理。
當(dāng)這個actor被創(chuàng)建時,createReceive方法用于指示如何處理接到的消息,這里是委托給receiveMessage()方法。
請注意,在進(jìn)行日志記錄時,不要在類中使用記錄器,而應(yīng)使用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();} } }
這里希望實(shí)現(xiàn)的效果是:讓我們的“守衛(wèi)Acotr”打印提交的消息。按下Enter鍵,Akka應(yīng)用程序?qū)⑼ㄟ^監(jiān)護(hù)人終止。與往常一樣,您可以在github上找到源代碼。
以上就是如何開發(fā)一個簡單的Akka Java應(yīng)用 的詳細(xì)內(nèi)容,更多關(guān)于Akka Java應(yīng)用 的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 淺談SpringMVC jsp前臺獲取參數(shù)的方式 EL表達(dá)式2. 關(guān)于Ajax跨域問題及解決方案詳析3. jsp+servlet簡單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))4. 刪除docker里建立容器的操作方法5. JavaScript實(shí)現(xiàn)組件化和模塊化方法詳解6. .Net Core和RabbitMQ限制循環(huán)消費(fèi)的方法7. ASP.NET MVC遍歷驗證ModelState的錯誤信息8. SpringMVC+Jquery實(shí)現(xiàn)Ajax功能9. jsp網(wǎng)頁實(shí)現(xiàn)貪吃蛇小游戲10. ASP中if語句、select 、while循環(huán)的使用方法
