/** * @param serviceInstances A list of service instances * @return The next available instance or a {@link NoAvailableServiceException} if none */ protected ServiceInstance getNextAvailable(List<ServiceInstance> serviceInstances) { List<ServiceInstance> availableServices = serviceInstances.stream() .filter(si -> si.getHealthStatus().equals(HealthStatus.UP)) .collect(Collectors.toList()); int len = availableServices.size(); if (len == 0) { throw new NoAvailableServiceException(getServiceID()); } int i = index.getAndAccumulate(len, (cur, n) -> cur >= n - 1 ? 0 : cur + 1); try { return availableServices.get(i); } catch (IndexOutOfBoundsException e) { throw new NoAvailableServiceException(getServiceID()); } } }
/** * @param serviceInstances A list of service instances * @return The next available instance or a {@link NoAvailableServiceException} if none */ protected ServiceInstance getNextAvailable(List<ServiceInstance> serviceInstances) { List<ServiceInstance> availableServices = serviceInstances.stream() .filter(si -> si.getHealthStatus().equals(HealthStatus.UP)) .collect(Collectors.toList()); int len = availableServices.size(); if (len == 0) { throw new NoAvailableServiceException(getServiceID()); } int i = index.getAndAccumulate(len, (cur, n) -> cur >= n - 1 ? 0 : cur + 1); return availableServices.get(i); } }
/** * @param serviceInstances A list of service instances * @return The next available instance or a {@link NoAvailableServiceException} if none */ protected ServiceInstance getNextAvailable(List<ServiceInstance> serviceInstances) { List<ServiceInstance> availableServices = serviceInstances.stream() .filter(si -> si.getHealthStatus().equals(HealthStatus.UP)) .collect(Collectors.toList()); int len = availableServices.size(); if (len == 0) { throw new NoAvailableServiceException(getServiceID()); } int i = index.getAndAccumulate(len, (cur, n) -> cur >= n - 1 ? 0 : cur + 1); try { return availableServices.get(i); } catch (IndexOutOfBoundsException e) { throw new NoAvailableServiceException(getServiceID()); } } }