/** * Returns the member instance to which the given {@code message} should be routed. If no suitable member could be * found an empty Optional is returned. * * @param routingKey the routing that should be used to select a member * @param commandMessage the command message to find a member for * @return the member that should handle the message or an empty Optional if no suitable member was found */ public Optional<Member> getMember(String routingKey, CommandMessage<?> commandMessage) { String hash = hash(routingKey); Optional<Member> foundMember = findSuitableMember(commandMessage, hashToMember.tailMap(hash).values()); if (!foundMember.isPresent()) { foundMember = findSuitableMember(commandMessage, hashToMember.headMap(hash).values()); } return foundMember; }
/** * Returns the member instance to which the given {@code message} should be routed. If no suitable member could be * found an empty Optional is returned. * * @param routingKey the routing that should be used to select a member * @param commandMessage the command message to find a member for * @return the member that should handle the message or an empty Optional if no suitable member was found */ public Optional<Member> getMember(String routingKey, CommandMessage<?> commandMessage) { String hash = hash(routingKey); SortedMap<String, ConsistentHashMember> tailMap = hashToMember.tailMap(hash); Iterator<Map.Entry<String, ConsistentHashMember>> tailIterator = tailMap.entrySet().iterator(); Optional<Member> foundMember = findSuitableMember(commandMessage, tailIterator); if (!foundMember.isPresent()) { Iterator<Map.Entry<String, ConsistentHashMember>> headIterator = hashToMember.headMap(hash).entrySet().iterator(); foundMember = findSuitableMember(commandMessage, headIterator); } return foundMember; }
/** * Returns the member instance to which the given {@code message} should be routed. If no suitable member could be * found an empty Optional is returned. * * @param routingKey the routing that should be used to select a member * @param commandMessage the command message to find a member for * @return the member that should handle the message or an empty Optional if no suitable member was found */ public Optional<Member> getMember(String routingKey, CommandMessage<?> commandMessage) { String hash = hash(routingKey); SortedMap<String, ConsistentHashMember> tailMap = hashToMember.tailMap(hash); Iterator<Map.Entry<String, ConsistentHashMember>> tailIterator = tailMap.entrySet().iterator(); Optional<Member> foundMember = findSuitableMember(commandMessage, tailIterator); if (!foundMember.isPresent()) { Iterator<Map.Entry<String, ConsistentHashMember>> headIterator = hashToMember.headMap(hash).entrySet().iterator(); foundMember = findSuitableMember(commandMessage, headIterator); } return foundMember; }
/** * Returns the member for the given <code>item</code>, that supports given <code>commandType</code>. If no such * member is available, this method returns <code>null</code>. * * @param item The item to find a node name for * @param commandType The type of command the member must support * @return The node name for the given <code>item</code>, or <code>null</code> if not found */ public String getMember(String item, String commandType) { String hash = Digester.md5Hex(item); SortedMap<String, Member> tailMap = hashToMember.tailMap(hash); Iterator<Map.Entry<String, Member>> tailIterator = tailMap.entrySet().iterator(); Member foundMember = findSuitableMember(commandType, tailIterator); if (foundMember == null) { // if the tail doesn't have a member, we should start back at the head Iterator<Map.Entry<String, Member>> headIterator = hashToMember.headMap(hash).entrySet().iterator(); foundMember = findSuitableMember(commandType, headIterator); } return foundMember == null ? null : foundMember.name(); }