Java源码示例:com.orbitz.consul.option.QueryOptions

示例1
private void setupWatcher(String serviceName) {
    if (watchers.containsKey(serviceName)) {
        return;
    }
    QueryOptions options = ImmutableQueryOptions.builder()
            .build();

    ServiceHealthCache healthCache = ServiceHealthCache.newCache(
            this.healthClientInjector.getValue(),
            serviceName,
            true,
            options,
            5
    );

    try {
        healthCache.addListener(new ServiceCacheListener(serviceName, this.topologyManagerInjector.getValue()));
        healthCache.start();
        healthCache.awaitInitialized(1, TimeUnit.SECONDS);
        this.watchers.put(serviceName, healthCache);
    } catch (Exception e) {
        ConsulTopologyMessages.MESSAGES.errorSettingUpCatalogWatcher(serviceName, e);
    }
}
 
示例2
/**
 * 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();
}
 
示例3
@Test
public void blockingCallTest() throws InterruptedException {
    injects();
    keyValueClient.putValue(KEY, VALUE);
    final StringBuilder builder = new StringBuilder();
    final ConsulResponseCallback<Optional<Value>> callback = new ConsulResponseCallback<Optional<Value>>() {
        final AtomicReference<BigInteger> index = new AtomicReference<BigInteger>(null);

        @Override
        public void onComplete(final ConsulResponse<Optional<Value>> consulResponse) {
            if (consulResponse.getResponse().isPresent()) {
                final Value v = consulResponse.getResponse().get();
                builder.setLength(0);
                builder.append(v.getValueAsString().get());
            }

            index.set(consulResponse.getIndex());
            watch();
        }

        void watch() {
            keyValueClient.getValue(KEY, QueryOptions.blockMinutes(5, index.get()).build(), this);
        }

        @Override
        public void onFailure(final Throwable throwable) {
            LOGGER.error("Error encountered", throwable);
            watch();
        }
    };

    keyValueClient.getValue(KEY, QueryOptions.blockMinutes(5, new BigInteger("0")).build(), callback);

    Thread.sleep(1000);

    keyValueClient.putValue(KEY, NEW_VALUE);

    Thread.sleep(500);

    Assert.assertEquals(builder.toString(), NEW_VALUE);

    keyValueClient.deleteKey(KEY);
    final String delValue = keyValueClient.getValueAsString(KEY).or(DELETED);
    Assert.assertEquals(delValue, DELETED);
}