这里首先按照官方文档测试下编写独立功能单一的 actor, 命名为 fusion-actor 作为日常可能用到测试项目
可以不提交版本库本地测试
这里第三方库依赖如下, 按照官方文档处理:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>fusion-actor</artifactId>
<!-- 继承框架 -->
<parent>
<groupId>com.meteorcat.fusion</groupId>
<artifactId>fusion-framework</artifactId>
<version>2024.4.19-SNAPSHOT</version>
</parent>
<!-- 第三方依赖 -->
<dependencies>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- Actor LogBack -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<!-- Actor -->
<dependency>
<groupId>org.apache.pekko</groupId>
<artifactId>pekko-actor-typed_${scala.binary.version}</artifactId>
</dependency>
</dependencies>
<!-- 打包配置 -->
<build>
<plugins>
<!-- 这里利用 springframework 工具打包 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.meteorcat.fusion.FusionActorApplication</mainClass>
<skip>false</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
注意在根目录的 pom.xml 需要追加子项目依赖:
<!-- 子模块定义 -->
<modules>
<module>actor</module>
</modules>
这里主要编写两个主要功能, 分别负责入口和Actor系统:
/// src/main/java/com/meteorcat/fusion/FusionActorApplication.java
package com.meteorcat.fusion;
import org.apache.pekko.actor.typed.ActorSystem;
import java.io.IOException;
/**
* 游戏项目启动入口
*/
public class FusionActorApplication {
/**
* 启动项目方法
*
* @param args 启动方法
*/
public static void main(String[] args) {
// 初始化 Actor 系统
final ActorSystem<FusionActorSupervisor.Message> actorSystem = ActorSystem.create(
FusionActorSupervisor.create(),
"fusion-actor-supervisor"
);
// 给系统推送消息
actorSystem.tell(new FusionActorSupervisor.Message("hello.world", null));
// 回车关闭 actor
try {
System.out.println(">>> Press ENTER to exit <<<");
int ignore = System.in.read();
} catch (IOException ignored) {
} finally {
actorSystem.terminate();
}
}
}
/// src/main/java/com/meteorcat/fusion/FusionActorSupervisor.java
package com.meteorcat.fusion;
import org.apache.pekko.actor.typed.ActorRef;
import org.apache.pekko.actor.typed.Behavior;
import org.apache.pekko.actor.typed.javadsl.AbstractBehavior;
import org.apache.pekko.actor.typed.javadsl.ActorContext;
import org.apache.pekko.actor.typed.javadsl.Behaviors;
import org.apache.pekko.actor.typed.javadsl.Receive;
import org.slf4j.Logger;
/**
* 简单的 Actor 服务
*/
public class FusionActorSupervisor extends AbstractBehavior<FusionActorSupervisor.Message> {
final Logger log = getContext().getLog();
/**
* 初始化
*
* @param context Actor上下文句柄
*/
public FusionActorSupervisor(ActorContext<Message> context) {
super(context);
}
/**
* 传递的消息结构
*
* @param message 消息内容
* @param reply 请求 Actor 的地址
*/
public record Message(String message, ActorRef<Message> reply) {
}
/**
* 静态注册对象
*
* @return Behavior
*/
public static Behavior<Message> create() {
return Behaviors.setup(FusionActorSupervisor::new);
}
/**
* 构建回调匹配
*
* @return Receive
*/
@Override
public Receive<Message> createReceive() {
return newReceiveBuilder()
.onMessage(Message.class, this::onMessage)
.build();
}
/**
* 回调内部
*
* @param msg 消息
* @return Behavior
*/
private Behavior<Message> onMessage(Message msg) {
log.info("ECHO : {}!", msg.message);
// 返回结果让其他 Actor 接收
if (msg.reply != null) {
msg.reply.tell(new Message(msg.message, getContext().getSelf()));
}
return this;
}
}
这里就是简单启动 actor 的系统配置, 也就我们的启动和挂载第一个 actor 服务;
现在这个系统什么作用都没有, 后续就是准备给他来点我们日常会使用的功能, 让其真正运行起来.