如何開發(fā)一個簡單的Akka Java應(yīng)用
Akka是一個免費的開源工具包和運行時,用于在JVM上構(gòu)建高度并發(fā),分布式和彈性消息驅(qū)動的應(yīng)用程序。除Akka之外,您還具有Akka-streams模塊,該模塊使流的提取和處理變得容易,Alpakka是基于Reactive Streams和Akka的Java和Scala的Reactive Enterprise Integration庫。這里重點介紹如何使用Java創(chuàng)建Akka項目并將其打包。
您已經(jīng)知道Akka是基于Scala構(gòu)建的,因此為什么要使用Java而不是Scala?選擇Java有多種原因。
Akka是在JVM上運行的工具包,因此您無需精通Scala即可使用它。 您可能已經(jīng)有一個精通Java的團隊,但沒有Scala的團隊。 如果您已經(jīng)具有基于Java的代碼庫和各種構(gòu)建工具(Maven等),則進行評估要容易得多。這里采用簡單的方法,并從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負責(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ā)送給它的消息。這樣,那些實現(xiàn)GuardianMessage接口的消息將在這里receiveMessage()方法中處理。
當(dāng)這個actor被創(chuàng)建時,createReceive方法用于指示如何處理接到的消息,這里是委托給receiveMessage()方法。
請注意,在進行日志記錄時,不要在類中使用記錄器,而應(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();} } }
這里希望實現(xiàn)的效果是:讓我們的“守衛(wèi)Acotr”打印提交的消息。按下Enter鍵,Akka應(yīng)用程序?qū)⑼ㄟ^監(jiān)護人終止。與往常一樣,您可以在github上找到源代碼。
以上就是如何開發(fā)一個簡單的Akka Java應(yīng)用 的詳細內(nèi)容,更多關(guān)于Akka Java應(yīng)用 的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. XML入門的常見問題(三)2. HTTP協(xié)議常用的請求頭和響應(yīng)頭響應(yīng)詳解說明(學(xué)習(xí))3. XML 非法字符(轉(zhuǎn)義字符)4. XML在語音合成中的應(yīng)用5. .NET Core 分布式任務(wù)調(diào)度ScheduleMaster詳解6. jscript與vbscript 操作XML元素屬性的代碼7. 不要在HTML中濫用div8. .NET Framework各版本(.NET2.0 3.0 3.5 4.0)區(qū)別9. ASP將數(shù)字轉(zhuǎn)中文數(shù)字(大寫金額)的函數(shù)10. HTML5實戰(zhàn)與剖析之觸摸事件(touchstart、touchmove和touchend)
