From a9067bf1610f9948da43d5abeba2b3c0c61b2dea Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Tue, 27 Feb 2024 12:01:42 +0100 Subject: [PATCH] [Major] Refactored SimpleExecution.runWith* actions to take a Consumer in the event of an exception --- .../execution/policy/SimpleExecution.java | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/service/src/main/java/li/strolch/execution/policy/SimpleExecution.java b/service/src/main/java/li/strolch/execution/policy/SimpleExecution.java index 461f69b82..fa33f74fa 100644 --- a/service/src/main/java/li/strolch/execution/policy/SimpleExecution.java +++ b/service/src/main/java/li/strolch/execution/policy/SimpleExecution.java @@ -10,11 +10,12 @@ import li.strolch.model.timevalue.impl.FloatValue; import li.strolch.model.timevalue.impl.ValueChange; import li.strolch.persistence.api.StrolchTransaction; import li.strolch.privilege.model.PrivilegeContext; +import li.strolch.utils.CheckedBiConsumer; import li.strolch.utils.time.PeriodDuration; import java.util.Locale; import java.util.concurrent.ScheduledFuture; -import java.util.function.BiConsumer; +import java.util.function.Consumer; import java.util.function.Supplier; /** @@ -39,8 +40,9 @@ public class SimpleExecution extends ExecutionPolicy { protected void startWarningTask(PeriodDuration duration, Action action, Supplier handler) { if (this.warningTask != null) { - logger.warn("There is already a warning task registered, for action " + action.getLocator() + - ". Cancelling and creating a new task..."); + logger.warn("There is already a warning task registered, for action " + + action.getLocator() + + ". Cancelling and creating a new task..."); this.warningTask.cancel(true); this.warningTask = null; } @@ -150,18 +152,18 @@ public class SimpleExecution extends ExecutionPolicy { return getContainer().getRealm(ctx.getCertificate()).openTx(ctx.getCertificate(), getClass(), readOnly); } - protected void runWithFreshActionReadonly(BiConsumer consumer, - Supplier failMsgSupplier) { - runWithFreshAction(true, consumer, failMsgSupplier); + protected void runWithFreshActionReadonly(CheckedBiConsumer consumer, + Consumer failHandler) { + runWithFreshAction(true, consumer, failHandler); } - protected void runWithFreshActionWritable(BiConsumer consumer, - Supplier failMsgSupplier) { - runWithFreshAction(false, consumer, failMsgSupplier); + protected void runWithFreshActionWritable(CheckedBiConsumer consumer, + Consumer failHandler) { + runWithFreshAction(false, consumer, failHandler); } - private void runWithFreshAction(boolean readOnly, BiConsumer consumer, - Supplier failMsgSupplier) { + private void runWithFreshAction(boolean readOnly, CheckedBiConsumer consumer, + Consumer failHandler) { try { runAsAgent(ctx -> { try (StrolchTransaction tx = openLocalTx(ctx, readOnly)) { @@ -174,7 +176,7 @@ public class SimpleExecution extends ExecutionPolicy { } }); } catch (Exception e) { - logger.error(failMsgSupplier.get(), e); + failHandler.accept(e); } } }