
com.orbitz.consul.cache.HealthCheckCache Maven / Gradle / Ivy
package com.orbitz.consul.cache;
import com.google.common.primitives.Ints;
import com.orbitz.consul.HealthClient;
import com.orbitz.consul.config.CacheConfig;
import com.orbitz.consul.model.health.HealthCheck;
import com.orbitz.consul.option.QueryOptions;
import java.util.concurrent.ScheduledExecutorService;
import java.util.function.Function;
public class HealthCheckCache extends ConsulCache {
private HealthCheckCache(HealthClient healthClient,
com.orbitz.consul.model.State state,
int watchSeconds,
QueryOptions queryOptions,
Function keyExtractor,
Scheduler callbackScheduler) {
super(keyExtractor,
(index, callback) -> {
checkWatch(healthClient.getNetworkTimeoutConfig().getClientReadTimeoutMillis(), watchSeconds);
QueryOptions params = watchParams(index, watchSeconds, queryOptions);
healthClient.getChecksByState(state, params, callback);
},
healthClient.getConfig().getCacheConfig(),
healthClient.getEventHandler(),
new CacheDescriptor("health.state", state.getName()),
callbackScheduler);
}
/**
* Factory method to construct a string/{@link HealthCheck} map for a particular {@link com.orbitz.consul.model.State}.
*
* Keys will be the {@link HealthCheck#getCheckId()}.
*
* @param healthClient the {@link HealthClient}
* @param state the state fo filter checks
* @return a cache object
*/
public static HealthCheckCache newCache(
final HealthClient healthClient,
final com.orbitz.consul.model.State state,
final int watchSeconds,
final QueryOptions queryOptions,
final Function keyExtractor,
final ScheduledExecutorService callbackExecutorService) {
Scheduler callbackScheduler = createExternal(callbackExecutorService);
return new HealthCheckCache(healthClient, state, watchSeconds, queryOptions, keyExtractor, callbackScheduler);
}
public static HealthCheckCache newCache(
final HealthClient healthClient,
final com.orbitz.consul.model.State state,
final int watchSeconds,
final QueryOptions queryOptions,
final Function keyExtractor) {
return new HealthCheckCache(healthClient, state, watchSeconds, queryOptions, keyExtractor, createDefault());
}
public static HealthCheckCache newCache(
final HealthClient healthClient,
final com.orbitz.consul.model.State state,
final int watchSeconds,
final QueryOptions queryOptions) {
return newCache(healthClient, state, watchSeconds, queryOptions, HealthCheck::getCheckId);
}
public static HealthCheckCache newCache(
final HealthClient healthClient,
final com.orbitz.consul.model.State state,
final int watchSeconds) {
return newCache(healthClient, state, watchSeconds, QueryOptions.BLANK);
}
public static HealthCheckCache newCache(final HealthClient healthClient, final com.orbitz.consul.model.State state) {
CacheConfig cacheConfig = healthClient.getConfig().getCacheConfig();
int watchSeconds = Ints.checkedCast(cacheConfig.getWatchDuration().getSeconds());
return newCache(healthClient, state, watchSeconds);
}
}