From 10bceb906d084a55b3d0bd8e0072b972012da1c8 Mon Sep 17 00:00:00 2001
From: petruki <31597636+petruki@users.noreply.github.com>
Date: Sat, 15 Jun 2024 16:10:37 -0700
Subject: [PATCH 1/3] Improved Snapshot scheduler and watcher APIs interface
---
.../switcherapi/client/SnapshotCallback.java | 13 ++++-----
.../client/SwitcherContextBase.java | 4 +--
.../switcherapi/client/SwitcherExecutor.java | 8 ++++++
.../service/local/SwitcherLocalService.java | 5 ++++
.../service/remote/SwitcherRemoteService.java | 5 ++++
.../client/utils/SnapshotEventHandler.java | 28 +++++++++++--------
.../local/SwitcherLocalServiceTest.java | 4 +--
7 files changed, 43 insertions(+), 24 deletions(-)
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..0a34e581 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()) {
@@ -272,7 +272,7 @@ public static boolean validateSnapshot() {
* When the file is modified the in-memory snapshot will reload
*/
public static void watchSnapshot() {
- watchSnapshot(new SnapshotEventHandler());
+ watchSnapshot(new SnapshotEventHandler() {});
}
/**
diff --git a/src/main/java/com/github/switcherapi/client/SwitcherExecutor.java b/src/main/java/com/github/switcherapi/client/SwitcherExecutor.java
index 3e71036f..19387bd2 100644
--- a/src/main/java/com/github/switcherapi/client/SwitcherExecutor.java
+++ b/src/main/java/com/github/switcherapi/client/SwitcherExecutor.java
@@ -70,6 +70,14 @@ public abstract class SwitcherExecutor {
*/
public abstract boolean notifyChange(final String snapshotFile, SnapshotEventHandler handler);
+ /**
+ * Update in-memory snapshot.
+ *
+ * @param snapshotFile Path location
+ * @return true if valid change
+ */
+ public abstract boolean notifyChange(final String snapshotFile);
+
/**
* Retrieve local snapshot version
*
diff --git a/src/main/java/com/github/switcherapi/client/service/local/SwitcherLocalService.java b/src/main/java/com/github/switcherapi/client/service/local/SwitcherLocalService.java
index 2b547ee7..8888f6a8 100644
--- a/src/main/java/com/github/switcherapi/client/service/local/SwitcherLocalService.java
+++ b/src/main/java/com/github/switcherapi/client/service/local/SwitcherLocalService.java
@@ -133,6 +133,11 @@ public boolean notifyChange(final String snapshotFile, SnapshotEventHandler hand
return true;
}
+ @Override
+ public boolean notifyChange(final String snapshotFile) {
+ return this.notifyChange(snapshotFile, new SnapshotEventHandler() {});
+ }
+
@Override
public long getSnapshotVersion() {
return domain.getVersion();
diff --git a/src/main/java/com/github/switcherapi/client/service/remote/SwitcherRemoteService.java b/src/main/java/com/github/switcherapi/client/service/remote/SwitcherRemoteService.java
index 3efa9586..73310420 100644
--- a/src/main/java/com/github/switcherapi/client/service/remote/SwitcherRemoteService.java
+++ b/src/main/java/com/github/switcherapi/client/service/remote/SwitcherRemoteService.java
@@ -101,6 +101,11 @@ public boolean notifyChange(String snapshotFile, SnapshotEventHandler handler) {
return this.switcherLocal.notifyChange(snapshotFile, handler);
}
+ @Override
+ public boolean notifyChange(String snapshotFile) {
+ return this.switcherLocal.notifyChange(snapshotFile);
+ }
+
@Override
public long getSnapshotVersion() {
return switcherLocal.getSnapshotVersion();
diff --git a/src/main/java/com/github/switcherapi/client/utils/SnapshotEventHandler.java b/src/main/java/com/github/switcherapi/client/utils/SnapshotEventHandler.java
index 6261932c..6752642f 100644
--- a/src/main/java/com/github/switcherapi/client/utils/SnapshotEventHandler.java
+++ b/src/main/java/com/github/switcherapi/client/utils/SnapshotEventHandler.java
@@ -1,9 +1,7 @@
package com.github.switcherapi.client.utils;
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.Logger;
-
import com.github.switcherapi.client.exception.SwitcherException;
+import org.apache.logging.log4j.LogManager;
/**
* Access snapshot event handler when a file is modified.
@@ -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/test/java/com/github/switcherapi/client/service/local/SwitcherLocalServiceTest.java b/src/test/java/com/github/switcherapi/client/service/local/SwitcherLocalServiceTest.java
index 687e4f4b..e4cb7d0e 100644
--- a/src/test/java/com/github/switcherapi/client/service/local/SwitcherLocalServiceTest.java
+++ b/src/test/java/com/github/switcherapi/client/service/local/SwitcherLocalServiceTest.java
@@ -37,7 +37,7 @@ void shouldNotifyWithError() {
SwitchersBase.configure(ContextBuilder.builder()
.environment("defect_default"));
- assertFalse(service.notifyChange("defect_default.json", new SnapshotEventHandler()));
+ assertFalse(service.notifyChange("defect_default.json"));
}
@Test
@@ -45,7 +45,7 @@ void shouldNotifyWithSuccess() {
SwitchersBase.configure(ContextBuilder.builder()
.environment("snapshot_watcher"));
- assertTrue(service.notifyChange("snapshot_watcher.json", new SnapshotEventHandler()));
+ assertTrue(service.notifyChange("snapshot_watcher.json"));
}
@Test
From 3d23facc7d019d62c272e1fca1d850ced4778057 Mon Sep 17 00:00:00 2001
From: petruki <31597636+petruki@users.noreply.github.com>
Date: Sat, 15 Jun 2024 16:52:02 -0700
Subject: [PATCH 2/3] Refactored SwitcherWatcher to be used only with Local
service
---
.../client/SwitcherContextBase.java | 14 +++-
.../switcherapi/client/SwitcherExecutor.java | 18 ------
.../service/local/SwitcherLocalService.java | 64 +++++++++++--------
.../service/remote/SwitcherRemoteService.java | 26 ++------
.../client/utils/SnapshotWatcher.java | 24 +++----
.../client/utils/SwitcherUtils.java | 4 +-
.../utils/SnapshotWatcherErrorTest.java | 30 +++++++++
7 files changed, 99 insertions(+), 81 deletions(-)
create mode 100644 src/test/java/com/github/switcherapi/client/utils/SnapshotWatcherErrorTest.java
diff --git a/src/main/java/com/github/switcherapi/client/SwitcherContextBase.java b/src/main/java/com/github/switcherapi/client/SwitcherContextBase.java
index 0a34e581..f8a477fe 100644
--- a/src/main/java/com/github/switcherapi/client/SwitcherContextBase.java
+++ b/src/main/java/com/github/switcherapi/client/SwitcherContextBase.java
@@ -270,6 +270,9 @@ 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() {});
@@ -278,11 +281,20 @@ public static void watchSnapshot() {
/**
* 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 19387bd2..b6817b0d 100644
--- a/src/main/java/com/github/switcherapi/client/SwitcherExecutor.java
+++ b/src/main/java/com/github/switcherapi/client/SwitcherExecutor.java
@@ -59,24 +59,6 @@ public abstract class SwitcherExecutor {
* @param switchers To be validated
*/
public abstract void checkSwitchers(final Set