mousio.etcd4j.responses.EtcdException Java Examples

The following examples show how to use mousio.etcd4j.responses.EtcdException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EtcdUtil.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the content of the key recursively as a JsonObject
 * @param path root path (i.e. /path1/path2)
 * @param etcdClient EtcdClient
 * @return JsonNode
 * @throws IOException
 * @throws EtcdAuthenticationException
 * @throws TimeoutException
 * @throws EtcdException
 */
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));
}
 
Example #2
Source File: TestFunctionality.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #3
Source File: TestFunctionality.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #4
Source File: TestFunctionality.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #5
Source File: TestFunctionality.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #6
Source File: TestFunctionality.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testIfCleanClose() throws IOException, EtcdException, EtcdAuthenticationException, TimeoutException {
  etcd.setRetryHandler(new RetryWithExponentialBackOff(20, 4, 1000));

  EtcdResponsePromise<EtcdKeysResponse> p = etcd.get("etcd4j_test/test").waitForChange().send();
  etcd.close();

  try {
    p.get();
    fail();
  } catch (IOException e){
    // should be catched because connection was canceled
    if (!(e.getCause() instanceof CancellationException)) {
      fail();
    }
  }
}
 
Example #7
Source File: TestFunctionality.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetAll() throws IOException, EtcdException, EtcdAuthenticationException, TimeoutException {
  EtcdKeysResponse.EtcdNode root;
  List<EtcdKeysResponse.EtcdNode> nodes;

  root = etcd.getAll().timeout(30, TimeUnit.SECONDS).send().get().getNode();
  nodes = root.getNodes();

  LOGGER.info("Nodes (1) {}", nodes);

  assertNotNull(nodes);
  assertTrue(root.isDir());

  etcd.put("etcd4j_testGetAll_1/foo1", "bar").prevExist(false).send().get();
  etcd.put("etcd4j_testGetAll_2/foo1", "bar").prevExist(false).send().get();

  root = etcd.getAll().timeout(30, TimeUnit.SECONDS).send().get().getNode();
  nodes = root.getNodes();

  LOGGER.info("Nodes (2) {}", nodes);

  assertNotNull(nodes);
  assertEquals(2, nodes.size());
}
 
Example #8
Source File: EtcdKeystoreTest.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
@Ignore
@Test
public void testSslCliAgainstRegularEtcd() throws SecurityContextException, URISyntaxException, IOException, EtcdAuthenticationException, TimeoutException, EtcdException {
    expectedEx.expect(IOException.class);
    expectedEx.expectMessage(matchesRegex("DecoderException: io.netty.handler.ssl.NotSslRecordException"));

    EtcdClient etcd = new EtcdClient(SecurityContextBuilder.forKeystoreAndTruststore(
            KEYSTORE_PATH,
            KEYSTORE_PASS,
            TRUSTSTORE_PATH,
            TRUSTSTORE_PASS
    ));

    // expected not to work when using certificates against a non-secured etcd
    etcd.put("/test", "1234").send().get();
}
 
Example #9
Source File: EtcdResponsePromise.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
/**
 * Get the response. (Blocking)
 *
 * Use addListener to fetch the value in a non blocking way.
 *
 * @return the response
 * @throws IOException                  on fail (Will be ReadTimeoutException if timeout occurred)
 * @throws EtcdException                on etcd fail
 * @throws EtcdAuthenticationException  on authentication failure
 * @throws TimeoutException             on Timeout
 */
@Override public T get() throws IOException, EtcdException, EtcdAuthenticationException, TimeoutException {
  waitForPromiseSuccess();

  if (response != null) {
    return response;
  } else {
    if (this.exception instanceof EtcdException) {
      throw (EtcdException) this.exception;
    } if (this.exception instanceof EtcdAuthenticationException) {
      throw (EtcdAuthenticationException) this.exception;
    } else if (this.exception instanceof IOException) {
      throw (IOException) this.exception;
    } else if (this.exception instanceof io.netty.handler.timeout.TimeoutException) {
      throw new TimeoutException();
    } else {
      throw new IOException(this.exception);
    }
  }
}
 
