Skip to content

Instantly share code, notes, and snippets.

@bhudgeons
Created May 8, 2014 22:09
Show Gist options
  • Save bhudgeons/78cc89fb6be47ff1c975 to your computer and use it in GitHub Desktop.
Save bhudgeons/78cc89fb6be47ff1c975 to your computer and use it in GitHub Desktop.
Demonstration of Akka Cluster Singleton Communication Problem
package sample.cluster.simple
import com.typesafe.config.ConfigFactory
import akka.actor.ActorSystem
import akka.actor.Props
import akka.actor._
import akka.contrib.pattern.ClusterSingletonManager
import akka.contrib.pattern.ClusterSingletonProxy
class MyClusterActor extends Actor {
def receive = {
case msg => {
println("MyClusterActor got *" + msg + "* from " + sender().path)
sender() ! "Here's a response from MyClusterActor"
}
}
}
case class Start(actor: ActorRef)
class MyRemoteActor extends Actor {
def receive = {
case Start(actor) => {
println("MyRemoteActor got Start with " + actor.path)
actor ! "Hello from RemoteActor"
}
case msg => {
println("MyRemoteActor got *" + msg + "* from " + sender().path)
}
}
}
object RemoteSystem {
val remoteConfig = ConfigFactory.parseString("akka.actor.provider=\"akka.remote.RemoteActorRefProvider\"").
withFallback(ConfigFactory.parseString("akka.remote.enabled-transports=[\"akka.remote.netty.tcp\"]")).
withFallback(ConfigFactory.parseString("akka.remote.netty.tcp.hostname=\"127.0.0.1\"")).
withFallback(ConfigFactory.parseString("akka.remote.netty.tcp.port=2551"))
val remoteSystem = ActorSystem("RemoteSystem", remoteConfig)
val remoteActor = remoteSystem.actorOf(Props[MyRemoteActor], "myremoteactor")
val clusterConfig = ConfigFactory.parseString("akka.actor.provider=\"akka.cluster.ClusterActorRefProvider\"").
withFallback(ConfigFactory.parseString("akka.remote.enabled-transports=[\"akka.remote.netty.tcp\"]")).
withFallback(ConfigFactory.parseString("akka.remote.netty.tcp.hostname=\"127.0.0.1\"")).
withFallback(ConfigFactory.parseString("akka.cluster.seed-nodes=[\"akka.tcp://[email protected]:2553\"]")).
withFallback(ConfigFactory.parseString("akka.remote.netty.tcp.port=2552"))
val clusterSystem = ActorSystem("ClusterSystem", clusterConfig)
val clusterActor = clusterSystem.actorOf(ClusterSingletonProxy.props(
singletonPath = "/user/myclusteractor/active",
role = None),
name = "MyClusterActor")
}
object ClusterSystem {
val clusterConfig = ConfigFactory.parseString("akka.actor.provider=\"akka.cluster.ClusterActorRefProvider\"").
withFallback(ConfigFactory.parseString("akka.remote.enabled-transports=[\"akka.remote.netty.tcp\"]")).
withFallback(ConfigFactory.parseString("akka.remote.netty.tcp.hostname=\"127.0.0.1\"")).
withFallback(ConfigFactory.parseString("akka.cluster.seed-nodes=[\"akka.tcp://[email protected]:2553\"]")).
withFallback(ConfigFactory.parseString("akka.remote.netty.tcp.port=2553"))
val clusterSystem = ActorSystem("ClusterSystem", clusterConfig)
clusterSystem.actorOf(ClusterSingletonManager.props(Props(classOf[MyClusterActor]), "active",
PoisonPill, None), "myclusteractor")
def setup() = println("Setting up ClusterSystem")
}
@patriknw
Copy link

patriknw commented May 9, 2014

tip: scala supports:

parseString("""akka.remote.enabled-transports="akka.remote.netty.tcp"
                     akka.remote.netty.tcp.hostname="127.0.0.1"
                 """)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment