akka.routing
Class ConsistentHashingRouter

java.lang.Object
  extended by akka.routing.ConsistentHashingRouter
All Implemented Interfaces:
DeprecatedRouterConfig, Group, Pool, RouterConfig, java.io.Serializable, scala.Equals, scala.Product

public class ConsistentHashingRouter
extends java.lang.Object
implements DeprecatedRouterConfig, scala.Product, scala.Serializable

A Router that uses consistent hashing to select a connection based on the sent message.

There is 3 ways to define what data to use for the consistent hash key.

1. You can define hashMapping / withHashMapper of the router to map incoming messages to their consistent hash key. This makes the decision transparent for the sender.

2. The messages may implement ConsistentHashingRouter.ConsistentHashable. The key is part of the message and it's convenient to define it together with the message definition.

3. The messages can be be wrapped in a ConsistentHashingRouter.ConsistentHashableEnvelope to define what data to use for the consistent hash key. The sender knows the key to use.

These ways to define the consistent hash key can be use together and at the same time for one router. The hashMapping is tried first.

Please note that providing both 'nrOfInstances' and 'routees' does not make logical sense as this means that the router should both create new actors and use the 'routees' actor(s). In this case the 'nrOfInstances' will be ignored and the 'routees' will be used.
The configuration parameter trumps the constructor arguments. This means that if you provide either 'nrOfInstances' or 'routees' during instantiation they will be ignored if the router is defined in the configuration file for the actor being used.

Supervision Setup

Any routees that are created by a router will be created as the router's children. The router is therefore also the children's supervisor.

The supervision strategy of the router actor can be configured with withSupervisorStrategy(akka.actor.SupervisorStrategy). If no strategy is provided, routers default to a strategy of “always escalate”. This means that errors are passed up to the router's supervisor for handling.

The router's supervisor will treat the error as an error with the router itself. Therefore a directive to stop or restart will cause the router itself to stop or restart. The router, in turn, will cause its children to stop and restart.

See Also:
Serialized Form

Nested Class Summary
static interface ConsistentHashingRouter.ConsistentHashable
          If you don't define the hashMapping when constructing the ConsistentHashingRouter the messages need to implement this interface to define what data to use for the consistent hash key.
static class ConsistentHashingRouter.ConsistentHashableEnvelope
          If you don't define the hashMapping when constructing the ConsistentHashingRouter and messages can't implement ConsistentHashingRouter.ConsistentHashable themselves they can we wrapped by this envelope instead.
static class ConsistentHashingRouter.ConsistentHashableEnvelope$
           
static interface ConsistentHashingRouter.ConsistentHashMapper
          JAVA API Mapping from message to the data to use for the consistent hash key.
static class ConsistentHashingRouter.emptyConsistentHashMapping$
           
 
Constructor Summary
ConsistentHashingRouter(int nr)
          Java API: Constructor that sets nrOfInstances to be created.
ConsistentHashingRouter(int nrOfInstances, scala.collection.immutable.Iterable<java.lang.String> routees, scala.Option<Resizer> resizer, java.lang.String routerDispatcher, SupervisorStrategy supervisorStrategy, int virtualNodesFactor, scala.PartialFunction<java.lang.Object,java.lang.Object> hashMapping)
           
ConsistentHashingRouter(java.lang.Iterable<java.lang.String> routeePaths)
          Java API: Constructor that sets the routees to be used.
ConsistentHashingRouter(Resizer resizer)
          Java API: Constructor that sets the resizer to be used.
 
Method Summary
static ConsistentHashingRouter apply(scala.collection.immutable.Iterable<ActorRef> routees)
          Creates a new ConsistentHashingRouter, routing to the specified routees
static ConsistentHashingRouter create(java.lang.Iterable<ActorRef> routees)
           
 Router createRouter(ActorSystem system)
          Create the actual router, responsible for routing messages to routees.
 scala.PartialFunction<java.lang.Object,java.lang.Object> hashMapping()
           
 int nrOfInstances()
          Initial number of routee instances
 RouterConfig overrideUnsetConfig(RouterConfig other)
           
 scala.collection.immutable.Iterable<java.lang.String> paths()
           
 scala.Option<Resizer> resizer()
          Pool with dynamically resizable number of routees return the Resizer to use.
 scala.collection.immutable.Iterable<java.lang.String> routees()
           
 java.lang.String routerDispatcher()
          Dispatcher ID to use for running the “head” actor, which handles supervision, death watch and router management messages
 SupervisorStrategy supervisorStrategy()
          SupervisorStrategy for the head actor, i.e.
 int virtualNodesFactor()
           
 ConsistentHashingRouter withDispatcher(java.lang.String dispatcherId)
          Java API for setting routerDispatcher
 RouterConfig withFallback(RouterConfig other)
          Uses the resizer and/or the supervisor strategy of the given Routerconfig if this RouterConfig doesn't have one, i.e.
 ConsistentHashingRouter withHashMapper(ConsistentHashingRouter.ConsistentHashMapper mapping)
          Java API for setting the mapping from message to the data to use for the consistent hash key.
 ConsistentHashingRouter withResizer(Resizer resizer)
          Java API for setting the resizer to be used.
 ConsistentHashingRouter withSupervisorStrategy(SupervisorStrategy strategy)
          Java API for setting the supervisor strategy to be used for the “head” Router actor.
 ConsistentHashingRouter withVirtualNodesFactor(int vnodes)
          Java API for setting the number of virtual nodes per node, used in ConsistentHash
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface akka.routing.Group
createRouterActor, props, routeeFor
 