Example #10
Source File: EtcdUtil.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
/**
 * Puts the content of the Json recursively from the specified <i>path</i>
 * @param path root path (i.e. /path1/path2)
 * @param data JsonNode
 * @param etcdClient EtcdClient
 * @throws IOException
 * @throws EtcdAuthenticationException
 * @throws TimeoutException
 * @throws EtcdException
 */
public static void putAsJson(String path, JsonNode data, EtcdClient etcdClient)
        throws IOException, EtcdAuthenticationException, TimeoutException, EtcdException {

  Map<String, Object> flattened = new JsonFlattener(EtcdUtil.jsonToString(data))
          .withFlattenMode(FlattenMode.MONGODB)
          .withSeparator('/')
          .flattenAsMap();

  // clean previous data and replace it with new json structure
  try {
    etcdClient.delete(path).recursive().send().get();
  } catch (EtcdException e) {
    // interrupt always except when the previous key didn't exist
    if (EtcdErrorCode.KeyNotFound != e.errorCode) {
      throw e;
    }
  }

  // iterate over every flattened key and build the structure in etcd
  for (Map.Entry<String, Object> entry : flattened.entrySet()) {
    etcdClient.put(path + "/" + entry.getKey(), String.valueOf(entry.getValue())).send().get();
  }
}
 
Example #11
Source File: EtcdJsonTest.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testRemoveJsonArray() throws IOException, EtcdException, TimeoutException, EtcdAuthenticationException {
    String newJson = "{\n" +
            "   \"widget\":{\n" +
            "      \"text\":{\n" +
            "         \"data\":[\n" +
            "         ]\n" +
            "      }\n" +
            "   }\n" +
            "}";

    JsonNode originalConfig = EtcdUtil.getAsJson("/etcd4j_test", etcd);
    assertEquals(originalConfig.at("/widget/text/data").size(), 2);

    EtcdUtil.putAsJson("/etcd4j_test", EtcdUtil.stringToJson(newJson), etcd);
    JsonNode updatedConfig = EtcdUtil.getAsJson("/etcd4j_test", etcd);
    assertEquals(updatedConfig.at("/widget/text/data").size(), 0);
}
 
Example #12
Source File: EtcdJsonTest.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testRemoveConfigurationObject() throws IOException, EtcdException, TimeoutException, EtcdAuthenticationException {
    String newJson = "{\n" +
            "   \"widget\":{\n" +
            "      \"text\":{\n" +
            "      }\n" +
            "   }\n" +
            "}";

    JsonNode originalConfig = EtcdUtil.getAsJson("/etcd4j_test", etcd);
    assertEquals(originalConfig.at("/widget/text/data").size(), 2);

    EtcdUtil.putAsJson("/etcd4j_test", EtcdUtil.stringToJson(newJson), etcd);
    JsonNode updatedConfig = EtcdUtil.getAsJson("/etcd4j_test", etcd);
    assertEquals(updatedConfig.at("/widget/text").asText(), "{}");
}
 
