|
Akka/Scala example source code file (ClusterSingletonProxySpec.scala)
The ClusterSingletonProxySpec.scala Akka example source code
/**
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.contrib.pattern
import org.scalatest.{ BeforeAndAfterAll, Matchers, WordSpecLike }
import akka.testkit.{ TestProbe, TestKit }
import akka.actor._
import com.typesafe.config.ConfigFactory
import akka.cluster.Cluster
import scala.concurrent.duration._
class ClusterSingletonProxySpec extends WordSpecLike with Matchers with BeforeAndAfterAll {
import ClusterSingletonProxySpec._
val seed = new ActorSys()
seed.cluster.join(seed.cluster.selfAddress)
val testSystems = (0 until 4).map(_ ⇒ new ActorSys(joinTo = Some(seed.cluster.selfAddress))) :+ seed
"The cluster singleton proxy" must {
"correctly identify the singleton" in {
testSystems.foreach(_.testProxy("Hello"))
testSystems.foreach(_.testProxy("World"))
}
}
override def afterAll() = testSystems.foreach(_.system.shutdown())
}
object ClusterSingletonProxySpec {
class ActorSys(name: String = "ClusterSingletonProxySystem", joinTo: Option[Address] = None)
extends TestKit(ActorSystem(name, ConfigFactory.parseString(cfg))) {
val cluster = Cluster(system)
joinTo.foreach(address ⇒ cluster.join(address))
cluster.registerOnMemberUp {
system.actorOf(ClusterSingletonManager.props(
singletonProps = Props[Singleton],
singletonName = "singleton",
terminationMessage = PoisonPill,
role = None,
maxHandOverRetries = 5,
maxTakeOverRetries = 2), name = "singletonManager")
}
val proxy = system.actorOf(ClusterSingletonProxy.props("user/singletonManager/singleton", None), s"singletonProxy-${cluster.selfAddress.port.getOrElse(0)}")
def testProxy(msg: String) {
val probe = TestProbe()
probe.send(proxy, msg)
// 25 seconds to make sure the singleton was started up
probe.expectMsg(25.seconds, "Got " + msg)
}
}
val cfg = """akka {
loglevel = INFO
cluster {
auto-down-unreachable-after = 10s
min-nr-of-members = 2
}
actor.provider = "akka.cluster.ClusterActorRefProvider"
remote {
log-remote-lifecycle-events = off
netty.tcp {
hostname = "127.0.0.1"
port = 0
}
}
}
"""
class Singleton extends Actor with ActorLogging {
log.info("Singleton created on {}", Cluster(context.system).selfAddress)
def receive: Actor.Receive = {
case msg ⇒
sender() ! "Got " + msg
}
}
}
Other Akka source code examplesHere is a short list of links related to this Akka ClusterSingletonProxySpec.scala source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.