Skip to content

Instantly share code, notes, and snippets.

@henrikengstrom
Created December 22, 2011 20:29
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/1511728 to your computer and use it in GitHub Desktop.
Save henrikengstrom/1511728 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
val actorSystem2 = ActorSystem("actorSystem2", ConfigFactory.parseString("""
akka {
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
transport = "akka.remote.netty.NettyRemoteTransport"
netty {
hostname = "127.0.0.1"
port = 2553
}
}
}
"""))
// Add a really simplistic actor to actorSystem1 with the name "simplisticActor"
actorSystem1.actorOf(Props(new Actor {
def receive = {
case x: String =>
println("RECEIVED MESSAGE: " + x)
}
}), "simplisticActor")
// Now we want to send messages to the simplistic actor from another actor system
// As you can see we refer to "actorSystem1" with the correct host "127.0.0.1" and port "2552" as
// defined when we created the actor system above. Also it is very important that we refer to
// the actor we want to use with the correct name "simplisticActor".
val remoteActor = actorSystem2.actorFor("akka://actorSystem1@127.0.0.1:2552/user/simplisticActor")
// Now send some messages to it
remoteActor ! "TEST 1"
remoteActor ! "TEST 2"
// 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