Example #13
Source File: EtcdJsonTest.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateJsonValues() throws EtcdAuthenticationException, TimeoutException, EtcdException, IOException {
    ObjectMapper mapper = new ObjectMapper();
    File testJson = new File("src/test/resources/test_data.json");
    JsonNode toEtcd = mapper.readTree(testJson);

    assertEquals(etcd.get("/etcd4j_test/widget/image/name").send().get().getNode().getValue(), "sun1");

    // update a fraction of the original json
    ObjectNode imageSubSection = (ObjectNode) toEtcd.at("/widget/image");
    imageSubSection.put("name", "moon");

    // update that fraction in etcd and check
    EtcdUtil.putAsJson("/etcd4j_test/widget/image", imageSubSection, etcd);
    assertEquals(etcd.get("/etcd4j_test/widget/image/name").send().get().getNode().getValue(), "moon");

    // verify the rest of the json is intact
    JsonNode fromEtcd = EtcdUtil.getAsJson("/etcd4j_test", etcd);
    assertEquals(EtcdUtil.jsonToString(fromEtcd.at("/widget/window")),
            EtcdUtil.jsonToString(EtcdUtil.getAsJson("/etcd4j_test/widget/window", etcd)));
    assertEquals(EtcdUtil.jsonToString(fromEtcd.at("/widget/text")),
            EtcdUtil.jsonToString(EtcdUtil.getAsJson("/etcd4j_test/widget/text", etcd)));
}
 
Example #14
Source File: EtcdJsonTest.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateConfigurationArray() throws IOException, EtcdException, TimeoutException, EtcdAuthenticationException {
    String newJson = "[\n" +
            "  \"example-1\",\n" +
            "  \"example-2\",\n" +
            "  \"example-3\",\n" +
            "  \"example-4\"\n" +
            "]\n";

    JsonNode originalConfig = EtcdUtil.getAsJson("/etcd4j_test", etcd);
    assertEquals(originalConfig.at("/widget/text/data").size(), 2);

    EtcdUtil.putAsJson("/etcd4j_test/widget/text/data", EtcdUtil.stringToJson(newJson), etcd);
    JsonNode updatedConfig = EtcdUtil.getAsJson("/etcd4j_test", etcd);
    assertEquals(updatedConfig.at("/widget/text/data").size(), 4);
}
 
Example #15
Source File: EtcdJsonTest.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateConfigurationObject() throws IOException, EtcdException, TimeoutException, EtcdAuthenticationException {
    String newJson = "{ " +
            "      \"debug\": \"off\",\n" +
            "      \"test\": \"value\"" +
            "}";

    JsonNode originalConfig = EtcdUtil.getAsJson("/etcd4j_test", etcd);
    assertEquals(originalConfig.at("/widget").has("test"), false);
    assertEquals(originalConfig.at("/widget/debug").asText(), "on");

    EtcdUtil.putAsJson("/etcd4j_test/widget", EtcdUtil.stringToJson(newJson), etcd);
    JsonNode updatedConfig = EtcdUtil.getAsJson("/etcd4j_test", etcd);
    assertEquals(updatedConfig.at("/widget").has("test"), true);
    assertEquals(updatedConfig.at("/widget/debug").asText(), "off");
    assertEquals(updatedConfig.at("/widget/test").asText(), "value");
}
 
Example #16
Source File: EtcdConfigWatcherRegister.java    From skywalking with Apache License 2.0 6 votes vote down vote up
private void onDataValueChanged(ResponsePromise<EtcdKeysResponse> promise, String dataId) {
    String key = getRealKey(dataId, settings.getGroup());
    try {
        EtcdKeysResponse.EtcdNode node = promise.get().getNode();
        String value = node.getValue();
        if (logger.isInfoEnabled()) {
            logger.info("Etcd config changed: {}: {}", key, node.getValue());
        }

        configItemKeyedByName.put(key, Optional.ofNullable(value));
    } catch (Exception e) {
        if (e instanceof EtcdException) {
            if (EtcdErrorCode.KeyNotFound == ((EtcdException) e).errorCode) {
                configItemKeyedByName.put(key, Optional.empty());
                return;
            }
        }
        throw new EtcdConfigException("wait for value changed fail", e);
    }
}
 
