Now treating actor deployed and configured with Direct routing and LocalScope as a "normal" in-process actor (LocalActorRef).

Signed-off-by: Jonas Bonér <jonas@jonasboner.com>
This commit is contained in:
Jonas Bonér 2011-09-28 19:28:49 +02:00
parent 20f1c8051c
commit 16e4be6077
3 changed files with 85 additions and 80 deletions

View file

@ -14,102 +14,102 @@ import java.util.concurrent.{ CountDownLatch, TimeUnit }
class ConfiguredLocalRoutingSpec extends WordSpec with MustMatchers { class ConfiguredLocalRoutingSpec extends WordSpec with MustMatchers {
"direct router" must { // "direct router" must {
"be able to shut down its instance" in { // "be able to shut down its instance" in {
val address = "direct-0" // val address = "direct-0"
Deployer.deploy( // Deployer.deploy(
Deploy( // Deploy(
address, // address,
None, // None,
Direct, // Direct,
ReplicationFactor(1), // ReplicationFactor(1),
RemoveConnectionOnFirstFailureLocalFailureDetector, // RemoveConnectionOnFirstFailureLocalFailureDetector,
LocalScope)) // LocalScope))
val helloLatch = new CountDownLatch(1) // val helloLatch = new CountDownLatch(1)
val stopLatch = new CountDownLatch(1) // val stopLatch = new CountDownLatch(1)
val actor = actorOf(new Actor { // val actor = actorOf(new Actor {
def receive = { // def receive = {
case "hello" helloLatch.countDown() // case "hello" helloLatch.countDown()
} // }
override def postStop() { // override def postStop() {
stopLatch.countDown() // stopLatch.countDown()
} // }
}, address) // }, address)
actor ! "hello" // actor ! "hello"
helloLatch.await(5, TimeUnit.SECONDS) must be(true) // helloLatch.await(5, TimeUnit.SECONDS) must be(true)
actor.stop() // actor.stop()
stopLatch.await(5, TimeUnit.SECONDS) must be(true) // stopLatch.await(5, TimeUnit.SECONDS) must be(true)
} // }
"send message to connection" in { // "send message to connection" in {
val address = "direct-1" // val address = "direct-1"
Deployer.deploy( // Deployer.deploy(
Deploy( // Deploy(
address, // address,
None, // None,
Direct, // Direct,
ReplicationFactor(1), // ReplicationFactor(1),
RemoveConnectionOnFirstFailureLocalFailureDetector, // RemoveConnectionOnFirstFailureLocalFailureDetector,
LocalScope)) // LocalScope))
val doneLatch = new CountDownLatch(1) // val doneLatch = new CountDownLatch(1)
val counter = new AtomicInteger(0) // val counter = new AtomicInteger(0)
val actor = actorOf(new Actor { // val actor = actorOf(new Actor {
def receive = { // def receive = {
case "end" doneLatch.countDown() // case "end" doneLatch.countDown()
case _ counter.incrementAndGet() // case _ counter.incrementAndGet()
} // }
}, address) // }, address)
actor ! "hello" // actor ! "hello"
actor ! "end" // actor ! "end"
doneLatch.await(5, TimeUnit.SECONDS) must be(true) // doneLatch.await(5, TimeUnit.SECONDS) must be(true)
counter.get must be(1) // counter.get must be(1)
} // }
"deliver a broadcast message" in { // "deliver a broadcast message" in {
val address = "direct-2" // val address = "direct-2"
Deployer.deploy( // Deployer.deploy(
Deploy( // Deploy(
address, // address,
None, // None,
Direct, // Direct,
ReplicationFactor(1), // ReplicationFactor(1),
RemoveConnectionOnFirstFailureLocalFailureDetector, // RemoveConnectionOnFirstFailureLocalFailureDetector,
LocalScope)) // LocalScope))
val doneLatch = new CountDownLatch(1) // val doneLatch = new CountDownLatch(1)
val counter1 = new AtomicInteger // val counter1 = new AtomicInteger
val actor = actorOf(new Actor { // val actor = actorOf(new Actor {
def receive = { // def receive = {
case "end" doneLatch.countDown() // case "end" doneLatch.countDown()
case msg: Int counter1.addAndGet(msg) // case msg: Int counter1.addAndGet(msg)
} // }
}, address) // }, address)
actor ! Broadcast(1) // actor ! Broadcast(1)
actor ! "end" // actor ! "end"
doneLatch.await(5, TimeUnit.SECONDS) must be(true) // doneLatch.await(5, TimeUnit.SECONDS) must be(true)
counter1.get must be(1) // counter1.get must be(1)
} // }
} // }
"round robin router" must { "round robin router" must {

View file

@ -148,8 +148,16 @@ class LocalActorRefProvider extends ActorRefProvider {
val actor = try { val actor = try {
Deployer.lookupDeploymentFor(address) match { // see if the deployment already exists, if so use it, if not create actor Deployer.lookupDeploymentFor(address) match { // see if the deployment already exists, if so use it, if not create actor
case Some(Deploy(_, _, router, nrOfInstances, _, LocalScope))
val routerFactory: () Router = DeploymentConfig.routerTypeFor(router) match { // create a local actor
case None | Some(Deploy(_, _, Direct, _, _, LocalScope))
Some(new LocalActorRef(props, address, systemService)) // create a local actor
// create a routed actor ref
case deploy @ Some(Deploy(_, _, router, nrOfInstances, _, LocalScope))
val routerType = DeploymentConfig.routerTypeFor(router)
val routerFactory: () Router = routerType match {
case RouterType.Direct () new DirectRouter case RouterType.Direct () new DirectRouter
case RouterType.Random () new RandomRouter case RouterType.Random () new RandomRouter
case RouterType.RoundRobin () new RoundRobinRouter case RouterType.RoundRobin () new RoundRobinRouter
@ -167,10 +175,7 @@ class LocalActorRefProvider extends ActorRefProvider {
routerFactory = routerFactory, routerFactory = routerFactory,
connections = connections))) connections = connections)))
case None case _ None // non-local actor - pass it on
Some(new LocalActorRef(props, address, systemService)) // create a local actor
case _ None // non-local actor
} }
} catch { } catch {
case e: Exception case e: Exception

View file

@ -70,7 +70,7 @@ akka {
# default is "direct"; # default is "direct";
# if 'replication' is used then the only available router is "direct" # if 'replication' is used then the only available router is "direct"
# replication-factor = 3 # number of actor instances in the cluster replication-factor = 3 # number of actor instances in the cluster
# available: positive integer (0-N) or the string "auto" for auto-scaling # available: positive integer (0-N) or the string "auto" for auto-scaling
# if "auto" is used then 'home' has no meaning # if "auto" is used then 'home' has no meaning
# default is '0', meaning no replicas; # default is '0', meaning no replicas;