如何開(kāi)發(fā)一個(gè)簡(jiǎn)單的Akka Java應(yīng)用
Akka是一個(gè)免費(fèi)的開(kāi)源工具包和運(yùn)行時(shí),用于在JVM上構(gòu)建高度并發(fā),分布式和彈性消息驅(qū)動(dòng)的應(yīng)用程序。除Akka之外,您還具有Akka-streams模塊,該模塊使流的提取和處理變得容易,Alpakka是基于Reactive Streams和Akka的Java和Scala的Reactive Enterprise Integration庫(kù)。這里重點(diǎn)介紹如何使用Java創(chuàng)建Akka項(xiàng)目并將其打包。
您已經(jīng)知道Akka是基于Scala構(gòu)建的,因此為什么要使用Java而不是Scala?選擇Java有多種原因。
Akka是在JVM上運(yùn)行的工具包,因此您無(wú)需精通Scala即可使用它。 您可能已經(jīng)有一個(gè)精通Java的團(tuán)隊(duì),但沒(méi)有Scala的團(tuán)隊(duì)。 如果您已經(jīng)具有基于Java的代碼庫(kù)和各種構(gòu)建工具(Maven等),則進(jìn)行評(píng)估要容易得多。這里采用簡(jiǎn)單的方法,并從lightbend quickstart下載應(yīng)用程序。
經(jīng)過(guò)一些調(diào)整后,maven文件將如下所示,請(qǐng)注意,我們將使用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)在有一個(gè)Actor負(fù)責(zé)管理您的其他Actor。這是稱(chēng)為“守衛(wèi)Acotr”的頂級(jí)Actor。它與ActorSystem一起創(chuàng)建,并且當(dāng)它停止時(shí),ActorSystem也將停止。
為了創(chuàng)建一個(gè)actor,您定義該actor將會(huì)收到的消息,并指定它會(huì)對(duì)這些消息響應(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ū)動(dòng)的,因此這個(gè)“守衛(wèi)Acotr”接受到發(fā)送給它的消息。這樣,那些實(shí)現(xiàn)GuardianMessage接口的消息將在這里receiveMessage()方法中處理。
當(dāng)這個(gè)actor被創(chuàng)建時(shí),createReceive方法用于指示如何處理接到的消息,這里是委托給receiveMessage()方法。
請(qǐng)注意,在進(jìn)行日志記錄時(shí),不要在類(lèi)中使用記錄器,而應(yīng)使用getContext().getLog()
在幕后,日志消息將自動(dòng)添加actor的路徑作為akkaSource映射診斷上下文(MDC)值。
最后一步是添加Main類(lèi)。
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ū)⑼ㄟ^(guò)監(jiān)護(hù)人終止。與往常一樣,您可以在github上找到源代碼。
以上就是如何開(kāi)發(fā)一個(gè)簡(jiǎn)單的Akka Java應(yīng)用 的詳細(xì)內(nèi)容,更多關(guān)于Akka Java應(yīng)用 的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. asp畫(huà)中畫(huà)廣告插入在每篇文章中的實(shí)現(xiàn)方法2. 讓chatgpt將html中的圖片轉(zhuǎn)為base64方法示例3. 使用純HTML的通用數(shù)據(jù)管理和服務(wù)4. ASP編碼必備的8條原則5. asp中Request.ServerVariables的參數(shù)集合6. WMLScript腳本程序設(shè)計(jì)第1/9頁(yè)7. CSS Hack大全-教你如何區(qū)分出IE6-IE10、FireFox、Chrome、Opera8. 熊海CMS代碼審計(jì)漏洞分析9. PHP session反序列化漏洞深入探究10. PHP反序列化漏洞實(shí)例深入解析