Example #17
Source File: EtcdFlagFieldUpdater.java    From java-flagz with MIT License 6 votes vote down vote up
/**
 * 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);
  }
}
 
Example #18
Source File: EtcdJsonTest.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetJsonArray() throws EtcdAuthenticationException, TimeoutException, EtcdException, IOException {
    JsonNode fromEtcd = EtcdUtil.getAsJson("/etcd4j_test", etcd);
    JsonNode arrayData = fromEtcd.at("/widget/text/data");

    assertTrue(!arrayData.isValueNode());
    assertEquals(arrayData.size(), 2);
    assertEquals(arrayData.at("/0").asText(), "Click Here");
    assertEquals(arrayData.at("/1").asText(), "Or here");
}
 
Example #19
Source File: EtcdJsonTest.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetPartialJson() throws EtcdAuthenticationException, TimeoutException, EtcdException, IOException {
    JsonNode asJson = EtcdUtil.getAsJson("/etcd4j_test/widget", etcd);
    assertEquals(asJson.at("/debug").asText(), "on");
    assertEquals(asJson.at("/window/name").asText(), "main_window");
    assertEquals(asJson.at("/text/data").size(), 2);
}
 
Example #20
Source File: TestFunctionality.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Tests redirect by sending a key with too many slashes.
 */
@Test
public void testRedirect() throws IOException, EtcdException, EtcdAuthenticationException, TimeoutException {
  etcd.put("etcd4j_test/redirect", "bar").send().get();

  // Test redirect with a double slash
  EtcdKeysResponse response = etcd.get("//etcd4j_test/redirect").consistent().send().get();
  assertEquals("bar", response.node.value);
}
 
Example #21
Source File: EtcdJsonTest.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutJson() throws EtcdAuthenticationException, TimeoutException, EtcdException, IOException {
    ObjectMapper mapper = new ObjectMapper();
    File testJson = new File("src/test/resources/test_data.json");
    JsonNode toEtcd = mapper.readTree(testJson);
    EtcdUtil.putAsJson("/etcd4j_test/put-json", toEtcd, etcd);

    EtcdKeysResponse widget = etcd.get("/etcd4j_test/put-json").send().get();
    assertEquals(widget.getNode().getNodes().size(), 1);

    EtcdKeysResponse widgets = etcd.get("/etcd4j_test/put-json/widget").send().get();
    assertEquals(widgets.getNode().getNodes().size(), 5);
}
 
Example #22
Source File: EtcdJsonTest.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testAssignValueToObject() throws EtcdAuthenticationException, TimeoutException, EtcdException, IOException {
    String newJson = "{\n" +
            "   \"test\":\"value\"\n" +
            "}";

    JsonNode originalConfig = EtcdUtil.getAsJson("/etcd4j_test", etcd);
    assertEquals(originalConfig.at("/widget/window").has("test"), false);

    EtcdUtil.putAsJson("/etcd4j_test/widget/window", EtcdUtil.stringToJson(newJson), etcd);
    JsonNode updatedConfig = EtcdUtil.getAsJson("/etcd4j_test", etcd);
    assertEquals(updatedConfig.at("/widget/window").has("test"), true);
}
 
Example #23
Source File: TestHugeDir.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetHugeDir() throws IOException, EtcdException, EtcdAuthenticationException, TimeoutException {
  for (int i = 0; i < 2000; i++) {
    etcd.put("/etcd4j_test/huge-dir/node-" + i, "bar").send().get();
  }

  List<EtcdKeysResponse.EtcdNode> nodes;

  nodes = etcd.getDir("/etcd4j_test/huge-dir/").send().get().getNode().getNodes();
  assertNotNull(nodes);
  assertEquals(2000, nodes.size());
}
 
Example #24
Source File: TestFunctionality.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 1000)
public void testChunkedData() throws IOException, EtcdException, EtcdAuthenticationException, TimeoutException {
  //creating very long key to force content to be chunked
  StringBuilder stringBuilder = new StringBuilder(15000);
  for (int i = 0; i < 15000; i++) {
    stringBuilder.append("a");
  }
  EtcdKeysResponse response = etcd.put("etcd4j_test/foo", stringBuilder.toString()).send().get();
  assertEquals(EtcdKeyAction.set, response.action);
}
 
