Created
March 13, 2012 19:29
-
-
Save gsimard/2030951 to your computer and use it in GitHub Desktop.
Akka + remote Actor => println not working ?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// application.conf | |
server { | |
akka { | |
actor { | |
provider = "akka.remote.RemoteActorRefProvider" | |
} | |
remote { | |
transport = "akka.remote.netty.NettyRemoteTransport" | |
netty { | |
hostname = "127.0.0.1" | |
port = 2552 | |
} | |
} | |
} | |
} | |
client { | |
akka { | |
actor { | |
provider = "akka.remote.RemoteActorRefProvider" | |
} | |
remote { | |
transport = "akka.remote.netty.NettyRemoteTransport" | |
netty { | |
hostname = "127.0.0.1" | |
port = 2553 | |
} | |
} | |
} | |
} | |
// PongAkka.scala | |
package ca.gsimard.client.akka | |
import com.typesafe.config.ConfigFactory | |
import akka.actor.Actor | |
import akka.actor.ActorRef | |
import akka.actor.ActorSystem | |
import akka.actor.Props | |
case class Greeting(g: String) | |
class PongAkka extends Actor { | |
def receive = { | |
case Greeting(g) ⇒ | |
println("Server: " + g) | |
sender ! Greeting("greetings from server.") | |
case _ ⇒ println("Server: received unknown message") | |
} | |
} | |
class PingAkka extends Actor { | |
var greeter: ActorRef = null | |
def receive = { | |
case "Connect" => | |
greeter = context.actorFor("akka://Server@127.0.0.1:2552/soundServer"); | |
println("Client: Connected") | |
case "Send" => greeter ! Greeting("hello") | |
case Greeting(g) ⇒ println("Client: " + g) | |
case _ ⇒ println("Client: received unknown message") | |
} | |
} | |
// main.scala | |
package ca.gsimard.client | |
import _root_.akka.actor.ActorSystem | |
import _root_.akka.actor.Props | |
import com.typesafe.config.ConfigFactory | |
object Main { | |
def main(args: Array[String]): Unit = { | |
val config1 = ConfigFactory.load("resources/application.conf") | |
val app1 = ActorSystem("Server", config1.getConfig("server").withFallback(config1)) | |
val server = app1.actorOf(Props[akka.PongAkka], name = "soundServer") | |
val config2 = ConfigFactory.load("resources/application.conf") | |
val app2 = ActorSystem("Client", config2.getConfig("client").withFallback(config2)) | |
val client = app2.actorOf(Props[akka.PingAkka], name = "soundClient") | |
client ! "Connect" | |
Thread.sleep(2000) | |
client ! "Send" | |
Thread.sleep(5000) | |
app1.shutdown() | |
app2.shutdown() | |
return | |
} | |
} | |
// Result: | |
[INFO] [03/13/2012 15:24:22.550] [main] [ActorSystem(SpacecraftServer)] REMOTE: RemoteServerStarted@akka://SpacecraftServer@127.0.0.1:2552 | |
[INFO] [03/13/2012 15:24:22.680] [main] [ActorSystem(SpacecraftClient)] REMOTE: RemoteServerStarted@akka://SpacecraftClient@127.0.0.1:2553 | |
Client: Connected | |
[INFO] [03/13/2012 15:24:24.826] [SpacecraftClient-akka.actor.default-dispatcher-5] [ActorSystem(SpacecraftClient)] REMOTE: RemoteClientStarted@akka://SpacecraftServer@127.0.0.1:2552 | |
[INFO] [03/13/2012 15:24:24.852] [SpacecraftServer-6] [ActorSystem(SpacecraftServer)] REMOTE: RemoteClientStarted@akka://SpacecraftClient@127.0.0.1:2553 | |
[INFO] [03/13/2012 15:24:29.756] [SpacecraftClient-akka.actor.default-dispatcher-3] [ActorSystem(SpacecraftClient)] REMOTE: RemoteClientShutdown@akka://SpacecraftServer@127.0.0.1:2552 | |
[INFO] [03/13/2012 15:24:29.756] [SpacecraftServer-akka.actor.default-dispatcher-1] [ActorSystem(SpacecraftServer)] REMOTE: RemoteClientShutdown@akka://SpacecraftClient@127.0.0.1:2553 | |
[INFO] [03/13/2012 15:24:29.769] [SpacecraftClient-akka.actor.default-dispatcher-3] [ActorSystem(SpacecraftClient)] REMOTE: RemoteServerShutdown@akka://SpacecraftClient@127.0.0.1:2553 | |
[INFO] [03/13/2012 15:24:29.769] [SpacecraftServer-akka.actor.default-dispatcher-1] [ActorSystem(SpacecraftServer)] REMOTE: RemoteServerShutdown@akka://SpacecraftServer@127.0.0.1:2552 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment