diff --git a/pom.xml b/pom.xml index cc1772f..16297d9 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ com.switcherapi switcher-client jar - 2.6.0 + 2.6.1-SNAPSHOT Switcher Client Switcher Client SDK for working with Switcher API diff --git a/src/main/java/com/switcherapi/client/model/AsyncSwitcher.java b/src/main/java/com/switcherapi/client/model/AsyncSwitcher.java index 5febab0..e07d399 100644 --- a/src/main/java/com/switcherapi/client/model/AsyncSwitcher.java +++ b/src/main/java/com/switcherapi/client/model/AsyncSwitcher.java @@ -31,7 +31,7 @@ public class AsyncSwitcher { private long nextRun = 0; public AsyncSwitcher(final Switcher switcher, long delay) { - this.executorService = Executors.newCachedThreadPool(r -> { + this.executorService = Executors.newSingleThreadExecutor(r -> { Thread thread = new Thread(r); thread.setName(SWITCHER_ASYNC_WORKER.toString()); thread.setDaemon(true); @@ -47,14 +47,19 @@ public AsyncSwitcher(final Switcher switcher, long delay) { * Switcher result for the Switcher executions map. */ public void execute() { - if (logger.isDebugEnabled()) { - SwitcherUtils.debug(logger, "nextRun: {} - currentTimeMillis: {}", nextRun, System.currentTimeMillis()); + final long currentTime = System.currentTimeMillis(); + final boolean debugEnabled = logger.isDebugEnabled(); + + if (debugEnabled) { + SwitcherUtils.debug(logger, "nextRun: {} - currentTimeMillis: {}", nextRun, currentTime); } - - if (nextRun < System.currentTimeMillis()) { - SwitcherUtils.debug(logger, "Running AsyncSwitcher"); - this.nextRun = System.currentTimeMillis() + this.delay; + if (nextRun < currentTime) { + if (debugEnabled) { + SwitcherUtils.debug(logger, "Running AsyncSwitcher"); + } + + this.nextRun = currentTime + this.delay; this.executorService.submit(this::run); } } diff --git a/src/main/java/com/switcherapi/client/model/SwitcherRequest.java b/src/main/java/com/switcherapi/client/model/SwitcherRequest.java index 1efb0a2..4cafc8c 100644 --- a/src/main/java/com/switcherapi/client/model/SwitcherRequest.java +++ b/src/main/java/com/switcherapi/client/model/SwitcherRequest.java @@ -8,7 +8,7 @@ import java.util.ArrayList; import java.util.List; -import java.util.Objects; +import java.util.Map; import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; @@ -83,18 +83,19 @@ public boolean isItOn() throws SwitcherException { @Override public SwitcherResult submit() throws SwitcherException { - if (SwitcherBypass.getBypass().containsKey(switcherKey)) { - return SwitcherBypass.getBypass().get(switcherKey).buildFromSwitcher(switcherKey, entry); + final Map bypass = SwitcherBypass.getBypass(); + if (!bypass.isEmpty() && bypass.containsKey(switcherKey)) { + return bypass.get(switcherKey).buildFromSwitcher(switcherKey, entry); } if (canUseAsync()) { - if (Objects.isNull(asyncSwitcher)) { + if (asyncSwitcher == null) { asyncSwitcher = new AsyncSwitcher(this, super.delay); } asyncSwitcher.execute(); final SwitcherResult response = executionsMap.get(entry); - if (Objects.nonNull(response)) { + if (response != null) { return response; } }