use of mousio.etcd4j.responses.EtcdKeysResponse in project camel by apache.
the class EtcdWatchConsumer method onResponse.
@Override
public void onResponse(ResponsePromise<EtcdKeysResponse> promise) {
if (!isRunAllowed()) {
return;
}
Exchange exchange = null;
Throwable throwable = promise.getException();
if (throwable != null && throwable instanceof EtcdException) {
EtcdException exception = (EtcdException) throwable;
// So we set the index to the one returned by the exception + 1
if (EtcdHelper.isOutdatedIndexException(exception)) {
LOGGER.debug("Outdated index, key: {}, cause={}", getPath(), exception.etcdCause);
// We set the index to the one returned by the exception + 1.
index.set(exception.index + 1);
// Clean-up the exception so it is not rethrown/handled
throwable = null;
}
} else {
try {
EtcdKeysResponse response = promise.get();
exchange = endpoint.createExchange();
exchange.getIn().setHeader(EtcdConstants.ETCD_NAMESPACE, getNamespace());
exchange.getIn().setHeader(EtcdConstants.ETCD_PATH, response.node.key);
exchange.getIn().setBody(response);
// Watch from the modifiedIndex + 1 of the node we got for ensuring
// no events are missed between watch commands
index.set(response.node.modifiedIndex + 1);
} catch (TimeoutException e) {
LOGGER.debug("Timeout watching for {}", getPath());
if (configuration.isSendEmptyExchangeOnTimeout()) {
exchange = endpoint.createExchange();
exchange.getIn().setHeader(EtcdConstants.ETCD_NAMESPACE, getNamespace());
exchange.getIn().setHeader(EtcdConstants.ETCD_TIMEOUT, true);
exchange.getIn().setHeader(EtcdConstants.ETCD_PATH, getPath());
exchange.getIn().setBody(null);
}
throwable = null;
} catch (Exception e1) {
throwable = e1;
}
if (exchange != null) {
try {
throwable = null;
getProcessor().process(exchange);
} catch (Exception e) {
getExceptionHandler().handleException("Error processing exchange", exchange, e);
}
}
}
if (throwable != null) {
handleException("Error processing etcd response", throwable);
}
try {
watch();
} catch (Exception e) {
handleException("Error watching key " + getPath(), e);
}
}
use of mousio.etcd4j.responses.EtcdKeysResponse in project camel by apache.
the class EtcdKeysTest method testKeys.
@Test(expected = EtcdException.class)
public void testKeys() throws Exception {
final String path = "/camel/" + UUID.randomUUID().toString();
final String value = UUID.randomUUID().toString();
final EtcdClient client = getClient();
final Map<String, Object> headers = new HashMap<>();
// *******************************************
// SET
// *******************************************
headers.clear();
headers.put(EtcdConstants.ETCD_ACTION, EtcdConstants.ETCD_KEYS_ACTION_SET);
headers.put(EtcdConstants.ETCD_PATH, path);
sendBody("direct:keys-set", value, headers);
MockEndpoint mockSet = getMockEndpoint("mock:result-set");
mockSet.expectedMinimumMessageCount(1);
mockSet.expectedHeaderReceived(EtcdConstants.ETCD_NAMESPACE, EtcdNamespace.keys.name());
mockSet.expectedHeaderReceived(EtcdConstants.ETCD_PATH, path);
mockSet.assertIsSatisfied();
// *******************************************
// GET
// *******************************************
headers.clear();
headers.put(EtcdConstants.ETCD_ACTION, EtcdConstants.ETCD_KEYS_ACTION_GET);
headers.put(EtcdConstants.ETCD_PATH, path);
sendBody("direct:keys-get", value, headers);
MockEndpoint mockGet = getMockEndpoint("mock:result-get");
mockGet.expectedMinimumMessageCount(1);
mockSet.expectedHeaderReceived(EtcdConstants.ETCD_NAMESPACE, EtcdNamespace.keys.name());
mockGet.expectedHeaderReceived(EtcdConstants.ETCD_PATH, path);
mockGet.expectedMessagesMatches(new Predicate() {
@Override
public boolean matches(Exchange exchange) {
EtcdKeysResponse keysResponse = exchange.getIn().getBody(EtcdKeysResponse.class);
assertNotNull(keysResponse);
assertNotNull(keysResponse.node);
assertNotNull(keysResponse.node.value);
return keysResponse.node.value.equals(value);
}
});
mockGet.assertIsSatisfied();
// *******************************************
// DELETE
// *******************************************
headers.clear();
headers.put(EtcdConstants.ETCD_ACTION, EtcdConstants.ETCD_KEYS_ACTION_DELETE);
headers.put(EtcdConstants.ETCD_PATH, path);
sendBody("direct:keys-del", "value", headers);
MockEndpoint mockDel = getMockEndpoint("mock:result-del");
mockDel.expectedMinimumMessageCount(1);
mockSet.expectedHeaderReceived(EtcdConstants.ETCD_NAMESPACE, EtcdNamespace.keys.name());
mockDel.expectedHeaderReceived(EtcdConstants.ETCD_PATH, path);
mockDel.assertIsSatisfied();
// *******************************************
// VALIDATION
// *******************************************
client.get(path).send().get();
fail("EtcdException should have been thrown");
}
use of mousio.etcd4j.responses.EtcdKeysResponse in project camel by apache.
the class EtcdServiceDiscovery method getServices.
protected List<ServiceDefinition> getServices(Predicate<EtcdServiceDefinition> filter) {
List<ServiceDefinition> servers = Collections.emptyList();
if (isRunAllowed()) {
try {
final EtcdConfiguration conf = getConfiguration();
final EtcdKeyGetRequest request = getClient().get(conf.getServicePath()).recursive();
if (conf.hasTimeout()) {
request.timeout(conf.getTimeout(), TimeUnit.SECONDS);
}
final EtcdKeysResponse response = request.send().get();
if (Objects.nonNull(response.node) && !response.node.nodes.isEmpty()) {
servers = response.node.nodes.stream().map(node -> node.value).filter(ObjectHelper::isNotEmpty).map(this::nodeFromString).filter(Objects::nonNull).filter(filter).sorted(EtcdServiceDefinition.COMPARATOR).collect(Collectors.toList());
}
} catch (Exception e) {
throw new RuntimeCamelException(e);
}
}
return servers;
}
use of mousio.etcd4j.responses.EtcdKeysResponse in project camel by apache.
the class EtcdWatchServiceDiscovery method onResponse.
// *************************************************************************
// Watch
// *************************************************************************
@Override
public void onResponse(ResponsePromise<EtcdKeysResponse> promise) {
if (!isRunAllowed()) {
return;
}
Throwable throwable = promise.getException();
if (throwable != null && throwable instanceof EtcdException) {
EtcdException exception = (EtcdException) throwable;
if (EtcdHelper.isOutdatedIndexException(exception)) {
LOGGER.debug("Outdated index, key={}, cause={}", servicePath, exception.etcdCause);
index.set(exception.index + 1);
}
} else {
try {
EtcdKeysResponse response = promise.get();
EtcdHelper.setIndex(index, response);
serversRef.set(getServices());
} catch (TimeoutException e) {
LOGGER.debug("Timeout watching for {}", getConfiguration().getServicePath());
throwable = null;
} catch (Exception e) {
throwable = e;
}
}
if (throwable == null) {
watch();
} else {
throw new RuntimeCamelException(throwable);
}
}
use of mousio.etcd4j.responses.EtcdKeysResponse in project camel by apache.
the class EtcdRoutePolicy method onResponse.
// *************************************************************************
// Watch
// *************************************************************************
@Override
public void onResponse(ResponsePromise<EtcdKeysResponse> promise) {
if (!isRunAllowed()) {
return;
}
Throwable throwable = promise.getException();
if (throwable != null && throwable instanceof EtcdException) {
EtcdException exception = (EtcdException) throwable;
if (EtcdHelper.isOutdatedIndexException(exception)) {
LOGGER.debug("Outdated index, key={}, cause={}", servicePath, exception.etcdCause);
index.set(exception.index + 1);
throwable = null;
}
} else {
try {
EtcdKeysResponse response = promise.get();
EtcdHelper.setIndex(index, response);
if (response.node.value == null) {
setLeader(tryTakeLeadership());
} else if (!ObjectHelper.equal(serviceName, response.node.value) && leader.get()) {
// Looks like I've lost leadership
setLeader(false);
}
} catch (TimeoutException e) {
LOGGER.debug("Timeout watching for {}", servicePath);
throwable = null;
} catch (Exception e1) {
throwable = e1;
}
}
if (throwable == null) {
watch();
} else {
throw new RuntimeCamelException(throwable);
}
}
Aggregations