I started akka system with HelloActor in one JVM and trying to send messages to it from client in another JVM. And nothing works. How I should do that correctly? Here is the code:
Simple Server
package akkaSample.severalSystems
import akka.actor.{Props, Actor, ActorSystem}
import com.typesafe.config.ConfigFactory
class HelloActor extends Actor {
override def preStart(): Unit = {
println("Hello actor started")
}
def receive = {
case "mew" => println("I said mew")
case "hello" => println("hello back at you")
case "shutdown" => context.stop(self)
case _ => println("huh?")
}
}
object Server extends App {
val root = ConfigFactory.load()
val one = root.getConfig("systemOne")
val system = ActorSystem("HelloSystem", one)
val helloActor = system.actorOf(Props[HelloActor], "HelloActor")
println (system)
println("Remote application started.")
}
Simple Client
package akkaSample.severalSystems.client
import akka.actor.ActorSystem
import com.typesafe.config.ConfigFactory
import scala.util.control.Breaks._
class Client {
}
object Client extends App {
println("started")
val root = ConfigFactory.load()
val two = root.getConfig("systemTwo")
val system = ActorSystem("mySystem", two)
//What should be there to access HelloActor?
val selection = ...
selection ! "mew"
println(selection.anchorPath)
}
configuration file
systemOne {
akka {
remote {
enabled-transports = ["akka.remote.netty.tcp"]
netty.tcp {
port = 2552
}
}
}
}
systemTwo {
akka {
remote {
enabled-transports = ["akka.remote.netty.tcp"]
netty.tcp {
port = 2553
}
}
}
}
It assume that Server starts before Client. Now I receive "akka://mySystem/deadLetters" (println(selection.anchorPath)
) when try to get actor like that system.actorSelection("akka://[email protected]:2552/HelloActor")
So how to retreive actor from ActorSystem in another JVM? Or it is not possible until I create a cluster, appoint seeds, leader and so on?
1) Akka Actor tell() Method It works on "fire-forget" approach. You can also use ! (bang) exclamation mark to send message. This is the preferred way of sending messages.
The Akka HTTP server backend is the default in Play. You can also use the Netty backend if you choose.
Akka has two ways of using remoting: Lookup : used to look up an actor on a remote node with actorSelection(path) Creation : used to create an actor on a remote node with actorOf(Props(...), actorName)
at-least-once delivery means that for each message handed to the mechanism potentially multiple attempts are made at delivering it, such that at least one succeeds; again, in more casual terms this means that messages may be duplicated but not lost.
It seems you missed adding RemoteActorRefProvider in the configuration. Your actor configuration should be like follows:
systemOne {
akka {
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
enabled-transports = ["akka.remote.netty.tcp"]
netty.tcp {
port = 2552
}
}
}
}
systemTwo {
akka {
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
enabled-transports = ["akka.remote.netty.tcp"]
netty.tcp {
port = 2553
}
}
}
}
Actor selection is done using
context.actorSelection("akka.tcp://[email protected]:2552/user/actorName")
You do not need to create an Akka Cluster to enable remoting capabilities.
This configuration change RemoteActorRefProvider
requires akka-remote
as a dependency.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With