Class ConsistentHash
java.lang.Object
org.axonframework.commandhandling.distributed.ConsistentHash
Component used by command routers to find members capable of handling a given command. Members are selected based on
their capabilities and a given routing key. If a ConsistentHash is asked to provide a member for a given routing key
it will always returning the same Member as long as none of the memberships have changed.
A ConsistentHash is used to route commands targeting the same aggregate to the same member. In that case the aggregate identifier is the routing key.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classMember implementation used by aConsistentHashregistry. -
Constructor Summary
ConstructorsConstructorDescriptionInitializes a newConsistentHash.ConsistentHash(Function<String, String> hashFunction) Initializes a newConsistentHashusing the givenhashFunctionto calculate positions for each member on the ring. -
Method Summary
Modifier and TypeMethodDescriptionbooleangetEligibleMembers(String routingKey) Returns the collection of nodes, represented asConsistentHash.ConsistentHashMember, in the order they would be considered for the given routing key.getMember(String routingKey, CommandMessage<?> commandMessage) Returns the member instance to which the givenmessageshould be routed.Returns the set of members registered with this consistent hash instance.protected static StringReturns the hash of the givenroutingKey.inthashCode()toString()intversion()Returns the version of this consistent hash instance.with(Member member, int loadFactor, CommandMessageFilter commandFilter) Registers the givenmemberwith givenloadFactorandcommandFilterif it is not already contained in theConsistentHash.Deregisters the givenmemberand returns a newConsistentHashwith updated memberships.
-
Constructor Details
-
ConsistentHash
public ConsistentHash()Initializes a newConsistentHash. To register members usewith(Member, int, CommandMessageFilter). -
ConsistentHash
Initializes a newConsistentHashusing the givenhashFunctionto calculate positions for each member on the ring. To register members usewith(Member, int, CommandMessageFilter).- Parameters:
hashFunction- The hash function to use to calculate each member's positions on the ring
-
-
Method Details
-
hash
Returns the hash of the givenroutingKey. By default this creates a MD5 hash with hex encoding.- Parameters:
routingKey- the routing key to hash- Returns:
- a hash of the input key
-
getEligibleMembers
Returns the collection of nodes, represented asConsistentHash.ConsistentHashMember, in the order they would be considered for the given routing key. Whether a CommandMessage would be forwarded to each of the candidates, depends on the Command Filter of each node.- Parameters:
routingKey- The routing key to select ordering- Returns:
- A collection containing each of the nodes, in the order they would be considered
-
getMember
Returns the member instance to which the givenmessageshould be routed. If no suitable member could be found an empty Optional is returned.- Parameters:
routingKey- the routing that should be used to select a membercommandMessage- the command message to find a member for- Returns:
- the member that should handle the message or an empty Optional if no suitable member was found
-
getMembers
Returns the set of members registered with this consistent hash instance.- Returns:
- the members of this consistent hash
-
with
Registers the givenmemberwith givenloadFactorandcommandFilterif it is not already contained in theConsistentHash. It will return the current ConsistentHash if the addition is a duplicate and returns a new ConsistentHash with updated memberships if it is not.The relative loadFactor of the member determines the likelihood of being selected as a destination for a command.
- Parameters:
member- the member to registerloadFactor- the load factor of the new membercommandFilter- filter describing which commands can be handled by the given member- Returns:
- a new
ConsistentHashinstance with updated memberships
-
without
Deregisters the givenmemberand returns a newConsistentHashwith updated memberships.- Parameters:
member- the member to remove from the consistent hash- Returns:
- a new
ConsistentHashinstance with updated memberships
-
equals
-
hashCode
public int hashCode() -
toString
-
version
public int version()Returns the version of this consistent hash instance. The version is increased by one each time a change is made.- Returns:
- the version of this consistent hash instance
-