2012-09-11 15:10:33 +02:00
|
|
|
/**
|
2013-01-09 01:47:48 +01:00
|
|
|
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
|
2012-09-11 15:10:33 +02:00
|
|
|
*/
|
|
|
|
|
package docs.routing
|
|
|
|
|
|
|
|
|
|
import akka.testkit.AkkaSpec
|
|
|
|
|
import akka.testkit.ImplicitSender
|
2013-09-19 08:00:05 +02:00
|
|
|
import akka.routing.FromConfig
|
|
|
|
|
import akka.actor.ActorRef
|
2012-09-11 15:10:33 +02:00
|
|
|
|
|
|
|
|
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 = {
|
2013-12-03 16:34:26 +01:00
|
|
|
case Entry(key, value) => cache += (key -> value)
|
2014-01-16 15:16:35 +01:00
|
|
|
case Get(key) => sender() ! cache.get(key)
|
2013-12-03 16:34:26 +01:00
|
|
|
case Evict(key) => cache -= key
|
2012-09-11 15:10:33 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-14 13:47:58 +02:00
|
|
|
case class Evict(key: String)
|
|
|
|
|
|
2012-09-11 15:10:33 +02:00
|
|
|
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 {
|
|
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
def context = system
|
|
|
|
|
|
2012-09-11 15:10:33 +02:00
|
|
|
//#consistent-hashing-router
|
|
|
|
|
import akka.actor.Props
|
2013-09-19 08:00:05 +02:00
|
|
|
import akka.routing.ConsistentHashingPool
|
2012-09-17 13:24:13 +02:00
|
|
|
import akka.routing.ConsistentHashingRouter.ConsistentHashMapping
|
2012-09-14 14:28:47 +02:00
|
|
|
import akka.routing.ConsistentHashingRouter.ConsistentHashableEnvelope
|
|
|
|
|
|
2012-09-17 13:24:13 +02:00
|
|
|
def hashMapping: ConsistentHashMapping = {
|
2013-12-03 16:34:26 +01:00
|
|
|
case Evict(key) => key
|
2012-09-14 14:28:47 +02:00
|
|
|
}
|
2012-09-11 15:10:33 +02:00
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
val cache: ActorRef =
|
|
|
|
|
context.actorOf(ConsistentHashingPool(10, hashMapping = hashMapping).
|
|
|
|
|
props(Props[Cache]), name = "cache")
|
2012-09-11 15:10:33 +02:00
|
|
|
|
2012-09-14 14:28:47 +02:00
|
|
|
cache ! ConsistentHashableEnvelope(
|
2012-09-17 13:24:13 +02:00
|
|
|
message = Entry("hello", "HELLO"), hashKey = "hello")
|
2012-09-14 14:28:47 +02:00
|
|
|
cache ! ConsistentHashableEnvelope(
|
2012-09-17 13:24:13 +02:00
|
|
|
message = Entry("hi", "HI"), hashKey = "hi")
|
2012-09-11 15:10:33 +02:00
|
|
|
|
|
|
|
|
cache ! Get("hello")
|
|
|
|
|
expectMsg(Some("HELLO"))
|
2012-09-14 14:28:47 +02:00
|
|
|
|
|
|
|
|
cache ! Get("hi")
|
2012-09-11 15:10:33 +02:00
|
|
|
expectMsg(Some("HI"))
|
2012-09-14 14:28:47 +02:00
|
|
|
|
|
|
|
|
cache ! Evict("hi")
|
|
|
|
|
cache ! Get("hi")
|
|
|
|
|
expectMsg(None)
|
|
|
|
|
|
2012-09-11 15:10:33 +02:00
|
|
|
//#consistent-hashing-router
|
2013-09-19 08:00:05 +02:00
|
|
|
|
2012-09-11 15:10:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|