Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How send messages to akka system in neighbouring jvm?

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?

like image 402
Cherry Avatar asked Sep 03 '14 10:09

Cherry


People also ask

How can I send a message to Akka?

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.

Does Akka use Netty?

The Akka HTTP server backend is the default in Play. You can also use the Netty backend if you choose.

What is Akka remoting?

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)

In which message delivery guarantee approach each message is guaranteed delivery to at least one of the components that retrieve messages from the queue?

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.


1 Answers

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.

like image 71
Vaibhav Raj Avatar answered Oct 22 '22 09:10

Vaibhav Raj