2012-09-17 11:40:06 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
package docs.jrouting;
|
|
|
|
|
|
|
|
|
|
import org.junit.AfterClass;
|
|
|
|
|
import org.junit.BeforeClass;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import akka.testkit.JavaTestKit;
|
|
|
|
|
import akka.actor.ActorSystem;
|
|
|
|
|
|
|
|
|
|
//#imports1
|
|
|
|
|
import akka.actor.UntypedActor;
|
|
|
|
|
import akka.routing.ConsistentHashingRouter.ConsistentHashable;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.io.Serializable;
|
|
|
|
|
//#imports1
|
|
|
|
|
|
|
|
|
|
//#imports2
|
|
|
|
|
import akka.actor.Props;
|
|
|
|
|
import akka.actor.ActorRef;
|
|
|
|
|
import akka.routing.ConsistentHashingRouter;
|
2012-09-17 13:24:13 +02:00
|
|
|
import akka.routing.ConsistentHashingRouter.ConsistentHashMapper;
|
2012-09-17 11:40:06 +02:00
|
|
|
import akka.routing.ConsistentHashingRouter.ConsistentHashableEnvelope;
|
|
|
|
|
//#imports2
|
|
|
|
|
|
|
|
|
|
public class ConsistentHashingRouterDocTestBase {
|
|
|
|
|
|
|
|
|
|
static ActorSystem system;
|
|
|
|
|
|
|
|
|
|
@BeforeClass
|
|
|
|
|
public static void setup() {
|
|
|
|
|
system = ActorSystem.create();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@AfterClass
|
|
|
|
|
public static void teardown() {
|
|
|
|
|
system.shutdown();
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-26 10:56:25 +02:00
|
|
|
static
|
2012-09-17 11:40:06 +02:00
|
|
|
//#cache-actor
|
2012-09-26 10:56:25 +02:00
|
|
|
public class Cache extends UntypedActor {
|
2012-09-17 11:40:06 +02:00
|
|
|
Map<String, String> cache = new HashMap<String, String>();
|
|
|
|
|
|
|
|
|
|
public void onReceive(Object msg) {
|
|
|
|
|
if (msg instanceof Entry) {
|
|
|
|
|
Entry entry = (Entry) msg;
|
|
|
|
|
cache.put(entry.key, entry.value);
|
|
|
|
|
} else if (msg instanceof Get) {
|
|
|
|
|
Get get = (Get) msg;
|
|
|
|
|
Object value = cache.get(get.key);
|
|
|
|
|
getSender().tell(value == null ? NOT_FOUND : value,
|
|
|
|
|
getContext().self());
|
|
|
|
|
} else if (msg instanceof Evict) {
|
|
|
|
|
Evict evict = (Evict) msg;
|
|
|
|
|
cache.remove(evict.key);
|
|
|
|
|
} else {
|
|
|
|
|
unhandled(msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-26 10:56:25 +02:00
|
|
|
//#cache-actor
|
|
|
|
|
static
|
|
|
|
|
//#cache-actor
|
|
|
|
|
public final class Evict implements Serializable {
|
2012-09-17 11:40:06 +02:00
|
|
|
public final String key;
|
|
|
|
|
public Evict(String key) {
|
|
|
|
|
this.key = key;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-26 10:56:25 +02:00
|
|
|
//#cache-actor
|
|
|
|
|
static
|
|
|
|
|
//#cache-actor
|
|
|
|
|
public final class Get implements Serializable, ConsistentHashable {
|
2012-09-17 11:40:06 +02:00
|
|
|
public final String key;
|
|
|
|
|
public Get(String key) {
|
|
|
|
|
this.key = key;
|
|
|
|
|
}
|
|
|
|
|
public Object consistentHashKey() {
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-26 10:56:25 +02:00
|
|
|
//#cache-actor
|
|
|
|
|
static
|
|
|
|
|
//#cache-actor
|
|
|
|
|
public final class Entry implements Serializable {
|
2012-09-17 11:40:06 +02:00
|
|
|
public final String key;
|
|
|
|
|
public final String value;
|
|
|
|
|
public Entry(String key, String value) {
|
|
|
|
|
this.key = key;
|
|
|
|
|
this.value = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-26 10:56:25 +02:00
|
|
|
//#cache-actor
|
|
|
|
|
static
|
|
|
|
|
//#cache-actor
|
|
|
|
|
public final String NOT_FOUND = "NOT_FOUND";
|
2012-09-17 11:40:06 +02:00
|
|
|
//#cache-actor
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void demonstrateUsageOfConsistentHashableRouter() {
|
|
|
|
|
|
|
|
|
|
new JavaTestKit(system) {{
|
|
|
|
|
|
|
|
|
|
//#consistent-hashing-router
|
|
|
|
|
|
2012-09-17 13:24:13 +02:00
|
|
|
final ConsistentHashMapper hashMapper = new ConsistentHashMapper() {
|
2012-09-17 11:40:06 +02:00
|
|
|
@Override
|
2012-09-17 13:24:13 +02:00
|
|
|
public Object hashKey(Object message) {
|
2012-09-17 11:40:06 +02:00
|
|
|
if (message instanceof Evict) {
|
|
|
|
|
return ((Evict) message).key;
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ActorRef cache = system.actorOf(new Props(Cache.class).withRouter(
|
2012-09-17 13:24:13 +02:00
|
|
|
new ConsistentHashingRouter(10).withHashMapper(hashMapper)),
|
2012-09-17 11:40:06 +02:00
|
|
|
"cache");
|
|
|
|
|
|
|
|
|
|
cache.tell(new ConsistentHashableEnvelope(
|
|
|
|
|
new Entry("hello", "HELLO"), "hello"), getRef());
|
|
|
|
|
cache.tell(new ConsistentHashableEnvelope(
|
|
|
|
|
new Entry("hi", "HI"), "hi"), getRef());
|
|
|
|
|
|
|
|
|
|
cache.tell(new Get("hello"), getRef());
|
|
|
|
|
expectMsgEquals("HELLO");
|
|
|
|
|
|
|
|
|
|
cache.tell(new Get("hi"), getRef());
|
|
|
|
|
expectMsgEquals("HI");
|
|
|
|
|
|
|
|
|
|
cache.tell(new Evict("hi"), getRef());
|
|
|
|
|
cache.tell(new Get("hi"), getRef());
|
|
|
|
|
expectMsgEquals(NOT_FOUND);
|
|
|
|
|
|
|
|
|
|
//#consistent-hashing-router
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|