This is an automated email from the ASF dual-hosted git repository. danhaywood pushed a commit to branch CAUSEWAY-3738 in repository https://gitbox.apache.org/repos/asf/causeway.git
commit 8375f70917bb20fbba56012b77b8c7599ec1a3cf Author: danhaywood <[email protected]> AuthorDate: Wed May 8 22:36:35 2024 +0100 CAUSEWAY-3738: extracts pendingCommandDtos and executeWithinOwnTransaction --- .../applib/job/RunBackgroundCommandsJob.java | 45 +++++++++++++--------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/extensions/core/commandlog/applib/src/main/java/org/apache/causeway/extensions/commandlog/applib/job/RunBackgroundCommandsJob.java b/extensions/core/commandlog/applib/src/main/java/org/apache/causeway/extensions/commandlog/applib/job/RunBackgroundCommandsJob.java index 1c0d954bcc..ee57bd3545 100644 --- a/extensions/core/commandlog/applib/src/main/java/org/apache/causeway/extensions/commandlog/applib/job/RunBackgroundCommandsJob.java +++ b/extensions/core/commandlog/applib/src/main/java/org/apache/causeway/extensions/commandlog/applib/job/RunBackgroundCommandsJob.java @@ -87,6 +87,17 @@ public class RunBackgroundCommandsJob implements Job { InteractionContext interactionContext = InteractionContext.builder().user(user).build(); // we obtain the list of Commands first; we use their CommandDto as it is serializable across transactions + final Optional<List<CommandDto>> commandDtosIfAny = pendingCommandDtos(interactionContext); + + // for each command, we execute within its own transaction. Failure of one should not impact the next. + commandDtosIfAny.ifPresent(commandDtos -> { + for (val commandDto : commandDtos) { + executeWithinOwnTransaction(commandDto, interactionContext); + } + }); + } + + private Optional<List<CommandDto>> pendingCommandDtos(InteractionContext interactionContext) { final Optional<List<CommandDto>> commandDtosIfAny = interactionService.callAndCatch(interactionContext, () -> transactionService.callTransactional(Propagation.REQUIRES_NEW, () -> @@ -100,24 +111,24 @@ public class RunBackgroundCommandsJob implements Job { ) .ifFailureFail() // we give up if unable to find these .getValue(); + return commandDtosIfAny; + } - // for each command, we execute within its own transaction. Failure of one should not impact the next. - commandDtosIfAny.ifPresent(commandDtos -> { - for (val commandDto : commandDtos) { - interactionService.runAndCatch(interactionContext, () -> { + private void executeWithinOwnTransaction(CommandDto commandDto, InteractionContext interactionContext) { + interactionService.runAndCatch(interactionContext, () -> { transactionService.runTransactional(Propagation.REQUIRES_NEW, () -> { - // look up the CommandLogEntry again because we are within a new transaction. - val commandLogEntryIfAny = commandLogEntryRepository.findByInteractionId(UUID.fromString(commandDto.getInteractionId())); - - // finally, we execute - commandLogEntryIfAny.ifPresent(commandLogEntry -> - { - commandExecutorService.executeCommand( - CommandExecutorService.InteractionContextPolicy.NO_SWITCH, commandDto); - commandLogEntry.setCompletedAt(clockService.getClock().nowAsJavaSqlTimestamp()); - }); - }) - .ifFailureFail(); + // look up the CommandLogEntry again because we are within a new transaction. + val commandLogEntryIfAny = commandLogEntryRepository.findByInteractionId(UUID.fromString(commandDto.getInteractionId())); + + // finally, we execute + commandLogEntryIfAny.ifPresent(commandLogEntry -> + { + commandExecutorService.executeCommand( + CommandExecutorService.InteractionContextPolicy.NO_SWITCH, commandDto); + commandLogEntry.setCompletedAt(clockService.getClock().nowAsJavaSqlTimestamp()); + }); + }) + .ifFailureFail(); }) .ifFailure(throwable -> { log.error("Failed to execute command: " + CommandDtoUtils.dtoMapper().toString(commandDto), throwable); @@ -136,8 +147,6 @@ public class RunBackgroundCommandsJob implements Job { }); }); }); - } - }); } }
