State state = null; State[] allStates = state.values();
/** * Returns the appropriate {@link com.orbitz.consul.model.State} given the * name. * * @param name The state name e.g. "passing". * @return The state. */ public static State fromName(String name) { for(State state : values()) { if(state.getName().equals(name)) { return state; } } throw new IllegalArgumentException("Invalid State name"); } }
/** * Asynchronously retrieves the healthchecks for a state in a given datacenter with {@link com.orbitz.consul.option.QueryOptions}. * <p/> * GET /v1/health/state/{state}?dc={datacenter} * * @param state The state to query. * @param queryOptions The Query Options to use. * @param callback Callback implemented by callee to handle results. * @return A {@link com.orbitz.consul.model.ConsulResponse} containing a list of * {@link com.orbitz.consul.model.health.HealthCheck} objects. */ public void getChecksByState(State state, QueryOptions queryOptions, ConsulResponseCallback<List<HealthCheck>> callback) { http.extractConsulResponse(api.getChecksByState(state.getName(), queryOptions.toQuery(), queryOptions.getTag(), queryOptions.getNodeMeta()), callback); }
/** * Checks in with Consul. * * @param checkId The Check ID to check in. * @param state The current state of the Check. * @param note Any note to associate with the Check. */ public void check(String checkId, State state, String note) throws NotRegisteredException { try { Map<String, String> query = note == null ? Collections.emptyMap() : ImmutableMap.of("note", note); http.handle(api.check(state.getPath(), checkId, query)); } catch (Exception ex) { throw new NotRegisteredException("Error checking state", ex); } }
/** * Retrieves the healthchecks for a state in a given datacenter with {@link com.orbitz.consul.option.QueryOptions}. * <p/> * GET /v1/health/state/{state}?dc={datacenter} * * @param state The state to query. * @param queryOptions The Query Options to use. * @return A {@link com.orbitz.consul.model.ConsulResponse} containing a list of * {@link com.orbitz.consul.model.health.HealthCheck} objects. */ public ConsulResponse<List<HealthCheck>> getChecksByState(State state, QueryOptions queryOptions) { return http.extractConsulResponse(api.getChecksByState(state.getName(), queryOptions.toQuery(), queryOptions.getTag(), queryOptions.getNodeMeta())); }
/** * Factory method to construct a string/{@link HealthCheck} map for a particular {@link com.orbitz.consul.model.State}. * <p/> * 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, Function<HealthCheck, String> keyExtractor) { final CallbackConsumer<HealthCheck> callbackConsumer = (index, callback) -> { QueryOptions params = watchParams(index, watchSeconds, queryOptions); healthClient.getChecksByState(state, params, callback); }; CacheDescriptor cacheDescriptor = new CacheDescriptor("health.state", state.getName()); return new HealthCheckCache(keyExtractor, callbackConsumer, healthClient.getConfig().getCacheConfig(), healthClient.getEventHandler(), cacheDescriptor); }