/** * Return {@code true} if the {@link CurrentUnitOfWork#isStarted()} returns {@code true} and in if the phase is * {@link UnitOfWork.Phase#STARTED}, otherwise {@code false}. * * @return {@code true} if the {@link CurrentUnitOfWork#isStarted()} returns {@code true} and in if the phase is * {@link UnitOfWork.Phase#STARTED}, otherwise {@code false}. */ private boolean inStartedPhaseOfUnitOfWork() { return CurrentUnitOfWork.isStarted() && UnitOfWork.Phase.STARTED.equals(CurrentUnitOfWork.get().phase()); }
private void commitAsNested() { try { changePhase(Phase.PREPARE_COMMIT, Phase.COMMIT); delegateAfterCommitToParent(this); parentUnitOfWork.onRollback(u -> changePhase(Phase.ROLLBACK)); } catch (Exception e) { setRollbackCause(e); changePhase(Phase.ROLLBACK); throw e; } }
/** * Indicates whether this UnitOfWork is started. It is started when the {@link #start()} method has been called, and * if the UnitOfWork has not been committed or rolled back. * * @return {@code true} if this UnitOfWork is started, {@code false} otherwise. */ default boolean isActive() { return phase().isStarted(); }
@Override public Aggregate<T> newInstance(Callable<T> factoryMethod) throws Exception { CurrentUnitOfWork.get().onRollback(u -> this.rolledBack = true); aggregate = delegate.newInstance(factoryMethod); return aggregate; }
private void delegateAfterCommitToParent(UnitOfWork<?> uow) { Optional<UnitOfWork<?>> parent = uow.parent(); if (parent.isPresent()) { parent.get().afterCommit(this::delegateAfterCommitToParent); } else { changePhase(Phase.AFTER_COMMIT); } }
@Override protected void addHandler(Phase phase, Consumer<UnitOfWork<T>> handler) { Assert.state(!phase.isBefore(phase()), () -> "Cannot register a listener for phase: " + phase + " because the Unit of Work is already in a later phase: " + phase()); processingContext.addHandler(phase, handler); }
@Override public Boolean resolveParameterValue(Message<?> message) { return CurrentUnitOfWork.map(unitOfWork -> !(unitOfWork instanceof BatchingUnitOfWork<?>) || ((BatchingUnitOfWork<?>) unitOfWork).isLastMessage(message)).orElse(true); }
/** * Initiates the rollback of this Unit of Work, invoking all registered rollback ({@link #onRollback(Consumer) and * clean-up handlers {@link #onCleanup(Consumer)}} respectively. Finally, the Unit of Work is unregistered from the * {@link CurrentUnitOfWork}. * <p/> * If the rollback is a result of an exception, consider using {@link #rollback(Throwable)} instead. * * @throws IllegalStateException if the Unit of Work is not in a compatible phase. */ default void rollback() { rollback(null); }
/** * Checks if the message being processed now is the last of the batch being processed in this unit of work. * * @return {@code true} if the message is the last of this batch, {@code false} otherwise */ public boolean isLastMessage() { return isLastMessage(getMessage()); }
/** * Checks if the message being processed now is the first of the batch being processed in this unit of work. * * @return {@code true} if the message is the first of this batch, {@code false} otherwise */ public boolean isFirstMessage() { return isFirstMessage(getMessage()); }
@Override public UnitOfWork<T> transformMessage(Function<T, ? extends Message<?>> transformOperator) { processingContext.transformMessage(transformOperator); return this; }
/** * Pause this Unit of Work by unregistering it with the {@link CurrentUnitOfWork}. This will detach it from the * current thread. */ public void pause() { CurrentUnitOfWork.clear(this); }
/** * Initializes a Unit of Work (without starting it). * * @param message the message that will be processed in the context of the unit of work */ public DefaultUnitOfWork(T message) { processingContext = new MessageProcessingContext<>(message); }
@Override public ExecutionResult getExecutionResult() { return processingContext.getExecutionResult(); } }
/** * Check if the Unit of Work is the 'currently' active Unit of Work returned by {@link CurrentUnitOfWork#get()}. * * @return {@code true} if the Unit of Work is the currently active Unit of Work */ default boolean isCurrent() { return CurrentUnitOfWork.isStarted() && CurrentUnitOfWork.get() == this; }
@Override public UnitOfWork<T> transformMessage(Function<T, ? extends Message<?>> transformOperator) { processingContext.transformMessage(transformOperator); return this; }
@Override public UnitOfWork<T> transformMessage(Function<T, ? extends Message<?>> transformOperator) { processingContext.transformMessage(transformOperator); return this; }