public void releaseLock() { try { KeyValueClient kv = consul.keyValueClient(); Optional<Value> value = kv.getValue(path); if (value.isPresent()) { Optional<String> session = value.get().getSession(); if (session.isPresent()) { kv.releaseLock(path, session.get()); } } } finally { destroySession(); } } }
@InvokeOnHeader(ConsulKeyValueActions.GET_VALUE) protected void getValue(Message message) throws Exception { Object result; Boolean asString = message.getHeader(ConsulConstants.CONSUL_VALUE_AS_STRING, getConfiguration().isValueAsString(), Boolean.class); if (asString) { result = getClient().getValueAsString( getMandatoryHeader(message, ConsulConstants.CONSUL_KEY, getConfiguration().getKey(), String.class) ).orElse(null); } else { result = getClient().getValue( getMandatoryHeader(message, ConsulConstants.CONSUL_KEY, getConfiguration().getKey(), String.class), message.getHeader(ConsulConstants.CONSUL_OPTIONS, QueryOptions.BLANK, QueryOptions.class) ).orElse(null); } setBodyAndResult(message, result); }
public boolean acquireLock(long maxWait, TimeUnit unit) { KeyValueClient kv = consul.keyValueClient(); sessionId = createSession(); Optional<Value> value = kv.getValue(path); if (kv.acquireLock(path, sessionId)) { return true; } BigInteger index = BigInteger.valueOf(value.get().getModifyIndex()); kv.getValue(path, QueryOptions.blockMinutes((int) unit.toMinutes(maxWait), index).build()); if (!kv.acquireLock(path, sessionId)) { destroySession(); return false; } else { return true; } }
/** * Retrieves a {@link com.orbitz.consul.model.kv.Value} for a specific key * from the key/value store. * * GET /v1/kv/{key} * * @param key The key to retrieve. * @return An {@link Optional} containing the value or {@link Optional#empty()()} */ public Optional<Value> getValue(String key) { return getValue(key, QueryOptions.BLANK); }
/** * Retrieves a session string for a specific key from the key/value store. * * GET /v1/kv/{key} * * @param key The key to retrieve. * @return An {@link Optional} containing the value as a string or * {@link Optional#empty()} */ public Optional<String> getSession(String key) { return getValue(key).flatMap(Value::getSession); }
/** {@inheritDoc} */ @Override public boolean existKey(String key) { return getKeyValueClient().getValue(key).isPresent(); }
/** * 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)); }
@Override public void watch(KeyValueClient client) { client.getValue(key, queryOptions(), this); }
@Override public void run() { KeyValueClient keyValueClient = consul.keyValueClient(); Optional<Value> optValue = keyValueClient.getValue("com/lkl/consul/kvs"); if (optValue.isPresent()) { Value value = optValue.get(); long flag = value.getFlags(); System.out.println("flag:" + flag); String propString = new String(Base64.decodeBase64(value.getValue().get())); System.out.println(propString); } } }
public boolean releaseLockForService(final String serviceName) { final String key = getServiceKey(serviceName); KeyValueClient kv = client.keyValueClient(); Optional<Value> value = kv.getValue(key); if(value.isPresent() && value.get().getSession().isPresent()) { return kv.releaseLock(key, value.get().getSession().get()); } else { return true; } }
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(); } } }
@Override public void run() { if (isRunAllowed()) { // Refresh session sessionClient.renewSession(sessionId); keyValueClient.getValue( servicePath, QueryOptions.blockSeconds(ttl / 3, index.get()).build(), this ); } } }
Consul consul = Consul.builder().withHostAndPort(hostAndPort).build(); KeyValueClient kvClient = consul.keyValueClient(); Optional<Value> valueOpt = kvClient.getValue(reqKey); if(!valueOpt.isPresent()) { LOG.log(Level.FINE, "key not found in consul: " + reqKey);
public void watch() { if (sessionId.get() == null) { return; } if (isStarting() || isStarted()) { // Watch for changes keyValueClient.getValue( path, QueryOptions.blockSeconds(configuration.getSessionRefreshInterval(), index.get()).build(), this ); if (sessionId.get() != null) { // Refresh session sessionClient.renewSession(sessionId.get()); } } } }
LOGGER.info("Reading connector config from Consul key: {}", configLocation); final String config = consul.keyValueClient().getValue(configLocation) .orElseThrow(() -> new ConfigException("missing Consul config key: " + configLocation)) .getValueAsString(UTF_8)