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(); } } }
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; } }
/** * Creates an immutable copy of a {@link Operation} value. * Uses accessors to get values to initialize the new immutable instance. * If an instance is already immutable, it is returned as is. * @param instance The instance to copy * @return A copied immutable Operation instance */ public static ImmutableOperation copyOf(Operation instance) { if (instance instanceof ImmutableOperation) { return (ImmutableOperation) instance; } return ImmutableOperation.builder() .from(instance) .build(); }
/** * Fill a builder with attribute values from the provided {@code TxResponse} instance. * Regular attribute values will be replaced with those from the given instance. * Absent optional values will not replace present values. * Collection elements and entries will be added, not replaced. * @param instance The instance from which to copy values * @return {@code this} builder for use in a chained invocation */ public final Builder from(TxResponse instance) { Preconditions.checkNotNull(instance, "instance"); addAllResults(instance.results()); addAllErrors(instance.errors()); return this; }
/** * Creates an immutable copy of a {@link TxResponse} value. * Uses accessors to get values to initialize the new immutable instance. * If an instance is already immutable, it is returned as is. * @param instance The instance to copy * @return A copied immutable TxResponse instance */ public static ImmutableTxResponse copyOf(TxResponse instance) { if (instance instanceof ImmutableTxResponse) { return (ImmutableTxResponse) instance; } return ImmutableTxResponse.builder() .from(instance) .build(); }
/** * Creates an immutable copy of a {@link Value} value. * Uses accessors to get values to initialize the new immutable instance. * If an instance is already immutable, it is returned as is. * @param instance The instance to copy * @return A copied immutable Value instance */ public static ImmutableValue copyOf(Value instance) { if (instance instanceof ImmutableValue) { return (ImmutableValue) instance; } return ImmutableValue.builder() .from(instance) .build(); }
/** * Creates an immutable copy of a {@link TxError} value. * Uses accessors to get values to initialize the new immutable instance. * If an instance is already immutable, it is returned as is. * @param instance The instance to copy * @return A copied immutable TxError instance */ public static ImmutableTxError copyOf(TxError instance) { if (instance instanceof ImmutableTxError) { return (ImmutableTxError) instance; } return ImmutableTxError.builder() .from(instance) .build(); }
/** * Builds a new {@link ImmutableOperation ImmutableOperation}. * @return An immutable instance of Operation * @throws java.lang.IllegalStateException if any required attributes are missing */ public ImmutableOperation build() { if (initBits != 0) { throw new IllegalStateException(formatRequiredAttributesMessage()); } return new ImmutableOperation(verb, key, value, flags, index, session); }
/** * Builds a new {@link ImmutableValue ImmutableValue}. * @return An immutable instance of Value * @throws java.lang.IllegalStateException if any required attributes are missing */ public ImmutableValue build() { if (initBits != 0) { throw new IllegalStateException(formatRequiredAttributesMessage()); } return new ImmutableValue(createIndex, modifyIndex, lockIndex, key, flags, value, session); }
/** * Copy the current immutable object by setting a value for the {@link Value#getModifyIndex() modifyIndex} attribute. * A value equality check is used to prevent copying of the same value by returning {@code this}. * @param value A new value for modifyIndex * @return A modified copy of the {@code this} object */ public final ImmutableValue withModifyIndex(long value) { if (this.modifyIndex == value) return this; return new ImmutableValue(this.createIndex, value, this.lockIndex, this.key, this.flags, this.value, this.session); }
/** * Creates a builder for {@link ImmutableValue ImmutableValue}. * @return A new ImmutableValue builder */ public static ImmutableValue.Builder builder() { return new ImmutableValue.Builder(); }
/** * Creates a builder for {@link ImmutableTxError ImmutableTxError}. * @return A new ImmutableTxError builder */ public static ImmutableTxError.Builder builder() { return new ImmutableTxError.Builder(); }
/** * Creates a builder for {@link ImmutableOperation ImmutableOperation}. * @return A new ImmutableOperation builder */ public static ImmutableOperation.Builder builder() { return new ImmutableOperation.Builder(); }
/** * Builds a new {@link ImmutableTxError ImmutableTxError}. * @return An immutable instance of TxError * @throws java.lang.IllegalStateException if any required attributes are missing */ public ImmutableTxError build() { return new ImmutableTxError(opIndex, what); } }
/** * Creates a builder for {@link ImmutableTxResponse ImmutableTxResponse}. * @return A new ImmutableTxResponse builder */ public static ImmutableTxResponse.Builder builder() { return new ImmutableTxResponse.Builder(); }
/** * This instance is equal to all instances of {@code ImmutableTxResponse} that have equal attribute values. * @return {@code true} if {@code this} is equal to {@code another} instance */ @Override public boolean equals(@Nullable Object another) { if (this == another) return true; return another instanceof ImmutableTxResponse && equalTo((ImmutableTxResponse) another); }
/** * Copy the current immutable object by setting a value for the {@link Value#getCreateIndex() createIndex} attribute. * A value equality check is used to prevent copying of the same value by returning {@code this}. * @param value A new value for createIndex * @return A modified copy of the {@code this} object */ public final ImmutableValue withCreateIndex(long value) { if (this.createIndex == value) return this; return new ImmutableValue(value, this.modifyIndex, this.lockIndex, this.key, this.flags, this.value, this.session); }
/** * Copy the current immutable object by setting a value for the {@link Value#getLockIndex() lockIndex} attribute. * A value equality check is used to prevent copying of the same value by returning {@code this}. * @param value A new value for lockIndex * @return A modified copy of the {@code this} object */ public final ImmutableValue withLockIndex(long value) { if (this.lockIndex == value) return this; return new ImmutableValue(this.createIndex, this.modifyIndex, value, this.key, this.flags, this.value, this.session); }
/** * Copy the current immutable object by setting a value for the {@link Value#getFlags() flags} attribute. * A value equality check is used to prevent copying of the same value by returning {@code this}. * @param value A new value for flags * @return A modified copy of the {@code this} object */ public final ImmutableValue withFlags(long value) { if (this.flags == value) return this; return new ImmutableValue(this.createIndex, this.modifyIndex, this.lockIndex, this.key, value, this.value, this.session); }