Last active
December 29, 2020 13:59
-
-
Save Charlyzzz/269b02e24211faf210cf32ef9a89a97d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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