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; } }
Map<String,String> props = new HashMap<>(); props.put(reqKey+".createIndex", String.valueOf(value.getCreateIndex())); props.put(reqKey+".modifyIndex", String.valueOf(value.getModifyIndex())); props.put(reqKey+".lockIndex", String.valueOf(value.getLockIndex())); props.put(reqKey+".flags", String.valueOf(value.getFlags()));
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); } }
/** * Fill a builder with attribute values from the provided {@code Value} instance. * Regular attribute values will be replaced with those from the given instance. * Absent optional values will not replace present values. * @param instance The instance from which to copy values * @return {@code this} builder for use in a chained invocation */ public final Builder from(Value instance) { Preconditions.checkNotNull(instance, "instance"); createIndex(instance.getCreateIndex()); modifyIndex(instance.getModifyIndex()); lockIndex(instance.getLockIndex()); key(instance.getKey()); flags(instance.getFlags()); Optional<String> valueOptional = instance.getValue(); if (valueOptional.isPresent()) { value(valueOptional); } Optional<String> sessionOptional = instance.getSession(); if (sessionOptional.isPresent()) { session(sessionOptional); } return this; }