@Override public void start() throws Exception { ConsulClient consulClient = ConsulClient.create(vertx); consulClient.putValue("key11", "value11", putResult -> { if (putResult.succeeded()) { System.out.println("KV pair saved"); consulClient.getValue("key11", getResult -> { if (getResult.succeeded()) { System.out.println("KV pair retrieved"); System.out.println(getResult.result().getValue()); } else { getResult.cause().printStackTrace(); } }); } else { putResult.cause().printStackTrace(); } }); } }
static KeyValue parse(JsonObject json) { return new KeyValue() .setKey(json.getString(KEY_KEY)) .setValue(Utils.decode64(json.getString(VALUE_KEY))) .setSession(json.getString(SESSION_KEY)) .setFlags(json.getLong(FLAGS_KEY, 0L)) .setCreateIndex(json.getLong(CREATE_KEY, 0L)) .setModifyIndex(json.getLong(MODIFY_KEY, 0L)) .setLockIndex(json.getLong(LOCK_KEY, 0L)); } }
public static void toJson(KeyValue obj, java.util.Map<String, Object> json) { json.put("createIndex", obj.getCreateIndex()); json.put("flags", obj.getFlags()); if (obj.getKey() != null) { json.put("key", obj.getKey()); } json.put("lockIndex", obj.getLockIndex()); json.put("modifyIndex", obj.getModifyIndex()); if (obj.getSession() != null) { json.put("session", obj.getSession()); } if (obj.getValue() != null) { json.put("value", obj.getValue()); } } }
private static JsonObject getTree(KeyValueList list, int prefix, String delimiter) { JsonObject tree = new JsonObject(); for (KeyValue keyValue : list.getList()) { if (keyValue.getKey().endsWith(delimiter)) { continue; } JsonObject json = tree; String[] arr = keyValue.getKey().substring(prefix).split(delimiter); for (int i = 0; i < arr.length; i++) { String key = arr[i]; if (i == arr.length - 1) { json.put(key, keyValue.getValue()); } else { JsonObject next = json.getJsonObject(key); if (next == null) { next = new JsonObject(); json.put(key, next); } json = next; } } } return tree; }
@Test public void canSetAllFlags(TestContext tc) { String key = randomFooBarAlpha(); String value = randomAlphaString(10); KeyValueOptions opts = new KeyValueOptions().setFlags(-1); ctx.writeClient() .putValueWithOptions(key, value, opts, tc.asyncAssertSuccess(b -> { tc.assertTrue(b); ctx.readClient().getValue(key, tc.asyncAssertSuccess(pair -> { tc.assertEquals(key, pair.getKey()); tc.assertEquals(value, pair.getValue()); assertEquals(opts.getFlags(), pair.getFlags()); ctx.writeClient().deleteValue(key, tc.asyncAssertSuccess()); })); })); }
@Test public void deleteBehavior(TestContext tc) { ctx.writeClient().createSessionWithOptions(new SessionOptions().setTtl(442).setBehavior(SessionBehavior.DELETE), tc.asyncAssertSuccess(id -> { ctx.writeClient().putValueWithOptions("foo/bar", "value1", new KeyValueOptions().setAcquireSession(id), tc.asyncAssertSuccess(b -> { tc.assertTrue(b); ctx.writeClient().getValue("foo/bar", tc.asyncAssertSuccess(pair -> { tc.assertEquals("value1", pair.getValue()); tc.assertEquals(id, pair.getSession()); ctx.writeClient().destroySession(id, tc.asyncAssertSuccess(v -> { ctx.writeClient().getValue("foo/bar", tc.asyncAssertSuccess(notfound -> { tc.assertFalse(notfound.isPresent()); })); })); })); })); })); } }
@Test public void watchExistingKey() throws InterruptedException { StateConsumer<String> consumer = new StateConsumer<>(); String key = ConsulContext.KEY_RW_PREFIX + randomAlphaString(10); String v1 = randomAlphaString(10); String v2 = randomAlphaString(10); assertTrue(getAsync(h -> ctx.writeClient().putValue(key, v1, h))); Watch<KeyValue> watch = Watch.key(key, vertx, ctx.readClientOptions()) .setHandler(kv -> { if (kv.succeeded()) { consumer.consume(kv.nextResult().isPresent() ? kv.nextResult().getValue() : EMPTY_MESSAGE); } else { consumer.consume(kv.cause().getMessage()); } }) .start(); consumer.await(v1); assertTrue(getAsync(h -> ctx.writeClient().putValue(key, v2, h))); consumer.await(v2); runAsync(h -> ctx.writeClient().deleteValue(key, h)); consumer.await(EMPTY_MESSAGE); consumer.check(); watch.stop(); }
private void blockingQuery(TestContext tc, BiConsumer<String, Handler<Long>> indexSupplier) { String key = randomFooBarAlpha(); String value = randomAlphaString(10); ctx.writeClient() .putValue(key, randomAlphaString(10), tc.asyncAssertSuccess(b1 -> { tc.assertTrue(b1); indexSupplier.accept(key, consulIndex -> { Async async = tc.async(2); vertx.setTimer(TimeUnit.SECONDS.toMillis(2), l -> { ctx.writeClient().putValue(key, value, tc.asyncAssertSuccess(b2 -> { tc.assertTrue(b2); ctx.readClient().getValueWithOptions(key, new BlockingQueryOptions().setIndex(consulIndex), tc.asyncAssertSuccess(kv -> { tc.assertTrue(kv.getModifyIndex() > consulIndex); tc.assertEquals(kv.getValue(), value); async.countDown(); })); ctx.readClient().getValuesWithOptions("foo/bar", new BlockingQueryOptions().setIndex(consulIndex), tc.asyncAssertSuccess(kv -> { tc.assertTrue(kv.getIndex() > consulIndex); tc.assertTrue(kv.getList().size() == 1); async.countDown(); })); })); }); async.handler(v -> ctx.writeClient().deleteValue(key, tc.asyncAssertSuccess())); }); })); } }
private List<String> getKeys(TxnResponse resp) { return resp.getResults().stream() .map(kv -> ((KeyValue) kv).getKey()).collect(Collectors.toList()); }
@Override void wait(long index, Handler<AsyncResult<State<KeyValue>>> handler) { BlockingQueryOptions options = new BlockingQueryOptions().setWait(BLOCKING_WAIT).setIndex(index); consulClient.getValueWithOptions(key, options, h -> handler.handle(h.map(kv -> new State<KeyValue>(kv, kv.getModifyIndex())))); } }
public static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json, KeyValueList obj) { for (java.util.Map.Entry<String, Object> member : json) { switch (member.getKey()) { case "index": if (member.getValue() instanceof Number) { obj.setIndex(((Number)member.getValue()).longValue()); } break; case "list": if (member.getValue() instanceof JsonArray) { java.util.ArrayList<io.vertx.ext.consul.KeyValue> list = new java.util.ArrayList<>(); ((Iterable<Object>)member.getValue()).forEach( item -> { if (item instanceof JsonObject) list.add(new io.vertx.ext.consul.KeyValue((JsonObject)item)); }); obj.setList(list); } break; } } }
case "createIndex": if (member.getValue() instanceof Number) { obj.setCreateIndex(((Number)member.getValue()).longValue()); obj.setFlags(((Number)member.getValue()).longValue()); obj.setKey((String)member.getValue()); obj.setLockIndex(((Number)member.getValue()).longValue()); obj.setModifyIndex(((Number)member.getValue()).longValue()); obj.setSession((String)member.getValue()); obj.setValue((String)member.getValue());
private void checkKeyValue(KeyValue expected, KeyValue actual) { assertEquals(expected, actual); assertEquals(expected.hashCode(), actual.hashCode()); assertEquals(expected.getKey(), actual.getKey()); assertEquals(expected.getValue(), actual.getValue()); assertEquals(expected.getSession(), actual.getSession()); assertEquals(expected.getCreateIndex(), actual.getCreateIndex()); assertEquals(expected.getFlags(), actual.getFlags()); assertEquals(expected.getModifyIndex(), actual.getModifyIndex()); assertEquals(expected.getLockIndex(), actual.getLockIndex()); }
@Test public void writeClientHaveFullAccessToOneValue(TestContext tc) { String key = randomFooBarAlpha(); String value = randomAlphaString(10); KeyValueOptions opts = new KeyValueOptions().setFlags(randomLong()); ctx.writeClient() .putValueWithOptions(key, value, opts, tc.asyncAssertSuccess(b -> { tc.assertTrue(b); ctx.readClient().getValue(key, tc.asyncAssertSuccess(pair -> { tc.assertEquals(key, pair.getKey()); tc.assertEquals(value, pair.getValue()); assertEquals(opts.getFlags(), pair.getFlags()); ctx.writeClient().deleteValue(key, tc.asyncAssertSuccess()); })); })); }
@Test public void watchCreatedKey() throws InterruptedException { StateConsumer<String> consumer = new StateConsumer<>(); String key = ConsulContext.KEY_RW_PREFIX + randomAlphaString(10); String v1 = randomAlphaString(10); String v2 = randomAlphaString(10); Watch<KeyValue> watch = Watch.key(key, vertx, ctx.readClientOptions()) .setHandler(kv -> { if (kv.succeeded()) { consumer.consume(kv.nextResult().isPresent() ? kv.nextResult().getValue() : EMPTY_MESSAGE); } else { consumer.consume(kv.cause().getMessage()); } }) .start(); consumer.await(EMPTY_MESSAGE); assertTrue(getAsync(h -> ctx.writeClient().putValue(key, v1, h))); consumer.await(v1); assertTrue(getAsync(h -> ctx.writeClient().putValue(key, v2, h))); consumer.await(v2); runAsync(h -> ctx.writeClient().deleteValue(key, h)); consumer.await(EMPTY_MESSAGE); consumer.check(); watch.stop(); }
@Test public void canGetValueBlocking(TestContext tc) { blockingQuery(tc, (key, h) -> ctx.readClient().getValue(key, tc.asyncAssertSuccess(list -> h.handle(list.getModifyIndex())))); }
@Override public ConsulClient getValueWithOptions(String key, BlockingQueryOptions options, Handler<AsyncResult<KeyValue>> resultHandler) { request(KV_VALID_CODES, HttpMethod.GET, "/v1/kv/" + urlEncode(key), new Query().put(options), null, resultHandler, resp -> { if (resp.statusCode() == HttpResponseStatus.NOT_FOUND.code()) { return new KeyValue(); } else { return KVParser.parse(resp.bodyAsJsonArray().getJsonObject(0)); } }); return this; }
public static KeyValue randomKeyValue() { return new KeyValue() .setKey(randomAlphaString(10)) .setValue(randomAlphaString(10)) .setSession(randomAlphaString(10)) .setCreateIndex(randomLong()) .setFlags(randomLong()) .setModifyIndex(randomLong()) .setLockIndex(randomLong()); }