Example #25
Source File: EtcdJsonTest.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetLargeNumbers() throws EtcdAuthenticationException, TimeoutException, EtcdException, IOException {
    JsonNode toEtcd = EtcdUtil.stringToJson("{\"number\": \"16161651321274258257474247745577454547774545785547\"}");
    EtcdUtil.putAsJson("/etcd4j_test/put-json", toEtcd, etcd);
    JsonNode asJson = EtcdUtil.getAsJson("/etcd4j_test/put-json", etcd);
    assertEquals("\"Infinity\"", asJson.get("number").toString());
}
 
Example #26
Source File: EtcdTests.java    From spring-cloud-cluster with Apache License 2.0 5 votes vote down vote up
@Test
public void testBlockingThreadLeader() throws InterruptedException, IOException, EtcdException, TimeoutException {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(BlockingThreadTestConfig.class);
	BlockingThreadTestCandidate candidate = ctx.getBean(BlockingThreadTestCandidate.class);
	YieldTestEventListener listener = ctx.getBean(YieldTestEventListener.class);
	assertThat(candidate.onGrantedLatch.await(5, TimeUnit.SECONDS), is(true));
	Thread.sleep(2000); // Let the grant-notification thread run for a while
	candidate.ctx.yield(); // Internally interrupts the grant notification thread
	assertThat(candidate.onRevokedLatch.await(10, TimeUnit.SECONDS), is(true));
	assertThat(listener.onEventsLatch.await(1, TimeUnit.MILLISECONDS), is(true));
	assertThat(listener.events.size(), is(2));
	ctx.close();
}
 
Example #27
Source File: EtcdClient.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Get the Members of Etcd
 *
 * @return vEtcdMembersResponse
 */
public EtcdMembersResponse getMembers() {
  try {
    return new EtcdMembersRequest(this.client,retryHandler).send().get();
  } catch (IOException | EtcdException | EtcdAuthenticationException | TimeoutException e) {
    return null;
  }
}
 
Example #28
Source File: EtcdFlagFieldUpdater.java    From java-flagz with MIT License 5 votes vote down vote up
private List<EtcdNode> fetchAllFlagzNodes() {
  try {
    EtcdResponsePromise<EtcdKeysResponse> promise = client
        .get(this.directoryPrefix)
        .dir()
        .send();
    EtcdKeysResponse response = promise.get();
    // NOTE: We use etcdIndex here, because we know we got latest data up to this point.
    lastKnownFlagzModificationIndex = response.etcdIndex;
    return MoreObjects.firstNonNull(response.node.nodes, ImmutableList.<EtcdNode>of());
  } catch (IOException | EtcdException
      | TimeoutException | EtcdAuthenticationException exception) {
    throw new EtcdFlagFieldUpdaterException.EtcdFetchingFailed(exception);
  }
}
 
Example #29
Source File: EtcdLifecycle.java    From spring-cloud-etcd with Apache License 2.0 5 votes vote down vote up
protected void register(Service service) {
	try {
		log.info("Registering service with etcd: " + service);
		String key = getServiceKey(service.appName, service.getId());
		//TODO: what should be serialized about the service?
		String value = props.getHostname() + ":" + service.getPort();
		etcd.put(key, value).ttl(props.getTtl()).send().get();
	} catch (IOException | TimeoutException | EtcdException e) {
		ReflectionUtils.rethrowRuntimeException(e);
	}
}
 
Example #30
Source File: EtcdClient.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Get the version of the Etcd server
 *
 * @return version as String
 * @deprecated use version() when using etcd 2.1+.
 */
@Deprecated
public String getVersion() {
  try {
    return new EtcdOldVersionRequest(this.client, retryHandler).send().get();
  } catch (IOException | EtcdException | EtcdAuthenticationException | TimeoutException e) {
    return null;
  }
}