Created
December 22, 2011 20:30
-
-
Save henrikengstrom/1511732 to your computer and use it in GitHub Desktop.
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
package com.typesafe.akka.demo.remote | |
import com.typesafe.config.ConfigFactory | |
import akka.actor.{Actor, Props, ActorSystem} | |
object MyApp extends App { | |
// Create an actor system that listens to port 2552 | |
val actorSystem1 = ActorSystem("actorSystem1", ConfigFactory.parseString(""" | |
akka { | |
actor { | |
provider = "akka.remote.RemoteActorRefProvider" | |
} | |
remote { | |
transport = "akka.remote.netty.NettyRemoteTransport" | |
netty { | |
hostname = "127.0.0.1" | |
port = 2552 | |
} | |
} | |
} | |
""")) | |
// Create an actor system that listens to port 2553 | |
// Also add deployment instructions for actor "simplisticActor" | |
val actorSystem2 = ActorSystem("actorSystem2", ConfigFactory.parseString(""" | |
akka { | |
actor { | |
provider = "akka.remote.RemoteActorRefProvider" | |
deployment { | |
/simplisticActor { | |
remote = "akka://actorSystem1@127.0.0.1:2552" | |
} | |
} | |
} | |
remote { | |
transport = "akka.remote.netty.NettyRemoteTransport" | |
netty { | |
hostname = "127.0.0.1" | |
port = 2553 | |
} | |
} | |
} | |
""")) | |
// Add the simplistic actor to actorSystem1 with the name "simplisticActor" from actorSystem2. | |
// The remote deployment happens because we give the actor the name "simplisticActor" which | |
// maps to the name in the configuration above. | |
val remoteActor = actorSystem2.actorOf(Props(new Actor { | |
def receive = { | |
case x: String => | |
println("RECEIVED ANOTHER MESSAGE: " + x) | |
} | |
}), "simplisticActor") | |
// Now send some messages to it | |
remoteActor ! "TEST 3" | |
remoteActor ! "TEST 4" | |
// A small wait for the messages to be received before shutting down the example | |
Thread.sleep(100) | |
actorSystem1.shutdown() | |
actorSystem2.shutdown() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment