Skip to content

Instantly share code, notes, and snippets.

@Charlyzzz
Last active December 29, 2020 13:59
Show Gist options
  • Save Charlyzzz/269b02e24211faf210cf32ef9a89a97d to your computer and use it in GitHub Desktop.
Save Charlyzzz/269b02e24211faf210cf32ef9a89a97d to your computer and use it in GitHub Desktop.
interface Package
object Echo : Package
object Broadcast : Package
data class Children(val response: Receptionist.Listing) : Package
val childrenServiceKey: ServiceKey<Echo> = ServiceKey.create(Echo::class.java, "children")
val father: Behavior<Package> = Behaviors.setup<Package> { ctx ->
val receptionistAdapter = ctx.messageAdapter(Receptionist.Listing::class.java) { Children(it) }
repeat(2) {
val child = ctx.spawnAnonymous(child)
ctx.system.receptionist().tell(Receptionist.register(childrenServiceKey, child))
}
Behaviors.receive<Package> { context, msg ->
when (msg) {
is Echo -> println("Echo from ${ctx.self.path()}")
is Broadcast -> {
println("Echo from ${ctx.self.path()}")
context.system.receptionist().tell(Receptionist.find(childrenServiceKey, receptionistAdapter))
}
is Children -> msg.response.serviceInstances(childrenServiceKey).foreach { it.tell(Echo) }
}
Behavior.same()
}
}
val child: Behavior<Echo> = Behaviors.receive<Echo> { ctx, _ ->
println("Echo from ${ctx.self.path()}")
Behavior.stopped()
}
fun main() {
val system = ActorSystem.create(father, "test")
system.tell(Echo)
system.tell(Broadcast)
}
Echo from akka://test/user
Echo from akka://test/user
Echo from akka://test/user/$b
Echo from akka://test/user/$c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment