Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2018-2019 Roman Levytskyi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.vertx.spi.cluster.consul.impl;
import io.vertx.core.CompositeFuture;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.VertxException;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.ext.consul.KeyValue;
import io.vertx.ext.consul.KeyValueList;
import io.vertx.ext.consul.KeyValueOptions;
import io.vertx.ext.consul.SessionBehavior;
import io.vertx.ext.consul.SessionOptions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import static io.vertx.core.Future.succeededFuture;
import static io.vertx.spi.cluster.consul.impl.ConversationUtils.asFutureConsulEntry;
import static io.vertx.spi.cluster.consul.impl.ConversationUtils.asFutureString;
/**
* Abstract map functionality for clustering maps.
*
* @author Roman Levytskyi
*/
public abstract class ConsulMap extends ConsulMapListener {
private static final Logger log = LoggerFactory.getLogger(ConsulMap.class);
protected ConsulMap(String name, ClusterManagerInternalContext appContext) {
super(name, appContext);
}
/**
* Puts an entry to Consul KV store.
*
* @param k - holds the key of an entry.
* @param v - holds the value of an entry.
* @return {@link Future}} containing result.
*/
Future putValue(K k, V v) {
return putValue(k, v, null);
}
/**
* Puts an entry to Consul KV store by taking into account additional options
* (these options are mainly used to make an entry ephemeral or to place TTL on an entry).
*
* @param k - holds the key of an entry.
* @param v - holds the value of an entry.
* @param keyValueOptions - holds kv options (note: null is allowed)
* @return {@link Future}} containing result.
*/
Future putValue(K k, V v, KeyValueOptions keyValueOptions) {
return assertKeyAndValueAreNotNull(k, v)
.compose(aVoid -> asFutureString(k, v, appContext.getNodeId()))
.compose(value -> putPlainValue(keyPath(k), value, keyValueOptions));
}
/**
* Puts plain entry {@link String key} and {@link String value} to Consul KV store.
*
* @param key - holds the consul key of an entry.
* @param value - holds the consul value (should be marshaled) of an entry.
* @param keyValueOptions - holds kv options (note: null is allowed)
* @return {@link Future}} containing result.
*/
protected Future putPlainValue(String key, String value, KeyValueOptions keyValueOptions) {
Promise promise = Promise.promise();
appContext.getConsulClient().putValueWithOptions(key, value, keyValueOptions, resultHandler -> {
if (resultHandler.succeeded()) {
if (log.isTraceEnabled()) {
String traceMessage = "[" + appContext.getNodeId() + "] " + key + " put is " + resultHandler.result();
if (keyValueOptions != null) {
log.trace(traceMessage + " with : " + keyValueOptions.getAcquireSession());
} else {
log.trace(traceMessage);
}
}
promise.complete(resultHandler.result());
} else {
log.error("[" + appContext.getNodeId() + "]" + " - Failed to put " + key + " -> " + value, resultHandler.cause());
promise.fail(resultHandler.cause());
}
});
return promise.future();
}
/**
* Gets the value by key.
*
* @param k - holds the key.
* @return @return {@link Future}} containing result.
*/
Future getValue(K k) {
return assertKeyIsNotNull(k)
.compose(aVoid -> getPlainValue(keyPath(k)))
.compose(consulValue -> asFutureConsulEntry(consulValue.getValue()))
.compose(consulEntry -> consulEntry == null ? succeededFuture() : succeededFuture((V) consulEntry.getValue()));
}
/**
* Gets the plain {@link String} value by plain {@link String} key.
*
* @param consulKey - holds the consul key.
* @return @return {@link Future}} containing result.
*/
Future getPlainValue(String consulKey) {
Promise promise = Promise.promise();
appContext.getConsulClient().getValue(consulKey, resultHandler -> {
if (resultHandler.succeeded()) {
// note: resultHandler.result().getValue() is null if nothing was found.
if (log.isTraceEnabled()) {
log.trace("[" + appContext.getNodeId() + "]" + " - Entry is found : " + resultHandler.result().getValue() + " by key: " + consulKey);
}
promise.complete(resultHandler.result());
} else {
log.error("[" + appContext.getNodeId() + "]" + " - Failed to look up an entry by: " + consulKey, resultHandler.cause());
promise.fail(resultHandler.cause());
}
});
return promise.future();
}
/**
* Gets all map's entries.
*
* @return @return {@link Future}} containing result.
*/
Future