/** * 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(); }
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(); }
/** * 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(); }
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; } } }
ImmutableQueryOptions.builder() .index(index) .wait(waitSeconds + "s")
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(); } }
@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; } } }
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(); }
protected static QueryOptions watchParams(final BigInteger index, final int blockSeconds, QueryOptions queryOptions) { checkArgument(!queryOptions.getIndex().isPresent() && !queryOptions.getWait().isPresent(), "Index and wait cannot be overridden"); ImmutableQueryOptions.Builder builder = ImmutableQueryOptions.builder() .from(watchDefaultParams(index, blockSeconds)) .token(queryOptions.getToken()) .consistencyMode(queryOptions.getConsistencyMode()) .near(queryOptions.getNear()) .datacenter(queryOptions.getDatacenter()); for (String tag : queryOptions.getTag()) { builder.addTag(tag); } return builder.build(); }
public ConsulServiceDiscovery(ConsulConfiguration configuration) throws Exception { this.client = Suppliers.memorize( () -> configuration.createConsulClient(getCamelContext()), e -> ObjectHelper.wrapRuntimeCamelException(e) ); ImmutableQueryOptions.Builder builder = ImmutableQueryOptions.builder(); ObjectHelper.ifNotEmpty(configuration.getDatacenter(), builder::datacenter); ObjectHelper.ifNotEmpty(configuration.getTags(), builder::tag); queryOptions = builder.build(); }