Java——akka actor 简单使用方法
1、pom.xml添加
<dependency> <groupId>com.typesafe.akka</groupId> <artifactId>akka-actor_2.12</artifactId> <version>2.5.12</version> </dependency>
2、向自定义Actor发送一个字符串
HiActor.java
package com.sfire.m2test.akka; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import akka.actor.AbstractActor; import akka.actor.ActorRef; import akka.actor.ActorSystem; import akka.actor.Props; /** * @FileName: HiActor.java * @Author : chong * @CreateTime: Apr 27, 2022 3:55:01 PM * @Version: 1.0.0 */ public class HiActor extends AbstractActor { private final static Logger LOGGER = LoggerFactory.getLogger(HiActor.class); @Override public Receive createReceive() { return receiveBuilder().match(String.class, result -> { LOGGER.info("hi , i am an actor..., i get result = " + result); }).build(); } public static void main(String[] args) { // 定义一个Actor管理仓库 ActorSystem DEFAULT = ActorSystem.create("blaze"); // 把自定义的Actor放入仓库并起个名字 ActorRef actorRef = DEFAULT.actorOf(Props.create(HiActor.class), "hi_actor"); // 告诉HiActor一些消息 actorRef.tell("something...", actorRef.noSender()); } }
3、向OneActor发送一个对象,OneActor把收到对象的age参数加1,再转发给TwoActor, OneActor再把age加1,回发给OneActor
OneActor.java
package com.sfire.m2test.akka; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.alibaba.fastjson.JSONObject; import akka.actor.AbstractActor; import akka.actor.ActorRef; import akka.actor.ActorSystem; import akka.actor.Props; /** * @FileName: OneActor.java * @Author : chong * @CreateTime: Apr 27, 2022 4:08:51 PM * @Version: 1.0.0 */ public class OneActor extends AbstractActor { private final static Logger LOGGER = LoggerFactory.getLogger(OneActor.class); @Override public Receive createReceive() { return receiveBuilder().match(SomeOne.class, someOne -> { JSONObject json = new JSONObject(); json.put("someOne", someOne); LOGGER.info(JSONObject.toJSONString(someOne)); // 把收到的消息对象,转而发给 TwoActor ActorRef actorRef = this.getContext().actorOf(Props.create(TwoActor.class, TwoActor::new)); someOne.setAge(someOne.getAge() + 1); actorRef.tell(someOne, this.getSelf()); }).build(); } public static void main(String[] args) { // 定义Actor管理仓库 ActorSystem actorSystem = ActorSystem.create("blaze"); // 把OneActor放入仓库 ActorRef actorRef = actorSystem.actorOf(Props.create(OneActor.class, OneActor::new), "one_actor"); // 向OneActor发送一个消息对象 SomeOne someOne = new SomeOne(1, "liming", 19); actorRef.tell(someOne, actorRef.noSender()); } }
TwoActor.java
package com.sfire.m2test.akka; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.alibaba.fastjson.JSONObject; import akka.actor.AbstractActor; import akka.japi.pf.ReceiveBuilder; /** * @FileName: TwoActor.java * @Author : chong * @CreateTime: Apr 27, 2022 4:36:06 PM * @Version: 1.0.0 */ public class TwoActor extends AbstractActor { private final static Logger LOGGER = LoggerFactory.getLogger(OneActor.class); @Override public Receive createReceive() { ReceiveBuilder builder = ReceiveBuilder.create(); builder.match(SomeOne.class, someOne -> { LOGGER.info(JSONObject.toJSONString(someOne)); Thread.sleep(1000); // 把消息再回发给发送者 someOne.setAge(someOne.getAge() + 1); this.getSender().tell(someOne, getSelf()); }); return builder.build(); } }
SomeOne.java
package com.sfire.m2test.akka; /** * @FileName: SomeInfo.java * @Author : chong * @CreateTime: Apr 27, 2022 4:06:55 PM * @Version: 1.0.0 */ public class SomeOne { private Integer id; private String name; private Integer age; public SomeOne(int id, String name, Integer age) { this.id = id; this.name = name; this.age = age; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }