/** * 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(); }
/** * 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()); }
@Override public void publish(List<? extends EventMessage<?>> events) { Stream<MessageMonitor.MonitorCallback> ingested = events.stream().map(messageMonitor::onMessageIngested); if (CurrentUnitOfWork.isStarted()) { UnitOfWork<?> unitOfWork = CurrentUnitOfWork.get(); Assert.state(!unitOfWork.phase().isAfter(PREPARE_COMMIT), () -> "It is not allowed to publish events when the current Unit of Work has already been " + "committed. Please start a new Unit of Work before publishing events."); Assert.state(!unitOfWork.root().phase().isAfter(PREPARE_COMMIT), () -> "It is not allowed to publish events when the root Unit of Work has already been " + "committed."); unitOfWork.afterCommit(u -> ingested.forEach(MessageMonitor.MonitorCallback::reportSuccess)); unitOfWork.onRollback(uow -> ingested.forEach( message -> message.reportFailure(uow.getExecutionResult().getExceptionResult()) )); eventsQueue(unitOfWork).addAll(events); } else { try { prepareCommit(intercept(events)); commit(events); afterCommit(events); ingested.forEach(MessageMonitor.MonitorCallback::reportSuccess); } catch (Exception e) { ingested.forEach(m -> m.reportFailure(e)); throw e; } } }
/** * Register handlers with the current Unit of Work that save or delete the given {@code aggregate} when * the Unit of Work is committed. * * @param aggregate The Aggregate to save or delete when the Unit of Work is committed */ protected void prepareForCommit(A aggregate) { if (UnitOfWork.Phase.STARTED.isBefore(CurrentUnitOfWork.get().phase())) { doCommit(aggregate); } else { CurrentUnitOfWork.get().onPrepareCommit(u -> { // If the aggregate isn't "managed" anymore, it means its state was invalidated by a rollback doCommit(aggregate); }); } }
if (u.parent().isPresent() && !u.parent().get().phase().isAfter(PREPARE_COMMIT)) { eventsQueue(u.parent().get()).addAll(eventQueue); } else { if (u.parent().isPresent() && !u.root().phase().isAfter(COMMIT)) { u.root().onCommit(w -> doWithEvents(this::commit, eventQueue)); } else { if (u.parent().isPresent() && !u.root().phase().isAfter(AFTER_COMMIT)) { u.root().afterCommit(w -> doWithEvents(this::afterCommit, eventQueue)); } else {
@Override public Connection getConnection() throws SQLException { if (!CurrentUnitOfWork.isStarted() || CurrentUnitOfWork.get().phase().isAfter(UnitOfWork.Phase.PREPARE_COMMIT)) { return delegate.getConnection();
/** * 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(); }
/** * 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(); }
/** * 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()); }
/** * 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()); }
if (u.parent().isPresent() && !u.parent().get().phase().isAfter(PREPARE_COMMIT)) { eventsQueue(u.parent().get()).addAll(eventQueue); } else { if (u.parent().isPresent() && !u.root().phase().isAfter(COMMIT)) { u.root().onCommit(w -> doWithEvents(this::commit, eventQueue)); } else { if (u.parent().isPresent() && !u.root().phase().isAfter(AFTER_COMMIT)) { u.root().afterCommit(w -> doWithEvents(this::afterCommit, eventQueue)); } else {
@Override public void publish(List<? extends EventMessage<?>> events) { Stream<MessageMonitor.MonitorCallback> ingested = events.stream().map(messageMonitor::onMessageIngested); if (CurrentUnitOfWork.isStarted()) { UnitOfWork<?> unitOfWork = CurrentUnitOfWork.get(); Assert.state(!unitOfWork.phase().isAfter(PREPARE_COMMIT), () -> "It is not allowed to publish events when the current Unit of Work has already been " + "committed. Please start a new Unit of Work before publishing events."); Assert.state(!unitOfWork.root().phase().isAfter(PREPARE_COMMIT), () -> "It is not allowed to publish events when the root Unit of Work has already been " + "committed."); unitOfWork.afterCommit(u -> ingested.forEach(MessageMonitor.MonitorCallback::reportSuccess)); unitOfWork.onRollback(u -> ingested.forEach(m -> m.reportFailure(u.getExecutionResult().getExceptionResult()))); eventsQueue(unitOfWork).addAll(events); } else { try { prepareCommit(intercept(events)); commit(events); afterCommit(events); ingested.forEach(MessageMonitor.MonitorCallback::reportSuccess); } catch (Exception e) { ingested.forEach(m -> m.reportFailure(e)); throw e; } } }
@Override public void publish(List<? extends EventMessage<?>> events) { Stream<MessageMonitor.MonitorCallback> ingested = events.stream().map(messageMonitor::onMessageIngested); if (CurrentUnitOfWork.isStarted()) { UnitOfWork<?> unitOfWork = CurrentUnitOfWork.get(); Assert.state(!unitOfWork.phase().isAfter(PREPARE_COMMIT), () -> "It is not allowed to publish events when the current Unit of Work has already been " + "committed. Please start a new Unit of Work before publishing events."); Assert.state(!unitOfWork.root().phase().isAfter(PREPARE_COMMIT), () -> "It is not allowed to publish events when the root Unit of Work has already been " + "committed."); unitOfWork.afterCommit(u -> ingested.forEach(MessageMonitor.MonitorCallback::reportSuccess)); unitOfWork.onRollback(uow -> ingested.forEach( message -> message.reportFailure(uow.getExecutionResult().getExceptionResult()) )); eventsQueue(unitOfWork).addAll(events); } else { try { prepareCommit(intercept(events)); commit(events); afterCommit(events); ingested.forEach(MessageMonitor.MonitorCallback::reportSuccess); } catch (Exception e) { ingested.forEach(m -> m.reportFailure(e)); throw e; } } }
if (u.parent().isPresent() && !u.parent().get().phase().isAfter(PREPARE_COMMIT)) { eventsQueue(u.parent().get()).addAll(eventQueue); } else { if (u.parent().isPresent() && !u.root().phase().isAfter(COMMIT)) { u.root().onCommit(w -> doWithEvents(this::commit, eventQueue)); } else { if (u.parent().isPresent() && !u.root().phase().isAfter(AFTER_COMMIT)) { u.root().afterCommit(w -> doWithEvents(this::afterCommit, eventQueue)); } else {
/** * Register handlers with the current Unit of Work that save or delete the given {@code aggregate} when * the Unit of Work is committed. * * @param aggregate The Aggregate to save or delete when the Unit of Work is committed */ protected void prepareForCommit(A aggregate) { if (UnitOfWork.Phase.STARTED.isBefore(CurrentUnitOfWork.get().phase())) { doCommit(aggregate); } else { CurrentUnitOfWork.get().onPrepareCommit(u -> { // if the aggregate isn't "managed" anymore, it means its state was invalidated by a rollback doCommit(aggregate); }); } }
/** * Register handlers with the current Unit of Work that save or delete the given {@code aggregate} when * the Unit of Work is committed. * * @param aggregate The Aggregate to save or delete when the Unit of Work is committed */ protected void prepareForCommit(A aggregate) { if (UnitOfWork.Phase.STARTED.isBefore(CurrentUnitOfWork.get().phase())) { doCommit(aggregate); } else { CurrentUnitOfWork.get().onPrepareCommit(u -> { // If the aggregate isn't "managed" anymore, it means its state was invalidated by a rollback doCommit(aggregate); }); } }
@Override public Connection getConnection() throws SQLException { if (!CurrentUnitOfWork.isStarted() || CurrentUnitOfWork.get().phase().isAfter(UnitOfWork.Phase.PREPARE_COMMIT)) { return delegate.getConnection();
@Override public Connection getConnection() throws SQLException { if (!CurrentUnitOfWork.isStarted() || CurrentUnitOfWork.get().phase().isAfter(UnitOfWork.Phase.PREPARE_COMMIT)) { return delegate.getConnection();