How send messages to akka system in neighbouring jvm?

1.8k views Asked by At

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

There are 1 answers

0
Vaibhav Raj On BEST ANSWER

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.