@Override public void onCommit(Consumer<UnitOfWork<T>> handler) { addHandler(Phase.COMMIT, handler); }
@Override public void rollback(Throwable cause) { if (logger.isDebugEnabled()) { logger.debug("Rolling back Unit Of Work.", cause); } Assert.state(isActive() && phase().isBefore(Phase.ROLLBACK), () -> String.format("The UnitOfWork is in an incompatible phase: %s", phase())); Assert.state(isCurrent(), () -> "The UnitOfWork is not the current Unit of Work"); try { setRollbackCause(cause); changePhase(Phase.ROLLBACK); if (isRoot()) { changePhase(Phase.CLEANUP, Phase.CLOSED); } } finally { CurrentUnitOfWork.clear(this); } }
@Override public void commit() { if (logger.isDebugEnabled()) { logger.debug("Committing Unit Of Work"); } Assert.state(phase() == Phase.STARTED, () -> String.format("The UnitOfWork is in an incompatible phase: %s", phase())); Assert.state(isCurrent(), () -> "The UnitOfWork is not the current Unit of Work"); try { if (isRoot()) { commitAsRoot(); } else { commitAsNested(); } } finally { CurrentUnitOfWork.clear(this); } }
/** * Ask the unit of work to transition to the given {@code phases} sequentially. In each of the phases the * unit of work is responsible for invoking the handlers attached to each phase. * <p/> * By default this sets the Phase and invokes the handlers attached to the phase. * * @param phases The phases to transition to in sequential order */ protected void changePhase(Phase... phases) { for (Phase phase : phases) { setPhase(phase); notifyHandlers(phase); } }
private void commitAsRoot() { try { try { changePhase(Phase.PREPARE_COMMIT, Phase.COMMIT); } catch (Exception e) { setRollbackCause(e); changePhase(Phase.ROLLBACK); throw e; } if (phase() == Phase.COMMIT) { changePhase(Phase.AFTER_COMMIT); } } finally { changePhase(Phase.CLEANUP, Phase.CLOSED); } }
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; } }
@Override public void start() { if (logger.isDebugEnabled()) { logger.debug("Starting Unit Of Work"); } Assert.state(Phase.NOT_STARTED.equals(phase()), () -> "UnitOfWork is already started"); rolledBack = false; onRollback(u -> rolledBack = true); CurrentUnitOfWork.ifStarted(parent -> { // we're nesting. this.parentUnitOfWork = parent; root().onCleanup(r -> changePhase(Phase.CLEANUP, Phase.CLOSED)); }); changePhase(Phase.STARTED); CurrentUnitOfWork.set(this); }
private void delegateAfterCommitToParent(UnitOfWork<?> uow) { Optional<UnitOfWork<?>> parent = uow.parent(); if (parent.isPresent()) { parent.get().afterCommit(this::delegateAfterCommitToParent); } else { changePhase(Phase.AFTER_COMMIT); } }
@Override public MetaData getCorrelationData() { if (correlationDataProviders.isEmpty()) { return MetaData.emptyInstance(); } Map<String, Object> result = new HashMap<>(); for (CorrelationDataProvider correlationDataProvider : correlationDataProviders) { final Map<String, ?> extraData = correlationDataProvider.correlationDataFor(getMessage()); if (extraData != null) { result.putAll(extraData); } } return MetaData.from(result); }
@Override public void start() { if (logger.isDebugEnabled()) { logger.debug("Starting Unit Of Work"); } Assert.state(Phase.NOT_STARTED.equals(phase()), () -> "UnitOfWork is already started"); rolledBack = false; onRollback(u -> rolledBack = true); CurrentUnitOfWork.ifStarted(parent -> { // we're nesting. this.parentUnitOfWork = parent; root().onCleanup(r -> changePhase(Phase.CLEANUP, Phase.CLOSED)); }); changePhase(Phase.STARTED); CurrentUnitOfWork.set(this); }
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; } }
private void commitAsRoot() { try { try { changePhase(Phase.PREPARE_COMMIT, Phase.COMMIT); } catch (Exception e) { setRollbackCause(e); changePhase(Phase.ROLLBACK); throw e; } if (phase() == Phase.COMMIT) { changePhase(Phase.AFTER_COMMIT); } } finally { changePhase(Phase.CLEANUP, Phase.CLOSED); } }
/** * Ask the unit of work to transition to the given {@code phases} sequentially. In each of the phases the * unit of work is responsible for invoking the handlers attached to each phase. * <p/> * By default this sets the Phase and invokes the handlers attached to the phase. * * @param phases The phases to transition to in sequential order */ protected void changePhase(Phase... phases) { for (Phase phase : phases) { setPhase(phase); notifyHandlers(phase); } }
private void delegateAfterCommitToParent(UnitOfWork<?> uow) { Optional<UnitOfWork<?>> parent = uow.parent(); if (parent.isPresent()) { parent.get().afterCommit(this::delegateAfterCommitToParent); } else { changePhase(Phase.AFTER_COMMIT); } }
@Override public MetaData getCorrelationData() { if (correlationDataProviders.isEmpty()) { return MetaData.emptyInstance(); } Map<String, Object> result = new HashMap<>(); for (CorrelationDataProvider correlationDataProvider : correlationDataProviders) { final Map<String, ?> extraData = correlationDataProvider.correlationDataFor(getMessage()); if (extraData != null) { result.putAll(extraData); } } return MetaData.from(result); }
@Override public void rollback(Throwable cause) { if (logger.isDebugEnabled()) { logger.debug("Rolling back Unit Of Work.", cause); } Assert.state(isActive() && phase().isBefore(Phase.ROLLBACK), () -> String.format("The UnitOfWork is in an incompatible phase: %s", phase())); Assert.state(isCurrent(), () -> "The UnitOfWork is not the current Unit of Work"); try { setRollbackCause(cause); changePhase(Phase.ROLLBACK); if (isRoot()) { changePhase(Phase.CLEANUP, Phase.CLOSED); } } finally { CurrentUnitOfWork.clear(this); } }
@Override public void commit() { if (logger.isDebugEnabled()) { logger.debug("Committing Unit Of Work"); } Assert.state(phase() == Phase.STARTED, () -> String.format("The UnitOfWork is in an incompatible phase: %s", phase())); Assert.state(isCurrent(), () -> "The UnitOfWork is not the current Unit of Work"); try { if (isRoot()) { commitAsRoot(); } else { commitAsNested(); } } finally { CurrentUnitOfWork.clear(this); } }
@Override public void start() { if (logger.isDebugEnabled()) { logger.debug("Starting Unit Of Work"); } Assert.state(Phase.NOT_STARTED.equals(phase()), () -> "UnitOfWork is already started"); rolledBack = false; onRollback(u -> rolledBack = true); CurrentUnitOfWork.ifStarted(parent -> { // we're nesting. this.parentUnitOfWork = parent; root().onCleanup(r -> changePhase(Phase.CLEANUP, Phase.CLOSED)); }); changePhase(Phase.STARTED); CurrentUnitOfWork.set(this); }
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; } }
private void commitAsRoot() { try { try { changePhase(Phase.PREPARE_COMMIT, Phase.COMMIT); } catch (Exception e) { setRollbackCause(e); changePhase(Phase.ROLLBACK); throw e; } if (phase() == Phase.COMMIT) { changePhase(Phase.AFTER_COMMIT); } } finally { changePhase(Phase.CLEANUP, Phase.CLOSED); } }