Example of a minimal remote actor Akka 2.1

EDIT Please note: I needed to make reverse changes to this https://github.com/akka/akka/commit/ce014ece3568938b2036c4ccfd21b92faba69607#L13L6 in order to make the accepted response work with AKKA 2.1, which is the stable distribution found on the akkas home page!


I read all the training materials that I could find in AKKA, but found nothing out of the box.

Using eclipse, I want to create 2 programs.

Program1: the actor "joe" starts and somehow makes it available at 127.0.0.1:some_port

Program2: gets a link to the actor "joe" at 127.0.0.1:some_port. Sends a welcome message "joe".

Program 1 must print something when a message is received. I want to run this example in eclipse using AKKA 2.1. Can someone list 2 programs (program1 and program2) along with a working application.conf file that does this and nothing more?


edit> let me show you what i got so far:

actor

case class Greeting(who: String) extends Serializable class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) ⇒ println("Hello " + who);log.info("Hello " + who) } } 

Program1

 package test import akka.actor.ActorSystem object Machine1 { def main(args: Array[String]): Unit = { val system = ActorSystem("MySystem") } } 

Program2

 package test import akka.actor.ActorSystem import akka.actor.Props import akka.actor.actorRef2Scala object Machine2 { def main(args: Array[String]): Unit = { val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter") greeter ! Greeting("Felix") } } 

application.conf

 akka { actor { deployment { /greeter { remote = "akka:// MySystem@127.0.0.1 :2553" } } } } 

However, this program works when I run only Program2 and gives:

 Hello Felix [INFO] [02/18/2013 12:27:29.999] [MySystem-akka.actor.default-dispatcher-2] [akka://MySystem/user/greeter] Hello Felix 

It does not seem to pick my application.conf. I tried placing it in the. / Src / and. / Folder of my eclipse project. No difference. In addition, I know that this is really a demote deployment, but I only need a welcome program to work with AKKA. I spent so much time on this without getting a simple working application.

+7
source share
4 answers

As mentioned in corefn, the remote documentation explains the details in detail. It also refers to the application. . This example should give you everything you need to get started.


Edit

To start the sample application, follow these steps:

GitHub Clone

 eecolor@BLACK :~/GihHub$ git clone https://github.com/akka/akka.git 

Go to akka directory and run sbt

 eecolor@BLACK :~/GihHub/akka$ sbt 

Switch to akka-sample-project

 akka > project akka-sample-remote 

Call run in the project and select CalcApp

 Multiple main classes detected, select one to run: [1] sample.remote.calculator.java.JCreationApp [2] sample.remote.calculator.LookupApp [3] sample.remote.calculator.CalcApp [4] sample.remote.calculator.java.JLookupApp [5] sample.remote.calculator.CreationApp [6] sample.remote.calculator.java.JCalcApp Enter number: 3 [info] Running sample.remote.calculator.CalcApp [INFO] [02/19/2013 19:22:09.055] [run-main] [Remoting] Starting remoting [INFO] [02/19/2013 19:22:09.230] [run-main] [Remoting] Remoting started; listening on addresses :[akka.tcp:// CalculatorApplication@127.0.0.1 :2552] Started Calculator Application - waiting for messages 

Switch to another console and repeat the first few steps.

 eecolor@BLACK :~/GihHub/akka$ sbt akka > project akka-sample-remote 

Call run and select LookupApp

 akka-sample-remote > run Multiple main classes detected, select one to run: [1] sample.remote.calculator.java.JCreationApp [2] sample.remote.calculator.LookupApp [3] sample.remote.calculator.CalcApp [4] sample.remote.calculator.java.JLookupApp [5] sample.remote.calculator.CreationApp [6] sample.remote.calculator.java.JCalcApp Enter number: 2 [info] Running sample.remote.calculator.LookupApp [INFO] [02/19/2013 19:23:39.358] [run-main] [Remoting] Starting remoting [INFO] [02/19/2013 19:23:39.564] [run-main] [Remoting] Remoting started; listening on addresses :[akka.tcp:// LookupApplication@127.0.0.1 :2553] Started Lookup Application Sub result: 14 - 16 = -2 Sub result: 13 - 22 = -9 Add result: 56 + 93 = 149 Add result: 18 + 19 = 37 

Go back to another console and you will see something like this:

 Calculating 14 - 16 Calculating 13 - 22 Calculating 56 + 93 Calculating 18 + 19 
+6
source

Update for Akka 2.2.3

A minimal remote application can be created as follows:

Create 2 projects in Eclipse: client and server

Server:

Server Code

 package server import akka.actor.Actor import akka.actor.ActorLogging import akka.actor.ActorSystem import akka.actor.Props class Joe extends Actor { def receive = { case msg: String => println("joe received " + msg + " from " + sender) case _ => println("Received unknown msg ") } } object Server extends App { val system = ActorSystem("GreetingSystem") val joe = system.actorOf(Props[Joe], name = "joe") println(joe.path) joe ! "local msg!" println("Server ready") } 

Applinc.conf application for the server

 akka { loglevel = "DEBUG" actor { provider = "akka.remote.RemoteActorRefProvider" } remote { enabled-transports = ["akka.remote.netty.tcp"] netty.tcp { hostname = "127.0.0.1" port = 2552 } log-sent-messages = on log-received-messages = on } } 

Client:

Client code

 package client import akka.actor._ import akka.actor.ActorDSL._ object Greet_Sender extends App { println("STARTING") implicit val system = ActorSystem("GreetingSystem-1") val joe = system.actorSelection("akka.tcp:// GreetingSystem@127.0.0.1 :2552/user/joe") println("That Joe:" + joe) val a = actor(new Act { whenStarting { joe ! "Hello Joe from remote" } }) joe ! "Hello" println("Client has sent Hello to joe") } 

Application.conf client:

 akka { #log-config-on-start = on stdout-loglevel = "DEBUG" loglevel = "DEBUG" actor { provider = "akka.remote.RemoteActorRefProvider" } remote { enabled-transports = ["akka.remote.netty.tcp"] log-sent-messages = on log-received-messages = on netty.tcp { hostname = "127.0.0.1" port = 0 } } } 

The configurations should be placed in two files called application.conf, as in the bin directory of two projects.

+18
source

Well, in your example, the client code never refers to the configuration file, and it will not work.

0
source

akka will use the application.conf file by default - so it does not need to be explicitly selected.

if you want the code to be (taking the code above as exmaple):

 val configFile = getClass.getClassLoader.getResource("akka_local_application.conf").getFile val config = ConfigFactory.parseFile(new File(configFile)) val system = ActorSystem("GreetingSystem",config) val joe = system.actorOf(Props[Joe], name = "joe") 
0
source

All Articles