Skip to content

smyrgeorge/actor4k

Repository files navigation

actor4k

Build Maven Central GitHub License GitHub commit activity GitHub issues Kotlin

A small actor system written in kotlin using Coroutines.

Important

The project is in a very early stage; thus, breaking changes should be expected.

📖 Documentation

🏠 Homepage (under construction)

Usage

implementation("io.github.smyrgeorge:actor4k:x.y.z")

Let's create an Actor!

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.

Working with Java

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);

Build

./gradlew build

Links and References