Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

scala - Akka 2.1 minimal remote actor example

EDIT Notice, I needed to make the reverse changes of this https://github.com/akka/akka/commit/ce014ece3568938b2036c4ccfd21b92faba69607#L13L6 to make the accepted answer work with AKKA 2.1 which is the stable distribution found on akkas homepage!


I have read all the tutorials I could find on AKKA, but nothing I found works "out of box".

Using eclipse, I want to create 2 programs.

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

Program2: gets a reference to actor "joe" at 127.0.0.1:some_port. Sends a hello message to "joe".

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


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 start only Program2 and it outputs:

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

It seems that it is not picking up my application.conf. I tried placing it both in the ./src/ and ./ folder of my eclipse project. No difference. Also, I know this is really demote deployment, but I need just a hello world program to work using AKKA. I spent so much time on this without getting a simple working application.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Update for Akka 2.2.3

A minimal remote application can be created as follows:

Create 2 Projects in Eclipse: Client and Server

Server:

The code for the server is

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")
}

The applincation.conf for the server is

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:

The Client-code is

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 's Joe:" + joe)

   val a = actor(new Act {
      whenStarting { joe ! "Hello Joe from remote" }
   })

   joe ! "Hello"

   println("Client has sent Hello to joe")
}

The client application.conf is:

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
    }
  }  
}

Configurations have to be placed in two files called application.conf, both within the bin directory of the two projects.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...