/** * Copy the current immutable object with elements that replace the content of {@link QueryOptions#getNodeMeta() nodeMeta}. * @param elements The elements to set * @return A modified copy of {@code this} object */ public final ImmutableQueryOptions withNodeMeta(String... elements) { ImmutableList<String> newValue = ImmutableList.copyOf(elements); return validate(new ImmutableQueryOptions( this.wait, this.token, this.index, this.near, this.datacenter, newValue, this.tag, this.consistencyMode)); }
/** * This instance is equal to all instances of {@code ImmutableQueryOptions} 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 ImmutableQueryOptions && equalTo((ImmutableQueryOptions) another); }
/** * Method to build an ACL token in a query option. * * @param token * @return QueryOption */ public static QueryOptions getAclToken(String token){ if(token == null || token.trim().isEmpty()){ return ImmutableQueryOptions.BLANK; } //ACL token for registering as a service on consul, check health and get service catalog ImmutableQueryOptions.Builder optionBuilder = ImmutableQueryOptions.builder().token(token); return optionBuilder.build(); }
/** * Builds a new {@link ImmutableQueryOptions ImmutableQueryOptions}. * @return An immutable instance of QueryOptions * @throws java.lang.IllegalStateException if any required attributes are missing */ public ImmutableQueryOptions build() { return ImmutableQueryOptions.validate(new ImmutableQueryOptions(this)); } }
private static ImmutableQueryOptions validate(ImmutableQueryOptions instance) { instance.validate(); return instance; }
private static ImmutableQueryOptions.Builder blockBuilder(String identifier, int qty, BigInteger index) { return ImmutableQueryOptions.builder() .wait(String.format("%s%s", qty, identifier)) .index(index); }
/** * Method to build an ACL token in a query option. * * @param token * @return QueryOption */ public static QueryOptions getAclToken(String token){ if(token == null || token.trim().isEmpty()){ return ImmutableQueryOptions.BLANK; } //ACL token for registering as a service on consul, check health and get service catalog ImmutableQueryOptions.Builder optionBuilder = ImmutableQueryOptions.builder().token(token); return optionBuilder.build(); }
/** * Copy the current immutable object by setting a value for the {@link QueryOptions#getConsistencyMode() consistencyMode} attribute. * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}. * @param value A new value for consistencyMode * @return A modified copy of the {@code this} object */ public final ImmutableQueryOptions withConsistencyMode(ConsistencyMode value) { if (this.consistencyMode == value) return this; return validate(new ImmutableQueryOptions( this.wait, this.token, this.index, this.near, this.datacenter, this.nodeMeta, this.tag, Preconditions.checkNotNull(value, "consistencyMode"))); }
/** * Creates an immutable copy of a {@link QueryOptions} 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 QueryOptions instance */ public static ImmutableQueryOptions copyOf(QueryOptions instance) { if (instance instanceof ImmutableQueryOptions) { return (ImmutableQueryOptions) instance; } return ImmutableQueryOptions.builder() .from(instance) .build(); }
/** * Copy the current immutable object with elements that replace the content of {@link QueryOptions#getTag() tag}. * @param elements The elements to set * @return A modified copy of {@code this} object */ public final ImmutableQueryOptions withTag(String... elements) { ImmutableList<String> newValue = ImmutableList.copyOf(elements); return validate(new ImmutableQueryOptions( this.wait, this.token, this.index, this.near, this.datacenter, this.nodeMeta, newValue, this.consistencyMode)); }
public static ConsulResponse<Value> awaitChange(KeyValueClient kv, String key, BigInteger index) { while (true) { final ConsulResponse<Value> response = kv.getConsulResponseWithValue(key, ImmutableQueryOptions.builder() .index(index) .wait("5m") .build()) .orElse(null); if (response == null) { LOGGER.debug("Document does not exist: {}", key); return null; } if (index.equals(response.getIndex())) { LOGGER.debug("Long poll timed out, polling again for {}", key); } else { return response; } } }
/** * Copy the current immutable object with elements that replace the content of {@link QueryOptions#getNodeMeta() nodeMeta}. * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}. * @param elements An iterable of nodeMeta elements to set * @return A modified copy of {@code this} object */ public final ImmutableQueryOptions withNodeMeta(Iterable<String> elements) { if (this.nodeMeta == elements) return this; ImmutableList<String> newValue = ImmutableList.copyOf(elements); return validate(new ImmutableQueryOptions( this.wait, this.token, this.index, this.near, this.datacenter, newValue, this.tag, this.consistencyMode)); }
ImmutableQueryOptions.builder() .index(index) .wait(waitSeconds + "s")
/** * Copy the current immutable object with elements that replace the content of {@link QueryOptions#getTag() tag}. * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}. * @param elements An iterable of tag elements to set * @return A modified copy of {@code this} object */ public final ImmutableQueryOptions withTag(Iterable<String> elements) { if (this.tag == elements) return this; ImmutableList<String> newValue = ImmutableList.copyOf(elements); return validate(new ImmutableQueryOptions( this.wait, this.token, this.index, this.near, this.datacenter, this.nodeMeta, newValue, this.consistencyMode)); }
public static void awaitRemoval(KeyValueClient kv, String key) { BigInteger index = BigInteger.ZERO; while (true) { final ConsulResponse<Value> response = kv.getConsulResponseWithValue(key, ImmutableQueryOptions.builder() .index(index) .wait("5m") .build()) .orElse(null); if (response == null) { return; } index = response.getIndex(); } }
/** * Copy the current immutable object by setting a <i>present</i> value for the optional {@link QueryOptions#getIndex() index} attribute. * @param value The value for index * @return A modified copy of {@code this} object */ public final ImmutableQueryOptions withIndex(BigInteger value) { @Nullable BigInteger newValue = Preconditions.checkNotNull(value, "index"); if (this.index == newValue) return this; return validate(new ImmutableQueryOptions( this.wait, this.token, newValue, this.near, this.datacenter, this.nodeMeta, this.tag, this.consistencyMode)); }
@Override public void run() { CatalogClient client = this.catalogClientInjector.getValue(); BigInteger index = null; while (true) { QueryOptions options = QueryOptions.BLANK; if (index != null) { options = ImmutableQueryOptions.builder() .wait("60s") .index(index) .build(); } ConsulResponse<Map<String, List<String>>> services = client.getServices(options); index = services.getIndex(); Map<String, List<String>> response = services.getResponse(); response.keySet().forEach(e -> { setupWatcher(e); }); try { Thread.sleep(1000); } catch (InterruptedException e) { break; } } }
/** * Copy the current immutable object by setting an optional value for the {@link QueryOptions#getIndex() index} attribute. * A shallow reference equality check is used on unboxed optional value to prevent copying of the same value by returning {@code this}. * @param optional A value for index * @return A modified copy of {@code this} object */ public final ImmutableQueryOptions withIndex(Optional<BigInteger> optional) { @Nullable BigInteger value = optional.orElse(null); if (this.index == value) return this; return validate(new ImmutableQueryOptions( this.wait, this.token, value, this.near, this.datacenter, this.nodeMeta, this.tag, this.consistencyMode)); }
protected QueryOptions buildQueryOptions(Message message, ConsulConfiguration conf) { ImmutableQueryOptions.Builder builder = ImmutableQueryOptions.builder(); ObjectHelper.ifNotEmpty( message.getHeader(ConsulConstants.CONSUL_INDEX, BigInteger.class), builder::index); ObjectHelper.ifNotEmpty( message.getHeader(ConsulConstants.CONSUL_WAIT, String.class), builder::wait); ObjectHelper.ifNotEmpty( message.getHeader(ConsulConstants.CONSUL_DATACENTER, conf.getDatacenter(), String.class), builder::datacenter); ObjectHelper.ifNotEmpty( message.getHeader(ConsulConstants.CONSUL_NEAR_NODE, conf.getNearNode(), String.class), builder::near); ObjectHelper.ifNotEmpty( conf.getAclToken(), builder::token); ObjectHelper.ifNotEmpty( message.getHeader(ConsulConstants.CONSUL_CONSISTENCY_MODE, conf.getConsistencyMode(), ConsistencyMode.class), builder::consistencyMode); ObjectHelper.ifNotEmpty( message.getHeader(ConsulConstants.CONSUL_NODE_META, conf.getNodeMeta(), List.class), builder::nodeMeta); return builder.build(); }
/** * Copy the current immutable object by setting a <i>present</i> value for the optional {@link QueryOptions#getToken() token} attribute. * @param value The value for token * @return A modified copy of {@code this} object */ public final ImmutableQueryOptions withToken(String value) { @Nullable String newValue = Preconditions.checkNotNull(value, "token"); if (Objects.equals(this.token, newValue)) return this; return validate(new ImmutableQueryOptions( this.wait, newValue, this.index, this.near, this.datacenter, this.nodeMeta, this.tag, this.consistencyMode)); }