Methods inherited from interface akka.routing.Pool
createRouterActor, defaultSupervisorStrategy, enrichWithPoolDispatcher, newRoutee, props, stopRouterWhenAllRouteesRemoved, usePoolDispatcher
 
Methods inherited from interface akka.routing.RouterConfig
isManagementMessage, routingLogicController, verifyConfig
 
Methods inherited from interface scala.Product
productArity, productElement, productIterator, productPrefix
 
Methods inherited from interface scala.Equals
canEqual, equals
 

Constructor Detail

ConsistentHashingRouter

public ConsistentHashingRouter(int nrOfInstances,
                               scala.collection.immutable.Iterable<java.lang.String> routees,
                               scala.Option<Resizer> resizer,
                               java.lang.String routerDispatcher,
                               SupervisorStrategy supervisorStrategy,
                               int virtualNodesFactor,
                               scala.PartialFunction<java.lang.Object,java.lang.Object> hashMapping)

ConsistentHashingRouter

public ConsistentHashingRouter(int nr)
Java API: Constructor that sets nrOfInstances to be created.


ConsistentHashingRouter

public ConsistentHashingRouter(java.lang.Iterable<java.lang.String> routeePaths)
Java API: Constructor that sets the routees to be used.

Parameters:
routeePaths - string representation of the actor paths of the routees that will be looked up using actorFor in ActorRefProvider

ConsistentHashingRouter

public ConsistentHashingRouter(Resizer resizer)
Java API: Constructor that sets the resizer to be used.

Method Detail

apply

public static ConsistentHashingRouter apply(scala.collection.immutable.Iterable<ActorRef> routees)
Creates a new ConsistentHashingRouter, routing to the specified routees


create

public static ConsistentHashingRouter create(java.lang.Iterable<ActorRef> routees)

nrOfInstances

public int nrOfInstances()
Description copied from interface: Pool
Initial number of routee instances

Specified by:
nrOfInstances in interface Pool

routees

public scala.collection.immutable.Iterable<java.lang.String> routees()

resizer

public scala.Option<Resizer> resizer()
Description copied from interface: Pool
Pool with dynamically resizable number of routees return the Resizer to use. The resizer is invoked once when the router is created, before any messages can be sent to it. Resize is also triggered when messages are sent to the routees, and the resizer is invoked asynchronously, i.e. not necessarily before the message has been sent.

Specified by:
resizer in interface Pool

routerDispatcher

public java.lang.String routerDispatcher()
Description copied from interface: RouterConfig
Dispatcher ID to use for running the “head” actor, which handles supervision, death watch and router management messages

Specified by:
routerDispatcher in interface RouterConfig

supervisorStrategy

public SupervisorStrategy supervisorStrategy()
Description copied from interface: Pool
SupervisorStrategy for the head actor, i.e. for supervising the routees of the pool.

Specified by:
supervisorStrategy in interface Pool

virtualNodesFactor

public int virtualNodesFactor()

hashMapping

public scala.PartialFunction<java.lang.Object,java.lang.Object> hashMapping()

paths

public scala.collection.immutable.Iterable<java.lang.String> paths()
Specified by:
paths in interface Group

withDispatcher

public ConsistentHashingRouter withDispatcher(java.lang.String dispatcherId)
Java API for setting routerDispatcher


withSupervisorStrategy

public ConsistentHashingRouter withSupervisorStrategy(SupervisorStrategy strategy)
Java API for setting the supervisor strategy to be used for the “head” Router actor.


withResizer

public ConsistentHashingRouter withResizer(Resizer resizer)
Java API for setting the resizer to be used.


withVirtualNodesFactor

public ConsistentHashingRouter withVirtualNodesFactor(int vnodes)
Java API for setting the number of virtual nodes per node, used in ConsistentHash


withHashMapper

public ConsistentHashingRouter withHashMapper(ConsistentHashingRouter.ConsistentHashMapper mapping)
Java API for setting the mapping from message to the data to use for the consistent hash key.


withFallback

public RouterConfig withFallback(RouterConfig other)
Uses the resizer and/or the supervisor strategy of the given Routerconfig if this RouterConfig doesn't have one, i.e. the resizer defined in code is used if resizer was not defined in config. Uses the the hashMapping defined in code, since that can't be defined in configuration.

Specified by:
withFallback in interface RouterConfig

createRouter

public Router createRouter(ActorSystem system)
Description copied from interface: RouterConfig
Create the actual router, responsible for routing messages to routees.

Specified by:
createRouter in interface RouterConfig
Parameters:
system - the ActorSystem this router belongs to

overrideUnsetConfig

public RouterConfig overrideUnsetConfig(RouterConfig other)