Skip to content

Instantly share code, notes, and snippets.

@henrikengstrom
Created December 22, 2011 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henrikengstrom/1511732 to your computer and use it in GitHub Desktop.
Save henrikengstrom/1511732 to your computer and use it in GitHub Desktop.
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