io.vertx.ext.consul.KeyValue.getValue()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(9.8k)|赞(0)|评价(0)|浏览(115)

本文整理了Java中io.vertx.ext.consul.KeyValue.getValue()方法的一些代码示例,展示了KeyValue.getValue()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。KeyValue.getValue()方法的具体详情如下:
包路径:io.vertx.ext.consul.KeyValue
类名称:KeyValue
方法名:getValue

KeyValue.getValue介绍

[英]Get the value. In case if KeyValue is result of transaction, value can be empty
[中]获取值。若KeyValue是事务的结果,则该值可以为空

代码示例

代码示例来源:origin: vert-x3/vertx-examples

@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();
   }
  });
 }
}

代码示例来源:origin: vert-x3/vertx-config

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;
}

代码示例来源:origin: io.vertx/vertx-consul-client

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());
  }
 }
}

代码示例来源:origin: io.vertx/vertx-consul-client

private void getEntries(TestContext tc, String prefix, Handler<List<String>> resultHandler) {
 ctx.readClient().getValues(prefix, tc.asyncAssertSuccess(list -> {
  resultHandler.handle(list.getList().stream()
   .map(kv -> kv.getKey() + "/" + kv.getValue()).collect(Collectors.toList()));
 }));
}

代码示例来源:origin: io.vertx/vertx-consul-client

private void valuesAccess(TestContext tc, ConsulClient accessClient) {
 String key1 = randomFooBarAlpha();
 String value1 = randomAlphaString(10);
 String key2 = randomFooBarAlpha();
 String value2 = randomAlphaString(10);
 ctx.writeClient().putValue(key1, value1, tc.asyncAssertSuccess(b1 -> {
  tc.assertTrue(b1);
  ctx.writeClient().putValue(key2, value2, tc.asyncAssertSuccess(b2 -> {
   tc.assertTrue(b2);
   accessClient.getValues("foo/bar", tc.asyncAssertSuccess(kvList -> {
    List<KeyValue> list = kvList.getList();
    tc.assertEquals(list.size(), 2);
    tc.assertTrue(list.stream()
     .filter(kv -> kv.getKey().equals(key1) && kv.getValue().equals(value1))
     .count() == 1);
    tc.assertTrue(list.stream()
     .filter(kv -> kv.getKey().equals(key2) && kv.getValue().equals(value2))
     .count() == 1);
    ctx.writeClient().deleteValues("foo/bar", tc.asyncAssertSuccess());
   }));
  }));
 }));
}

代码示例来源:origin: io.vertx/vertx-consul-client

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);
   }));
  }));
 }
}

代码示例来源:origin: io.vertx/vertx-consul-client

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());
}

代码示例来源:origin: io.vertx/vertx-consul-client

@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());
   }));
  }));
}

代码示例来源:origin: io.vertx/vertx-consul-client

@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());
   }));
  }));
}

代码示例来源:origin: io.vertx/vertx-consul-client

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()));
    });
   }));
 }
}

代码示例来源:origin: io.vertx/vertx-consul-client

@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());
   }));
  }));
}

代码示例来源:origin: io.vertx/vertx-consul-client

@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());
   }));
  }));
}

代码示例来源:origin: io.vertx/vertx-consul-client

@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();
}

代码示例来源:origin: io.vertx/vertx-consul-client

@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();
}

代码示例来源:origin: io.vertx/vertx-consul-client

@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());
      }));
     }));
    }));
   }));
  }));
 }
}

相关文章