/** * {@inheritDoc} * <p> * Returns a lazily initialized value of the {@link Value#getValueAsString() valueAsString} attribute. * Initialized once and only once and stored for subsequent access with proper synchronization. * @return A lazily initialized value of the {@code l.name} attribute */ @Override public Optional<String> getValueAsString() { if ((lazyInitBitmap & VALUE_AS_STRING_LAZY_INIT_BIT) == 0) { synchronized (this) { if ((lazyInitBitmap & VALUE_AS_STRING_LAZY_INIT_BIT) == 0) { this.valueAsString = Preconditions.checkNotNull(super.getValueAsString(), "valueAsString"); lazyInitBitmap |= VALUE_AS_STRING_LAZY_INIT_BIT; } } } return valueAsString; }
@JsonIgnore @org.immutables.value.Value.Lazy public Optional<String> getValueAsString() { return getValueAsString(Charset.defaultCharset()); }
/** * Retrieves a string value for a specific key from the key/value store. * * GET /v1/kv/{key} * * @param key The key to retrieve. * @param charset The charset of the value * @return An {@link Optional} containing the value as a string or * {@link Optional#empty()} */ public Optional<String> getValueAsString(String key, Charset charset) { return getValue(key).flatMap(v -> v.getValueAsString(charset)); }
private static Function<ConsulResponse<Value>, String> valueAsString() { return value -> value.getResponse().getValueAsString(UTF_8).orElse(""); }
/** * Retrieves a list of string values for a specific key from the key/value * store. * * GET /v1/kv/{key}?recurse * * @param key The key to retrieve. * @param charset The charset of the value * @return A list of zero to many string values. */ public List<String> getValuesAsString(String key, Charset charset) { List<String> result = new ArrayList<>(); for(Value value : getValues(key)) { value.getValueAsString(charset).ifPresent(result::add); } return result; }
private void reload() { Map<String, String> newConsulValues = new HashMap<>(); List<Value> valueList; try { LOG.debug("Reloading configuration from Consuls' K-V store"); valueList = kvClient.getValues("/"); } catch (Exception e) { throw new SourceCommunicationException("Can't get values from k-v store", e); } for (Value value : valueList) { String val = ""; if (value.getValueAsString().isPresent()) { val = value.getValueAsString().get(); } LOG.trace("Consul provided configuration key: " + value.getKey() + " with value: " + val); newConsulValues.put(value.getKey(), val); } consulValues = newConsulValues; }
public Optional<String> getLeaderInfoForService(final String serviceName) { String key = getServiceKey(serviceName); Optional<Value> value = client.keyValueClient().getValue(key); return value.flatMap(val -> { if(val.getSession().isPresent()) { return val.getValueAsString(); } return Optional.empty(); }); }
@Override public void run() { Consul consulThread = Consul.builder().build(); // connect to Consul on localhost KeyValueClient kvClientThread = consulThread.keyValueClient(); while (true) { configProps.keySet().forEach((s) -> { Optional<Value> te = kvClientThread.getValue(addSn(s)); if (!te.get().getValueAsString().get().equals(configProps.get(s))) { callback.onChange(); configProps.put(s, te.get().getValueAsString().get()); } }); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } } }
public static Map<String, String> getKeyValue(String route) { Consul consul = consulInstance(); KeyValueClient keyValueClient = consul.keyValueClient(); Map<String, String> keyValues = new HashMap<>(); ConsulResponse<List<Value>> consulResponseWithValues = keyValueClient.getConsulResponseWithValues(route); if(consulResponseWithValues.getResponse() != null) { for (Value value: consulResponseWithValues.getResponse()) { String key = value.getKey(); String v = ""; if(value.getValueAsString().isPresent()) { v = value.getValueAsString().get(); } LOG.info("Load key: " + route + " value: " + v); keyValues.put(key, v); } } return keyValues; }
protected void onValue(Value value) { final Exchange exchange = endpoint.createExchange(); final Message message = exchange.getIn(); message.setHeader(ConsulConstants.CONSUL_KEY, value.getKey()); message.setHeader(ConsulConstants.CONSUL_RESULT, true); message.setHeader(ConsulConstants.CONSUL_FLAGS, value.getFlags()); message.setHeader(ConsulConstants.CONSUL_CREATE_INDEX, value.getCreateIndex()); message.setHeader(ConsulConstants.CONSUL_LOCK_INDEX, value.getLockIndex()); message.setHeader(ConsulConstants.CONSUL_MODIFY_INDEX, value.getModifyIndex()); if (value.getSession().isPresent()) { message.setHeader(ConsulConstants.CONSUL_SESSION, value.getSession().get()); } message.setBody(configuration.isValueAsString() ? value.getValueAsString().orElse(null) : value.getValue().orElse(null)); try { getProcessor().process(exchange); } catch (Exception e) { getExceptionHandler().handleException("Error processing exchange", exchange, e); } }
public static void atomicUpdate(KeyValueClient kv, ConsulResponse<Value> initialResponse, Function<String, String> mutator) throws IOException { final Value v = initialResponse.getResponse(); final String key = v.getKey(); LOGGER.debug("Updating key {}", key); final String oldValue = v.getValueAsString(UTF_8).orElse(missingDocumentValue); final String newValue = mutator.apply(oldValue); if (Objects.equals(newValue, oldValue)) { return; } final long index = initialResponse.getIndex().longValue(); final PutOptions options = ImmutablePutOptions.builder().cas(index).build(); boolean success = kv.putValue(key, newValue, 0, options, UTF_8); if (!success) { LOGGER.info("Failed to put new document (optimistic locking failure?); reloading and retrying"); atomicUpdate(kv, key, mutator); } }
private static void atomicUpdate(KeyValueClient kv, String key, Function<String, String> mutator) throws IOException { while (true) { final ConsulResponse<Value> r = kv.getConsulResponseWithValue(key).orElse(null); if (r == null) { // Don't automatically create the document, because it might need to be associated with another node's session. // For example, an RPC endpoint doc is updated by both client and server, but is tied to the server session. throw new IOException("Can't update non-existent document: " + key); } final BigInteger index = r.getIndex(); final String oldValue = r.getResponse().getValueAsString(UTF_8).orElse(missingDocumentValue); final String newValue = mutator.apply(oldValue); if (Objects.equals(newValue, oldValue)) { return; } final PutOptions options = ImmutablePutOptions.builder().cas(index.longValue()).build(); boolean success = kv.putValue(key, newValue, 0, options, UTF_8); if (success) { return; } // todo truncated exponential backoff, please! Die if timeout! //MILLISECONDS.sleep(100); } }
LOGGER.debug("Long poll timed out, polling again for {}", key); } else { final String valueAsString = response.getResponse().getValueAsString(UTF_8).orElse(null); final T mappedValue = mapper.apply(valueAsString); if (condition.test(mappedValue)) {
.getValueAsString(UTF_8) .orElseThrow(() -> new ConfigException("missing value for Consul key: " + configLocation));
final String json = response.getResponse().getValueAsString(UTF_8).orElse("{}"); final EndpointDocument initialEndpoint = mapper.readValue(json, EndpointDocument.class);