@Override
public Member getNextMember(AlgorithmContext algorithmContext) {
if (members.size() == 0) {
return null;
}
Member current = null;
lock.lock();
try {
int currentMemberIndex = algorithmContext.getCurrentMemberIndex();
if (currentMemberIndex >= members.size()) {
currentMemberIndex = 0;
}
if (log.isDebugEnabled()) {
log.debug(String.format("Searching for next member: [service] %s [cluster]: %s [member-count]: %d [current-index] %d", algorithmContext.getServiceName(), algorithmContext.getClusterId(), members.size(), currentMemberIndex));
}
int index = members.size();
do {
current = members.get(currentMemberIndex);
if (currentMemberIndex == members.size() - 1) {
currentMemberIndex = 0;
} else {
currentMemberIndex++;
}
index--;
} while ((!current.isActive()) && index > 0);
algorithmContext.setCurrentMemberIndex(currentMemberIndex);
} finally {
lock.unlock();
}
return current;