Actor 的实现 by kotlin
JDK 8+
kotlin actor implement, because kotlin have 'coroutine' to use.
and also provide 'thread' to handle actor message
使用 kotlin 实现只是为了更高效率的 coroutine
<dependency>
<groupId>com.neko233</groupId>
<artifactId>actor-kotlin</artifactId>
<version>0.1.0</version>
</dependency>
implementation("com.neko233:actor-kotlin:0.1.0")
- Actor online/offline 两种状态. 当 offline 时候也能处理.
- Actor all operator will through actor-system schedule.
ActorSystem
- Actor
- ActorWorkerCenter
ActorSystem Lifecycle:
- start
- shutdown
class AnnotationActor(actorId: String) : Actor(actorId) {
companion object {
@JvmStatic
private val LOGGER = LoggerFactory.getLogger(AnnotationActor::class.java)!!
}
@ActorMethodOnline
fun onlineFun(
sender: Actor,
data: String,
) {
LOGGER.warn(
"\nfromActorId={}, toActorId={}, data={}\n", sender.getActorId(),
this.getActorId(),
data
)
}
@ActorMethodOffline
fun offlineFun(
toActorId: String,
data: OfflineMockData,
) {
LOGGER.warn(
"\nmyActorId={}, toActorId={}, data={}\n", this.getActorId(),
toActorId,
data
)
}
}
@Test
fun demo() {
// create a Actor-System name of "demo"
val actorSystem = ActorApp233.run("demo")
// register actor
val actor1 = AnnotationActor("demo")
actorSystem.addActor(actor1)
println()
// send to online actor (myself)
actor1.send("demo", "demo")
actor1.send("demo", "test-thread2")
// online-actor send to offline-actor
actor1.send(
"demo233", OfflineMockData(
name = "offline-thread1"
)
)
actor1.send(
"demo233", OfflineMockData(
name = "offline-thread2"
)
)
println()
}
}