A small actor system written in kotlin using Coroutines.
Important
The project is in a very early stage; thus, breaking changes should be expected.
🏠 Homepage (under construction)
implementation("io.github.smyrgeorge:actor4k:x.y.z")
data class Req(val msg: String)
data class Resp(val msg: String)
data class AccountActor(
override val shard: String,
override val key: String
) : Actor(shard, key) {
override suspend fun onBeforeActivate() {
log.info { "[${address()}] before-activate" }
}
override suspend fun onActivate(m: Message) {
log.info { "[${address()}] activate ($m)" }
}
override fun onReceive(m: Message, r: Response.Builder): Response {
val msg = m.cast<Req>()
log.info { "[$name] Received message: $msg" }
val res = Resp("Pong!")
return r.value(res).build()
}
}
Now let's send some messages:
val a: Actor.Ref = ActorSystem.get(AccountActor::class, "ACC0010")
val req = Req(msg = "[tell] Hello World!")
a.tell(req)
val req2 = Req(msg = "[ask] Ping!")
val r = a.ask<Resp>(req2)
println(r)
See other examples here.
We provide special utilities to accomplish this. Whenever you need to, simply call the asJava() method and the magic will happen.
For instance take a look here.
ActorSystem system = ActorSystem.INSTANCE.start(ActorSystem.INSTANCE.getConf());
Actor.Ref ref = system.getRegistry().asJava().get(AccountActor.class, "ACC00011").join();
System.out.println(ref);
./gradlew build