@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(); } }); } }
/** * Returns key/value pair that corresponding to the specified key. * An empty {@link io.vertx.ext.consul.KeyValue} object will be returned if no such key is found. * @param key the key * @param resultHandler will be provided with key/value pair * @return reference to this, for fluency */ public io.vertx.rxjava.ext.consul.ConsulClient getValue(String key, Handler<AsyncResult<KeyValue>> resultHandler) { delegate.getValue(key, resultHandler); return this; }
/** * Returns key/value pair that corresponding to the specified key. * An empty {@link io.vertx.ext.consul.KeyValue} object will be returned if no such key is found. * @param key the key * @param resultHandler will be provided with key/value pair * @return reference to this, for fluency */ public io.vertx.rxjava.ext.consul.ConsulClient getValue(String key, Handler<AsyncResult<KeyValue>> resultHandler) { delegate.getValue(key, resultHandler); return this; }
@Override public ConsulService getValue(String key, Handler<AsyncResult<KeyValue>> resultHandler) { consulClient.getValue(key, resultHandler); return this; }
public static io.vertx.ext.consul.ConsulClient getValue(io.vertx.ext.consul.ConsulClient j_receiver, java.lang.String key, io.vertx.core.Handler<io.vertx.core.AsyncResult<java.util.Map<String, Object>>> resultHandler) { io.vertx.core.impl.ConversionHelper.fromObject(j_receiver.getValue(key, resultHandler != null ? new io.vertx.core.Handler<io.vertx.core.AsyncResult<io.vertx.ext.consul.KeyValue>>() { public void handle(io.vertx.core.AsyncResult<io.vertx.ext.consul.KeyValue> ar) { resultHandler.handle(ar.map(event -> event != null ? io.vertx.core.impl.ConversionHelper.fromJsonObject(event.toJson()) : null)); } } : null)); return j_receiver; } public static io.vertx.ext.consul.ConsulClient getValueWithOptions(io.vertx.ext.consul.ConsulClient j_receiver, java.lang.String key, java.util.Map<String, Object> options, io.vertx.core.Handler<io.vertx.core.AsyncResult<java.util.Map<String, Object>>> resultHandler) {
@Test public void handleExceptions() { CountDownLatch latch = new CountDownLatch(2); String unknownKey = randomFooBarAlpha(); ctx.readClient().getValue(unknownKey, kv -> { latch.countDown(); throw new RuntimeException(); }); boolean zero = true; try { zero = latch.await(2, TimeUnit.SECONDS); } catch (Exception ignore) { } assertFalse(zero); }
@Test public void canGetValueBlocking(TestContext tc) { blockingQuery(tc, (key, h) -> ctx.readClient().getValue(key, tc.asyncAssertSuccess(list -> h.handle(list.getModifyIndex())))); }
private void createKV(TestContext tc, String key, String value, Handler<Long> resultHandler) { ctx.writeClient().putValue(key, value, tc.asyncAssertSuccess(b -> { tc.assertTrue(b); ctx.readClient().getValue(key, tc.asyncAssertSuccess(pair -> { resultHandler.handle(pair.getModifyIndex()); })); })); } }
@Test public void keyNotFound(TestContext tc) { String key = randomFooBarUnicode(); ctx.readClient() .getValue(key, tc.asyncAssertSuccess(kv -> { tc.assertFalse(kv.isPresent()); })); }
private void go(TestContext tc, boolean trustAll, PemTrustOptions trustOptions) { ConsulClient secureClient = ctx.createSecureClient(trustAll, trustOptions); secureClient.putValue("foo/bars42", "value42", tc.asyncAssertSuccess(b -> { tc.assertTrue(b); secureClient.getValue("foo/bars42", tc.asyncAssertSuccess(pair -> { tc.assertEquals(pair.getValue(), "value42"); ctx.closeClient(secureClient); })); })); } }
@Test public void readClientEmptyValue(TestContext tc) { String key = randomFooBarAlpha(); ctx.writeClient() .putValue(key, "", tc.asyncAssertSuccess(b -> { tc.assertTrue(b); ctx.readClient().getValue(key, tc.asyncAssertSuccess(pair -> { tc.assertEquals(key, pair.getKey()); tc.assertEquals("", pair.getValue()); 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 readClientCanReadOneValue(TestContext tc) { String key = randomFooBarUnicode(); String value = randomUnicodeString(10); ctx.writeClient() .putValue(key, value, tc.asyncAssertSuccess(b -> { tc.assertTrue(b); ctx.readClient().getValue(key, tc.asyncAssertSuccess(pair -> { tc.assertEquals(key, pair.getKey()); tc.assertEquals(value, pair.getValue()); ctx.writeClient().deleteValue(key, tc.asyncAssertSuccess()); })); })); }
@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 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 checkAndSet(TestContext tc) { String key = randomFooBarAlpha(); ctx.writeClient() .putValue(key, randomAlphaString(10), tc.asyncAssertSuccess(b1 -> { tc.assertTrue(b1); ctx.readClient().getValue(key, tc.asyncAssertSuccess(pair1 -> { ctx.writeClient().putValue(key, randomAlphaString(10), tc.asyncAssertSuccess(b2 -> { tc.assertTrue(b2); ctx.readClient().getValue(key, tc.asyncAssertSuccess(pair2 -> { tc.assertTrue(pair1.getModifyIndex() < pair2.getModifyIndex()); ctx.writeClient().putValueWithOptions(key, randomAlphaString(10), new KeyValueOptions().setCasIndex(pair1.getModifyIndex()), tc.asyncAssertSuccess(b3 -> { tc.assertFalse(b3); ctx.writeClient().putValueWithOptions(key, randomAlphaString(10), new KeyValueOptions().setCasIndex(pair2.getModifyIndex()), tc.asyncAssertSuccess(b4 -> { tc.assertTrue(b4); ctx.writeClient().deleteValue(key, tc.asyncAssertSuccess()); })); })); })); })); })); })); }