* Separate routing logic, to be usable stand alone, e.g. in actors * Simplify RouterConfig, only a factory * Move reading of config from Deployer to the RouterConfig * Distiction between Pool and Group router types * Remove usage of actorFor, use ActorSelection * Management messages to add and remove routees * Simplify the internals of RoutedActorCell & co * Move resize specific code to separate RoutedActorCell subclass * Change resizer api to only return capacity change * Resizer only allowed together with Pool * Re-implement all routers, and keep old api during deprecation phase * Replace ClusterRouterConfig, deprecation * Rewrite documentation * Migration guide * Also includes related ticket: +act #3087 Create nicer Props factories for RouterConfig
79 lines
1.9 KiB
Scala
79 lines
1.9 KiB
Scala
/**
|
|
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
|
|
*/
|
|
package docs.routing
|
|
|
|
import akka.testkit.AkkaSpec
|
|
import akka.testkit.ImplicitSender
|
|
import akka.routing.FromConfig
|
|
import akka.actor.ActorRef
|
|
|
|
object ConsistentHashingRouterDocSpec {
|
|
|
|
//#cache-actor
|
|
import akka.actor.Actor
|
|
import akka.routing.ConsistentHashingRouter.ConsistentHashable
|
|
|
|
class Cache extends Actor {
|
|
var cache = Map.empty[String, String]
|
|
|
|
def receive = {
|
|
case Entry(key, value) ⇒ cache += (key -> value)
|
|
case Get(key) ⇒ sender ! cache.get(key)
|
|
case Evict(key) ⇒ cache -= key
|
|
}
|
|
}
|
|
|
|
case class Evict(key: String)
|
|
|
|
case class Get(key: String) extends ConsistentHashable {
|
|
override def consistentHashKey: Any = key
|
|
}
|
|
|
|
case class Entry(key: String, value: String)
|
|
//#cache-actor
|
|
|
|
}
|
|
|
|
class ConsistentHashingRouterDocSpec extends AkkaSpec with ImplicitSender {
|
|
|
|
import ConsistentHashingRouterDocSpec._
|
|
|
|
"demonstrate usage of ConsistentHashableRouter" in {
|
|
|
|
def context = system
|
|
|
|
//#consistent-hashing-router
|
|
import akka.actor.Props
|
|
import akka.routing.ConsistentHashingPool
|
|
import akka.routing.ConsistentHashingRouter.ConsistentHashMapping
|
|
import akka.routing.ConsistentHashingRouter.ConsistentHashableEnvelope
|
|
|
|
def hashMapping: ConsistentHashMapping = {
|
|
case Evict(key) ⇒ key
|
|
}
|
|
|
|
val cache: ActorRef =
|
|
context.actorOf(ConsistentHashingPool(10, hashMapping = hashMapping).
|
|
props(Props[Cache]), name = "cache")
|
|
|
|
cache ! ConsistentHashableEnvelope(
|
|
message = Entry("hello", "HELLO"), hashKey = "hello")
|
|
cache ! ConsistentHashableEnvelope(
|
|
message = Entry("hi", "HI"), hashKey = "hi")
|
|
|
|
cache ! Get("hello")
|
|
expectMsg(Some("HELLO"))
|
|
|
|
cache ! Get("hi")
|
|
expectMsg(Some("HI"))
|
|
|
|
cache ! Evict("hi")
|
|
cache ! Get("hi")
|
|
expectMsg(None)
|
|
|
|
//#consistent-hashing-router
|
|
|
|
}
|
|
|
|
}
|