diff --git a/EtcdConnector/pom.xml b/EtcdConnector/pom.xml index d79c7fa..46c0690 100644 --- a/EtcdConnector/pom.xml +++ b/EtcdConnector/pom.xml @@ -9,7 +9,7 @@ 4.0.0 etcdconnector - 1.1-SNAPSHOT + 1.2-SNAPSHOT diff --git a/EtcdConnector/src/main/java/eu/mikroskeem/utils/etcdconnector/EtcdConnector.java b/EtcdConnector/src/main/java/eu/mikroskeem/utils/etcdconnector/EtcdConnector.java index efcea6d..56d2750 100644 --- a/EtcdConnector/src/main/java/eu/mikroskeem/utils/etcdconnector/EtcdConnector.java +++ b/EtcdConnector/src/main/java/eu/mikroskeem/utils/etcdconnector/EtcdConnector.java @@ -1,34 +1,80 @@ package eu.mikroskeem.utils.etcdconnector; -import mousio.client.retry.RetryOnce; -import mousio.etcd4j.EtcdClient; -import org.slf4j.LoggerFactory; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.io.IOException; -import java.net.URI; +import java.util.List; +import java.util.function.Function; + +public interface EtcdConnector { -public class EtcdConnector extends EtcdConnectorBase { /** - * Sets up EtcdConnector against - * Etcd server with no encryption + * Closes Etcd4j client * - * @param etcdUrls url(s) where to connect to + * @throws IOException Thrown by Etcd4j */ - public EtcdConnector(URI... etcdUrls) throws IOException { - super(); + void close() throws IOException; - /* Set up logger */ - logger = LoggerFactory.getLogger("EtcdConnector"); + /** + * Gets etcd key value + * + * @param path Key path + * @return Key value + */ + @Nullable String getKey(@NotNull String path); - /* Initialize client */ - initClient(etcdUrls); + /** + * Gets etcd key value and sets default, + * if key isn't defined + * + * @param path Key path + * @param defaultValue Value to set if key is nonexistent + * @return Key value + */ + @Nullable String getKey(@NotNull String path, @NotNull String defaultValue); - /* Test client */ - testEtcd(); - } + /** + * Gets etcd directory contents and returns it as + * List of String + * + * @param path directory path + * @return directory contents + */ + @Nullable List getDir(@NotNull String path); - @Override void initClient(URI... urls){ - etcdClient = new EtcdClient(urls); - etcdClient.setRetryHandler(new RetryOnce(5000)); - } -} \ No newline at end of file + /** + * Sets etcd key value + * + * @param path key path + * @param value key value + * @return whether write succeeded or not + */ + boolean putKey(@NotNull String path, @NotNull String value); + + /** + * Sets etcd key value with TTL + * + * @param path key path + * @param value key value + * @param ttl key ttl (seconds) + * @return whether write succeeded or not + */ + boolean putKey(@NotNull String path, @NotNull String value, @NotNull Integer ttl); + + /** + * Deletes etcd key + * + * @param path key path + * @return whether delete succeeded or not + */ + boolean deleteKey(@NotNull String path); + + /** + * Waits for key change + * + * @param path Key path + * @param callback Callback to run + */ + void waitKey(@NotNull String path, @NotNull Function callback); +} diff --git a/EtcdConnector/src/main/java/eu/mikroskeem/utils/etcdconnector/EtcdConnectorBase.java b/EtcdConnector/src/main/java/eu/mikroskeem/utils/etcdconnector/EtcdConnectorBase.java index 6b799e0..d1ca204 100644 --- a/EtcdConnector/src/main/java/eu/mikroskeem/utils/etcdconnector/EtcdConnectorBase.java +++ b/EtcdConnector/src/main/java/eu/mikroskeem/utils/etcdconnector/EtcdConnectorBase.java @@ -3,11 +3,11 @@ package eu.mikroskeem.utils.etcdconnector; import com.google.gson.Gson; import mousio.etcd4j.EtcdClient; +import mousio.etcd4j.promises.EtcdResponsePromise; import mousio.etcd4j.responses.*; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.io.File; import java.io.IOException; @@ -15,14 +15,14 @@ import java.net.URI; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeoutException; +import java.util.function.Function; -abstract class EtcdConnectorBase { +abstract class EtcdConnectorBase implements EtcdConnector { Logger logger; EtcdClient etcdClient; EtcdConnectorBase() throws IOException {} - /** * Initializes EtcdClient object */ @@ -45,23 +45,12 @@ abstract class EtcdConnectorBase { logger.debug("Etcd is working!"); } - /** - * Closes Etcd4j client - * - * @throws IOException Thrown by Etcd4j - */ public void close() throws IOException { if(this.etcdClient != null){ this.etcdClient.close(); } } - /** - * Gets etcd key value - * - * @param path Key path - * @return Key value - */ @Nullable public String getKey(@NotNull String path){ logger.debug("getKey: {}", path); @@ -83,14 +72,6 @@ abstract class EtcdConnectorBase { return null; } - /** - * Gets etcd key value and sets default, - * if key isn't defined - * - * @param path Key path - * @param defaultValue Value to set if key is nonexistent - * @return Key value - */ @Nullable public String getKey(@NotNull String path, @NotNull String defaultValue){ logger.debug("getKey: {}, {}", path, defaultValue); @@ -114,13 +95,6 @@ abstract class EtcdConnectorBase { return null; } - /** - * Gets etcd directory contents and returns it as - * List of String - * - * @param path directory path - * @return directory contents - */ @Nullable public List getDir(@NotNull String path){ logger.debug("getDir: {}", path); @@ -140,13 +114,6 @@ abstract class EtcdConnectorBase { return null; } - /** - * Sets etcd key value - * - * @param path key path - * @param value key value - * @return whether write succeeded or not - */ public boolean putKey(@NotNull String path, @NotNull String value){ logger.debug("putKey: {}, {}", path, value); try { @@ -160,14 +127,6 @@ abstract class EtcdConnectorBase { return false; } - /** - * Sets etcd key value with TTL - * - * @param path key path - * @param value key value - * @param ttl key ttl (seconds) - * @return whether write succeeded or not - */ public boolean putKey(@NotNull String path, @NotNull String value, @NotNull Integer ttl){ logger.debug("putKey: {}, {}, {}", path, value, ttl); try { @@ -180,11 +139,6 @@ abstract class EtcdConnectorBase { return false; } - /** - * Deletes etcd key - * @param path key path - * @return whether delete succeeded or not - */ public boolean deleteKey(@NotNull String path){ logger.debug("deleteKey: {}", path); try { @@ -200,4 +154,16 @@ abstract class EtcdConnectorBase { } return false; } + + public void waitKey(@NotNull String path, @NotNull Function callback){ + logger.debug("waitKey: {}", path); + try { + EtcdResponsePromise promise = etcdClient.get(path).waitForChange().send(); + promise.addListener(p -> { + callback.apply(p.getNow()); + }); + } catch (IOException e){ + e.printStackTrace(); + } + } } diff --git a/EtcdConnector/src/main/java/eu/mikroskeem/utils/etcdconnector/EtcdHTTPConnector.java b/EtcdConnector/src/main/java/eu/mikroskeem/utils/etcdconnector/EtcdHTTPConnector.java new file mode 100644 index 0000000..734514c --- /dev/null +++ b/EtcdConnector/src/main/java/eu/mikroskeem/utils/etcdconnector/EtcdHTTPConnector.java @@ -0,0 +1,34 @@ +package eu.mikroskeem.utils.etcdconnector; + +import mousio.client.retry.RetryOnce; +import mousio.etcd4j.EtcdClient; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.net.URI; + +public class EtcdHTTPConnector extends EtcdConnectorBase implements EtcdConnector { + /** + * Sets up EtcdHTTPConnector against + * Etcd server with no encryption + * + * @param etcdUrls url(s) where to connect to + */ + public EtcdHTTPConnector(URI... etcdUrls) throws IOException { + super(); + + /* Set up logger */ + logger = LoggerFactory.getLogger("EtcdConnector"); + + /* Initialize client */ + initClient(etcdUrls); + + /* Test client */ + testEtcd(); + } + + @Override void initClient(URI... urls){ + etcdClient = new EtcdClient(urls); + etcdClient.setRetryHandler(new RetryOnce(5000)); + } +} \ No newline at end of file diff --git a/EtcdConnector/src/main/java/eu/mikroskeem/utils/etcdconnector/EtcdSSLConnector.java b/EtcdConnector/src/main/java/eu/mikroskeem/utils/etcdconnector/EtcdSSLConnector.java index e98c99b..ef7a728 100644 --- a/EtcdConnector/src/main/java/eu/mikroskeem/utils/etcdconnector/EtcdSSLConnector.java +++ b/EtcdConnector/src/main/java/eu/mikroskeem/utils/etcdconnector/EtcdSSLConnector.java @@ -10,7 +10,7 @@ import javax.net.ssl.SSLException; import java.io.IOException; import java.net.URI; -public class EtcdSSLConnector extends EtcdConnectorBase { +public class EtcdSSLConnector extends EtcdConnectorBase implements EtcdConnector { public EtcdSSLConnector(URI... etcdUrls) throws IOException { super();