本文整理汇总了Java中mousio.etcd4j.responses.EtcdKeysResponse类的典型用法代码示例。如果您正苦于以下问题:Java EtcdKeysResponse类的具体用法?Java EtcdKeysResponse怎么用?Java EtcdKeysResponse使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
EtcdKeysResponse类属于mousio.etcd4j.responses包,在下文中一共展示了EtcdKeysResponse类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readDir
import mousio.etcd4j.responses.EtcdKeysResponse; //导入依赖的package包/类
public ConcurrentMap<String, String> readDir(
final String path,
final boolean shiftted) {
return Fn.getJvm(new ConcurrentHashMap<>(), () -> {
final EtcdKeysResponse.EtcdNode node = readNode(path, this.client::getDir);
return Fn.getJvm(new ConcurrentHashMap<>(), () -> {
final ConcurrentMap<String, String> result = new ConcurrentHashMap<>();
/** Nodes **/
final List<EtcdKeysResponse.EtcdNode> nodes = node.getNodes();
for (final EtcdKeysResponse.EtcdNode nodeItem : nodes) {
String key = nodeItem.getKey();
if (shiftted) {
key = key.substring(key.lastIndexOf(Strings.SLASH) + Values.ONE);
}
result.put(key, nodeItem.getValue());
}
return result;
}, node);
}, path);
}
示例2: write
import mousio.etcd4j.responses.EtcdKeysResponse; //导入依赖的package包/类
public <T> JsonObject write(final String path, final T data, final int ttl) {
return Fn.getJvm(null, () -> {
final EtcdKeyPutRequest request = this.client.put(path,
Fn.getSemi(data instanceof JsonObject || data instanceof JsonArray,
LOGGER,
() -> Instance.invoke(data, "encode"),
data::toString));
if (Values.ZERO != ttl) {
request.ttl(ttl);
}
/** Timeout **/
if (-1 != this.timeout) {
request.timeout(this.timeout, TimeUnit.SECONDS);
}
final EtcdResponsePromise<EtcdKeysResponse> promise = request.send();
final EtcdKeysResponse response = promise.get();
return Jackson.serializeJson(response.getNode());
}, path, data);
}
示例3: handleEtcdWatchErrors
import mousio.etcd4j.responses.EtcdKeysResponse; //导入依赖的package包/类
/**
* Handles watch issues with watching. See Etcd docs:
*
* @see <a href="https://github.com/coreos/etcd/blob/master/Documentation/api.md">Docs</a>
*/
private EtcdKeysResponse handleEtcdWatchErrors(EtcdException exception) {
//
if (exception.errorCode == ETCD_EVENT_INDEX_CLEARED_CODE) {
// If our watching failed due to index, re-read everything because we might have missed
// something. The lastKnownFlagzModificationIndex will be reset in fetchAllFlagzNodes.
initialSetAllFlagz();
return null;
} else if (exception.errorCode == ETCD_WATCHER_CLEARED_CODE) {
// This means that etcd is recovering from a problem.
try {
Thread.sleep(reelectionBackoffMs.get());
} catch (InterruptedException e1) {
// ignore
}
return null;
} else {
throw new EtcdFlagFieldUpdaterException.EtcdFetchingFailed(exception);
}
}
示例4: tryTakeLeadership
import mousio.etcd4j.responses.EtcdKeysResponse; //导入依赖的package包/类
private boolean tryTakeLeadership() throws Exception {
boolean result = false;
try {
EtcdKeysResponse response = getClient()
.put(servicePath, serviceName)
.prevExist(false)
.ttl(ttl)
.send()
.get();
result = ObjectHelper.equal(serviceName, response.node.value);
setIndex(response);
} catch (EtcdException e) {
if (!e.isErrorCode(EtcdErrorCode.NodeExist)) {
throw e;
}
}
return result;
}
示例5: getAsJson
import mousio.etcd4j.responses.EtcdKeysResponse; //导入依赖的package包/类
/**
* Gets the content of the key recursively as a JsonObject
* @param path root path (i.e. /path1/path2)
* @param etcdClient EtcdClient
* @return JsonNode
*/
public static JsonNode getAsJson(String path, EtcdClient etcdClient)
throws IOException, EtcdAuthenticationException, TimeoutException, EtcdException {
EtcdKeyGetRequest etcdKeyGetRequest = etcdClient.get(path).recursive();
EtcdKeysResponse dataTree = etcdKeyGetRequest.send().get();
ObjectNode jNode = JsonNodeFactory.instance.objectNode();
if (dataTree.getNode().getNodes().isEmpty()) {
iterateOverNodes(jNode, dataTree.getNode());
} else {
for (EtcdNode node : dataTree.getNode().getNodes()) {
iterateOverNodes(jNode, node);
}
}
return dotNotationToStandardJson(jNode.at(path));
}
示例6: testSuccessWithoutRetrying
import mousio.etcd4j.responses.EtcdKeysResponse; //导入依赖的package包/类
@Test
public void testSuccessWithoutRetrying() throws Exception {
whenHttp(server)
.match(get("/v2/keys/foo"))
.then(SUCCESS);
try (EtcdClient etcd = new EtcdClient(serverURI)) {
EtcdResponsePromise<EtcdKeysResponse> promise = etcd.get("foo")
.setRetryPolicy(new RetryNTimes(1, 10))
.send();
EtcdKeysResponse resp = promise.get();
assertThat(resp.node.value).isEqualTo("bar");
assertThat(promise.getException()).isNull();
}
verifyHttp(server).once(
method(Method.GET),
uri("/v2/keys/foo")
);
}
示例7: testSuccessAfterRetrying
import mousio.etcd4j.responses.EtcdKeysResponse; //导入依赖的package包/类
@Test
public void testSuccessAfterRetrying() throws Exception {
whenHttp(server)
.match(get("/v2/keys/foo"))
.then(sequence(FAILURE, SUCCESS));
try (EtcdClient etcd = new EtcdClient(serverURI)) {
EtcdResponsePromise<EtcdKeysResponse> promise = etcd.get("foo")
.setRetryPolicy(new RetryNTimes(1, 10))
.send();
EtcdKeysResponse resp = promise.get();
assertThat(resp.node.value).isEqualTo("bar");
assertThat(promise.getException()).isNull();
}
verifyHttp(server).times(2,
method(Method.GET),
uri("/v2/keys/foo")
);
}
示例8: testDir
import mousio.etcd4j.responses.EtcdKeysResponse; //导入依赖的package包/类
/**
* Directory tests
*/
@Test
public void testDir() throws IOException, EtcdException, EtcdAuthenticationException, TimeoutException {
EtcdKeysResponse r = etcd.putDir("etcd4j_test/foo_dir").send().get();
assertEquals(r.action, EtcdKeyAction.set);
r = etcd.getDir("etcd4j_test/foo_dir").consistent().send().get();
assertEquals(r.action, EtcdKeyAction.get);
// Test slash before key
r = etcd.getDir("/etcd4j_test/foo_dir").send().get();
assertEquals(r.action, EtcdKeyAction.get);
r = etcd.put("etcd4j_test/foo_dir/foo", "bar").send().get();
assertEquals(r.node.value, "bar");
r = etcd.putDir("etcd4j_test/foo_dir/foo_subdir").ttl(20).send().get();
assertEquals(r.action, EtcdKeyAction.set);
assertNotNull(r.node.expiration);
r = etcd.deleteDir("etcd4j_test/foo_dir").recursive().send().get();
assertEquals(r.action, EtcdKeyAction.delete);
}
示例9: testRecursive
import mousio.etcd4j.responses.EtcdKeysResponse; //导入依赖的package包/类
/**
* Recursive
*/
@Test
public void testRecursive() throws IOException, EtcdException, EtcdAuthenticationException, TimeoutException {
etcd.put("etcd4j_test/nested/root/key-1", "key1").send().get();
etcd.put("etcd4j_test/nested/root/node-1/key-2", "key2").send().get();
etcd.put("etcd4j_test/nested/root/node-1/child/key-3", "key3").send().get();
etcd.put("etcd4j_test/nested/root/node-2/key-4", "key4").send().get();
EtcdKeysResponse r;
r = etcd.get("etcd4j_test/nested").recursive().timeout(10, TimeUnit.SECONDS).send().get();
assertEquals(1, r.node.nodes.size());
assertEquals(3, r.node.nodes.get(0).nodes.size());
r = etcd.getDir("etcd4j_test/nested").recursive().timeout(10, TimeUnit.SECONDS).send().get();
assertEquals(1, r.node.nodes.size());
assertEquals(3, r.node.nodes.get(0).nodes.size());
r = etcd.deleteDir("etcd4j_test/nested").recursive().send().get();
assertEquals(r.action, EtcdKeyAction.delete);
}
示例10: testInOrderKeys
import mousio.etcd4j.responses.EtcdKeysResponse; //导入依赖的package包/类
/**
* In order key tests
*/
@Test
public void testInOrderKeys() throws IOException, EtcdException, EtcdAuthenticationException, TimeoutException {
EtcdKeysResponse r = etcd.post("etcd4j_test/queue", "Job1").send().get();
assertEquals(r.action, EtcdKeyAction.create);
r = etcd.post("etcd4j_test/queue", "Job2").ttl(20).send().get();
assertEquals(r.action, EtcdKeyAction.create);
r = etcd.get(r.node.key).consistent().send().get();
assertTrue(r.node.key.endsWith(r.node.createdIndex+""));
assertEquals(r.node.value, "Job2");
r = etcd.get("etcd4j_test/queue").consistent().recursive().sorted().send().get();
assertEquals(2, r.node.nodes.size());
assertEquals("Job2", r.node.nodes.get(1).value);
r = etcd.deleteDir("etcd4j_test/queue").recursive().send().get();
assertEquals(r.action, EtcdKeyAction.delete);
}
示例11: testWait
import mousio.etcd4j.responses.EtcdKeysResponse; //导入依赖的package包/类
/**
* In order key tests
*/
@Test
public void testWait() throws IOException, EtcdException, EtcdAuthenticationException, InterruptedException, TimeoutException {
EtcdResponsePromise<EtcdKeysResponse> p = etcd.get("etcd4j_test/test").waitForChange().send();
// Ensure the change is received after the listen command is received.
new Timer().schedule(new TimerTask() {
@Override
public void run() {
try {
etcd.put("etcd4j_test/test", "changed").send().get();
} catch (IOException | EtcdException | EtcdAuthenticationException | TimeoutException e) {
fail();
}
}
}, 20);
EtcdKeysResponse r = p.get();
assertEquals("changed", r.node.value);
}
示例12: testIfCleanClose
import mousio.etcd4j.responses.EtcdKeysResponse; //导入依赖的package包/类
@Test
public void testIfCleanClose() throws IOException, EtcdException, EtcdAuthenticationException, TimeoutException {
EtcdClient client = new EtcdClient();
client.setRetryHandler(new RetryWithExponentialBackOff(20, 4, 1000));
EtcdResponsePromise<EtcdKeysResponse> p = client.get("etcd4j_test/test").waitForChange().send();
client.close();
try {
p.get();
fail();
} catch (IOException e){
// should be catched because connection was canceled
if (!(e.getCause() instanceof CancellationException)) {
fail();
}
}
}
示例13: testGetAll
import mousio.etcd4j.responses.EtcdKeysResponse; //导入依赖的package包/类
@Test
public void testGetAll() throws IOException, EtcdException, EtcdAuthenticationException, TimeoutException {
EtcdKeysResponse.EtcdNode root;
List<EtcdKeysResponse.EtcdNode> nodes;
EtcdClient client = new EtcdClient();
root = client.getAll().timeout(30, TimeUnit.SECONDS).send().get().getNode();
nodes = root.getNodes();
LOGGER.info("Nodes (1) {}", nodes);
assertNotNull(nodes);
assertTrue(root.isDir());
client.put("etcd4j_testGetAll_1/foo1", "bar").prevExist(false).send().get();
client.put("etcd4j_testGetAll_2/foo1", "bar").prevExist(false).send().get();
root = client.getAll().timeout(30, TimeUnit.SECONDS).send().get().getNode();
nodes = root.getNodes();
LOGGER.info("Nodes (2) {}", nodes);
assertNotNull(nodes);
assertEquals(2, nodes.size());
}
示例14: testGetHugeDir
import mousio.etcd4j.responses.EtcdKeysResponse; //导入依赖的package包/类
@Test
public void testGetHugeDir() throws IOException, EtcdException, EtcdAuthenticationException, TimeoutException {
EtcdNettyConfig config = new EtcdNettyConfig();
config.setMaxFrameSize(1024 * 1024); // Desired max size
EtcdNettyClient nettyClient = new EtcdNettyClient(config, URI.create("http://localhost:4001"));
EtcdClient client = new EtcdClient(nettyClient);
for (int i = 0; i < 2000; i++) {
client.put("/etcd4j_test/huge-dir/node-" + i, "bar").send().get();
}
List<EtcdKeysResponse.EtcdNode> nodes;
nodes = client.getDir("/etcd4j_test/huge-dir/").send().get().getNode().getNodes();
assertNotNull(nodes);
assertEquals(2000, nodes.size());
}
示例15: readNode
import mousio.etcd4j.responses.EtcdKeysResponse; //导入依赖的package包/类
private EtcdKeysResponse.EtcdNode readNode(
final String path,
final Function<String, EtcdKeyGetRequest> executor) {
return Fn.getJvm(null, () -> {
final EtcdKeyGetRequest request = executor.apply(path);
/** Timeout **/
if (-1 != this.timeout) {
request.timeout(this.timeout, TimeUnit.SECONDS);
}
final EtcdResponsePromise<EtcdKeysResponse> promise = request.send();
final EtcdKeysResponse response = promise.get();
return response.getNode();
}, path);
}