I am in the process of learning Akka remoting and clustering. I am not getting the expected behavior when I am trying to send a message to a remote Actor.
Remote Actor deployed on localhost:2552
object SimpleActor {
def apply(): Behavior[String] = Behaviors.receive { (context, message) =>
context.log.info(s"Simple Actor System got a message: $message")
Behaviors.same
}
def registerActor(): Behavior[Unit] = Behaviors.setup { context =>
val simpleActor = context.spawnAnonymous(SimpleActor())
context.system.receptionist ! Receptionist.Register(MyServiceKey, simpleActor)
context.log.info(s"Registered SimpleActor to ${MyServiceKey.toString}")
Behaviors.empty
}
}
object SimpleActorSystem {
def main(args: Array[String]): Unit = {
ActorSystem(SimpleActor.registerActor(), "SimpleActorSystemRemote",
ConfigFactory.load("part2_remoting/remoteActors.conf").getConfig("remoteSystem"))
}
}
When the above code is run, I see SimpleActor getting successfully registered to MyServiecKey.
Main actor trying to send a message to remote Actor:
object Pinger {
def apply(pingService: ActorRef[String], message: String): Behavior[Unit] = Behaviors.setup { _ =>
pingService ! message
Behaviors.empty
}
}
object Guardian {
def apply(): Behavior[Receptionist.Listing] = Behaviors.setup[Receptionist.Listing] { context =>
context.system.receptionist ! Receptionist.Subscribe(MyServiceKey, context.self)
Behaviors.receiveMessagePartial[Receptionist.Listing] {
case MyServiceKey.Listing(listings) =>
println(s"Size of listings: ${listings.size}")
listings.foreach(ref => context.spawnAnonymous(Pinger(ref, "Hello from Remote Actor!")))
Behaviors.same
}
Behaviors.same
}
}
def main(args: Array[String]): Unit = {
ActorSystem(Guardian(), "Guardian")
}
Configuration details for ActorSystem:
akka {
actor {
provider = remote
}
remote {
artery {
enabled = on
transport = aeron-udp
canonical.hostname = "localhost"
canonical.port = 2551
}
}
}
remoteSystem {
akka {
actor {
provider = remote
}
remote {
artery {
enabled = on
transport = aeron-udp
canonical.hostname = "localhost"
canonical.port = 2552
}
}
}
}
Expected Behavior:
Simple Actor System got a message: Hello from Remote Actor!
Is clustering required to send a message to an Actor?
Appreciate any help!