diff --git a/src/main/java/com/github/switcherapi/client/SnapshotCallback.java b/src/main/java/com/github/switcherapi/client/SnapshotCallback.java
index f597c022..071f0e2f 100644
--- a/src/main/java/com/github/switcherapi/client/SnapshotCallback.java
+++ b/src/main/java/com/github/switcherapi/client/SnapshotCallback.java
@@ -2,19 +2,16 @@
import com.github.switcherapi.client.utils.SwitcherUtils;
import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.Logger;
-public class SnapshotCallback {
-
- private static final Logger logger = LogManager.getLogger(SnapshotCallback.class);
+public interface SnapshotCallback {
/**
* Callback method that will be invoked when the snapshot is updated
*
* @param version of the new snapshot
*/
- public void onSnapshotUpdate(long version) {
- SwitcherUtils.debug(logger, "Snapshot updated: {}", version);
+ default void onSnapshotUpdate(long version) {
+ SwitcherUtils.debug(LogManager.getLogger(SnapshotCallback.class), "Snapshot updated: {}", version);
}
/**
@@ -22,7 +19,7 @@ public void onSnapshotUpdate(long version) {
*
* @param e Exception
*/
- public void onSnapshotUpdateError(Exception e) {
- SwitcherUtils.debug(logger, "Failed to update snapshot: {}", e.getMessage());
+ default void onSnapshotUpdateError(Exception e) {
+ SwitcherUtils.debug(LogManager.getLogger(SnapshotCallback.class), "Failed to update snapshot: {}", e.getMessage());
}
}
diff --git a/src/main/java/com/github/switcherapi/client/SwitcherContextBase.java b/src/main/java/com/github/switcherapi/client/SwitcherContextBase.java
index 3fc5b5c5..f8a477fe 100644
--- a/src/main/java/com/github/switcherapi/client/SwitcherContextBase.java
+++ b/src/main/java/com/github/switcherapi/client/SwitcherContextBase.java
@@ -178,7 +178,7 @@ public static boolean scheduleSnapshotAutoUpdate(String intervalValue, SnapshotC
}
final long interval = SwitcherUtils.getMillis(intervalValue);
- final SnapshotCallback callbackFinal = Optional.ofNullable(callback).orElse(new SnapshotCallback());
+ final SnapshotCallback callbackFinal = Optional.ofNullable(callback).orElse(new SnapshotCallback() {});
final Runnable runnableSnapshotValidate = () -> {
try {
if (validateSnapshot()) {
@@ -270,19 +270,31 @@ public static boolean validateSnapshot() {
/**
* Start watching snapshot files for modifications.
* When the file is modified the in-memory snapshot will reload
+ *
+ *
+ * (*) Requires client to use local settings
*/
public static void watchSnapshot() {
- watchSnapshot(new SnapshotEventHandler());
+ watchSnapshot(new SnapshotEventHandler() {});
}
/**
* Start watching snapshot files for modifications.
* When the file is modified the in-memory snapshot will reload
+ *
+ *
+ * (*) Requires client to use local settings
*
* @param handler to notify snapshot change events
+ * @throws SwitcherException if using remote service
*/
public static void watchSnapshot(SnapshotEventHandler handler) {
- SwitcherUtils.watchSnapshot(instance, handler);
+ if (!(instance instanceof SwitcherLocalService)) {
+ throw new SwitcherException("Cannot watch snapshot when using remote", new UnsupportedOperationException());
+ }
+
+ SwitcherLocalService executorInstance = (SwitcherLocalService) instance;
+ SwitcherUtils.watchSnapshot(executorInstance, handler);
}
/**
diff --git a/src/main/java/com/github/switcherapi/client/SwitcherExecutor.java b/src/main/java/com/github/switcherapi/client/SwitcherExecutor.java
index 3e71036f..6675dbad 100644
--- a/src/main/java/com/github/switcherapi/client/SwitcherExecutor.java
+++ b/src/main/java/com/github/switcherapi/client/SwitcherExecutor.java
@@ -8,7 +8,6 @@
import com.github.switcherapi.client.model.criteria.Snapshot;
import com.github.switcherapi.client.model.response.CriteriaResponse;
import com.github.switcherapi.client.service.remote.ClientRemote;
-import com.github.switcherapi.client.utils.SnapshotEventHandler;
import com.github.switcherapi.client.utils.SnapshotLoader;
import com.github.switcherapi.client.utils.SwitcherUtils;
import com.google.gson.Gson;
@@ -59,16 +58,6 @@ public abstract class SwitcherExecutor {
* @param switchers To be validated
*/
public abstract void checkSwitchers(final Set
@@ -12,16 +10,22 @@
* @author Roger Floriano (petruki)
* @since 2022-06-29
*/
-public class SnapshotEventHandler {
-
- private static final Logger logger = LogManager.getLogger(SnapshotEventHandler.class);
-
- public void onSuccess() {
- SwitcherUtils.debug(logger, "Snapshot has been changed");
+public interface SnapshotEventHandler {
+
+ /**
+ * Callback method that will be invoked when the snapshot is updated
+ */
+ default void onSuccess() {
+ SwitcherUtils.debug(LogManager.getLogger(SnapshotEventHandler.class), "Snapshot has been changed");
}
-
- public void onError(SwitcherException exception) {
- logger.error(exception);
+
+ /**
+ * Callback method that will be invoked when the snapshot update fails
+ *
+ * @param exception Exception
+ */
+ default void onError(SwitcherException exception) {
+ LogManager.getLogger(SnapshotEventHandler.class).error(exception);
}
}
diff --git a/src/main/java/com/github/switcherapi/client/utils/SnapshotWatcher.java b/src/main/java/com/github/switcherapi/client/utils/SnapshotWatcher.java
index 1af79475..c6d70947 100644
--- a/src/main/java/com/github/switcherapi/client/utils/SnapshotWatcher.java
+++ b/src/main/java/com/github/switcherapi/client/utils/SnapshotWatcher.java
@@ -1,23 +1,17 @@
package com.github.switcherapi.client.utils;
-import java.io.IOException;
-import java.nio.file.ClosedWatchServiceException;
-import java.nio.file.FileSystems;
-import java.nio.file.Path;
-import java.nio.file.Paths;
-import java.nio.file.StandardWatchEventKinds;
-import java.nio.file.WatchEvent;
-import java.nio.file.WatchKey;
-import java.nio.file.WatchService;
-
+import com.github.switcherapi.client.SwitcherContextBase;
+import com.github.switcherapi.client.model.ContextKey;
+import com.github.switcherapi.client.service.local.SwitcherLocalService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
-import com.github.switcherapi.client.SwitcherContextBase;
-import com.github.switcherapi.client.SwitcherExecutor;
-import com.github.switcherapi.client.model.ContextKey;
+import java.io.IOException;
+import java.nio.file.*;
/**
+ * SnapshotWatcher runs in a separate thread to watch for changes in the snapshot file.
+ *
* @author Roger Floriano (petruki)
* @since 2020-05-13
*/
@@ -29,9 +23,9 @@ public class SnapshotWatcher implements Runnable {
private WatchService watcher;
- private SwitcherExecutor executorInstance;
+ private SwitcherLocalService executorInstance;
- public SnapshotWatcher(final SwitcherExecutor executorInstance, SnapshotEventHandler handler) {
+ public SnapshotWatcher(final SwitcherLocalService executorInstance, SnapshotEventHandler handler) {
this.executorInstance = executorInstance;
this.handler = handler;
}
diff --git a/src/main/java/com/github/switcherapi/client/utils/SwitcherUtils.java b/src/main/java/com/github/switcherapi/client/utils/SwitcherUtils.java
index e54b1558..61a6527a 100644
--- a/src/main/java/com/github/switcherapi/client/utils/SwitcherUtils.java
+++ b/src/main/java/com/github/switcherapi/client/utils/SwitcherUtils.java
@@ -1,10 +1,10 @@
package com.github.switcherapi.client.utils;
-import com.github.switcherapi.client.SwitcherExecutor;
import com.github.switcherapi.client.exception.SwitcherContextException;
import com.github.switcherapi.client.exception.SwitcherInvalidDateTimeArgumentException;
import com.github.switcherapi.client.model.ContextKey;
import com.github.switcherapi.client.service.WorkerName;
+import com.github.switcherapi.client.service.local.SwitcherLocalService;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
@@ -141,7 +141,7 @@ public static Set