Complete unfinished ConsistentHashingRouterDocSpec, see #944

This commit is contained in:
Patrik Nordwall 2012-09-14 14:28:47 +02:00
parent 48d8a09075
commit 50fc5a03a2
3 changed files with 24 additions and 11 deletions

View file

@ -306,7 +306,8 @@ Code example:
FIXME Java example of consistent routing
In the above example you see that the ``Get`` message implements ``ConsistentHashable`` itself,
while the ``Entry`` message is wrapped in a ``ConsistentHashableEnvelope``.
while the ``Entry`` message is wrapped in a ``ConsistentHashableEnvelope``. The ``Evict``
message is handled by the ``withConsistentHashMapping``.
This is an example of how to define a consistent-hashing router in configuration:

View file

@ -19,7 +19,7 @@ object ConsistentHashingRouterDocSpec {
def receive = {
case Entry(key, value) cache += (key -> value)
case Get(key) sender ! cache.get(key)
case Evict(key) => cache -= key
case Evict(key) cache -= key
}
}
@ -30,10 +30,6 @@ object ConsistentHashingRouterDocSpec {
}
case class Entry(key: String, value: String)
case class EntryEnvelope(entry: Entry) extends ConsistentHashableEnvelope {
override def consistentHashKey: Any = entry.key
override def message: Any = entry
}
//#cache-actor
}
@ -47,16 +43,31 @@ class ConsistentHashingRouterDocSpec extends AkkaSpec with ImplicitSender {
//#consistent-hashing-router
import akka.actor.Props
import akka.routing.ConsistentHashingRouter
import akka.routing.ConsistentHashingRouter.ConsistentHashRoute
import akka.routing.ConsistentHashingRouter.ConsistentHashableEnvelope
val cache = system.actorOf(Props[Cache].withRouter(ConsistentHashingRouter(10)), "cache")
def consistentHashRoute: ConsistentHashingRouter.ConsistentHashRoute = {
case Evict(key) key
}
cache ! EntryEnvelope(Entry("hello", "HELLO"))
cache ! EntryEnvelope(Entry("hi", "HI"))
val cache = system.actorOf(Props[Cache].withRouter(ConsistentHashingRouter(10,
consistentHashRoute = consistentHashRoute)), name = "cache")
cache ! ConsistentHashableEnvelope(
message = Entry("hello", "HELLO"), consistentHashKey = "hello")
cache ! ConsistentHashableEnvelope(
message = Entry("hi", "HI"), consistentHashKey = "hi")
cache ! Get("hello")
cache ! Get("hi")
expectMsg(Some("HELLO"))
cache ! Get("hi")
expectMsg(Some("HI"))
cache ! Evict("hi")
cache ! Get("hi")
expectMsg(None)
//#consistent-hashing-router
}

View file

@ -313,7 +313,8 @@ Code example:
.. includecode:: code/docs/routing/ConsistentHashingRouterDocSpec.scala#consistent-hashing-router
In the above example you see that the ``Get`` message implements ``ConsistentHashable`` itself,
while the ``Entry`` message is wrapped in a ``ConsistentHashableEnvelope``.
while the ``Entry`` message is wrapped in a ``ConsistentHashableEnvelope``. The ``Evict``
message is handled by the ``consistentHashRoute`` partial function.
This is an example of how to define a consistent-hashing router in configuration: