Class ConsistentHash

java.lang.Object
org.axonframework.commandhandling.distributed.ConsistentHash

public class ConsistentHash extends Object
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.

  • Constructor Details

  • Method Details

    • hash

      protected static String hash(String routingKey)
      Returns the hash of the given routingKey. 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

      public Collection<ConsistentHash.ConsistentHashMember> getEligibleMembers(String routingKey)
      Returns the collection of nodes, represented as ConsistentHash.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

      public Optional<Member> getMember(String routingKey, CommandMessage<?> commandMessage)
      Returns the member instance to which the given message should 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 member
      commandMessage - 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

      public Set<Member> getMembers()
      Returns the set of members registered with this consistent hash instance.
      Returns:
      the members of this consistent hash
    • with

      public ConsistentHash with(Member member, int loadFactor, CommandMessageFilter commandFilter)
      Registers the given member with given loadFactor and commandFilter if it is not already contained in the ConsistentHash. 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 register
      loadFactor - the load factor of the new member
      commandFilter - filter describing which commands can be handled by the given member
      Returns:
      a new ConsistentHash instance with updated memberships
    • without

      public ConsistentHash without(Member member)
      Deregisters the given member and returns a new ConsistentHash with updated memberships.
      Parameters:
      member - the member to remove from the consistent hash
      Returns:
      a new ConsistentHash instance with updated memberships
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • 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