/** * Set the execution result of processing the current {@link #getMessage() Message}. In case this context has a * previously set ExecutionResult, setting a new result is only allowed if the new result is an exception result. * <p/> * In case the previously set result is also an exception result, the exception in the new execution result is * added to the original exception as a suppressed exception. * * @param executionResult the ExecutionResult of the currently handled Message */ @SuppressWarnings("ThrowableResultOfMethodCallIgnored") public void setExecutionResult(ExecutionResult executionResult) { Assert.state(this.executionResult == null || executionResult.isExceptionResult(), () -> String.format("Cannot change execution result [%s] to [%s] for message [%s].", message, this.executionResult, executionResult)); if (this.executionResult != null && this.executionResult.isExceptionResult()) { this.executionResult.getExceptionResult().addSuppressed(executionResult.getExceptionResult()); } else { this.executionResult = executionResult; } }
@Override public BiFunction<Integer, EventMessage<?>, EventMessage<?>> handle(List<? extends EventMessage<?>> messages) { StringBuilder sb = new StringBuilder(String.format("Events published: [%s]", messages.stream().map(m -> m.getPayloadType().getSimpleName()).collect(Collectors.joining(", ")))); CurrentUnitOfWork.ifStarted(unitOfWork -> { Message<?> message = unitOfWork.getMessage(); if (message == null) { sb.append(" while processing an operation not tied to an incoming message"); } else { sb.append(String.format(" while processing a [%s]", message.getPayloadType().getSimpleName())); } ExecutionResult executionResult = unitOfWork.getExecutionResult(); if (executionResult != null) { if (executionResult.isExceptionResult()) { @SuppressWarnings("ThrowableResultOfMethodCallIgnored") Throwable exception = executionResult.getExceptionResult(); exception = exception instanceof ExecutionException ? exception.getCause() : exception; sb.append(String.format(" which failed with a [%s]", exception.getClass().getSimpleName())); } else if (executionResult.getResult() != null) { sb.append(String.format(" which yielded a [%s] return value", executionResult.getResult().getClass().getSimpleName())); } } }); logger.info(sb.toString()); return (i, m) -> m; } }
@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; } } }
@Override protected void appendEvents(List<? extends EventMessage<?>> events, Serializer serializer) { AppendEventTransaction sender; if (CurrentUnitOfWork.isStarted()) { sender = CurrentUnitOfWork.get().root().getOrComputeResource(APPEND_EVENT_TRANSACTION, k -> { AppendEventTransaction appendEventTransaction = eventStoreClient.createAppendEventConnection(); CurrentUnitOfWork.get().root().onRollback( u -> appendEventTransaction.rollback(u.getExecutionResult().getExceptionResult()) ); CurrentUnitOfWork.get().root().onCommit(u -> commit(appendEventTransaction)); return appendEventTransaction; }); } else { sender = eventStoreClient.createAppendEventConnection(); } for (EventMessage<?> eventMessage : events) { sender.append(map(eventMessage, serializer)); } if (!CurrentUnitOfWork.isStarted()) { commit(sender); } }
/** * Set the execution result of processing the current {@link #getMessage() Message}. In case this context has a * previously set ExecutionResult, setting a new result is only allowed if the new result is an exception result. * <p/> * In case the previously set result is also an exception result, the exception in the new execution result is * added to the original exception as a suppressed exception. * * @param executionResult the ExecutionResult of the currently handled Message */ @SuppressWarnings("ThrowableResultOfMethodCallIgnored") public void setExecutionResult(ExecutionResult executionResult) { Assert.state(this.executionResult == null || executionResult.isExceptionResult(), () -> String.format("Cannot change execution result [%s] to [%s] for message [%s].", message, this.executionResult, executionResult)); if (this.executionResult != null && this.executionResult.isExceptionResult()) { this.executionResult.getExceptionResult().addSuppressed(executionResult.getExceptionResult()); } else { this.executionResult = executionResult; } }
/** * Set the execution result of processing the current {@link #getMessage() Message}. In case this context has a * previously set ExecutionResult, setting a new result is only allowed if the new result is an exception result. * <p/> * In case the previously set result is also an exception result, the exception in the new execution result is * added to the original exception as a suppressed exception. * * @param executionResult the ExecutionResult of the currently handled Message */ @SuppressWarnings("ThrowableResultOfMethodCallIgnored") public void setExecutionResult(ExecutionResult executionResult) { Assert.state(this.executionResult == null || executionResult.isExceptionResult(), () -> String.format("Cannot change execution result [%s] to [%s] for message [%s].", message, this.executionResult, executionResult)); if (this.executionResult != null && this.executionResult.isExceptionResult()) { this.executionResult.getExceptionResult().addSuppressed(executionResult.getExceptionResult()); } else { this.executionResult = executionResult; } }
@Override public BiFunction<Integer, EventMessage<?>, EventMessage<?>> handle(List<? extends EventMessage<?>> messages) { StringBuilder sb = new StringBuilder(String.format("Events published: [%s]", messages.stream().map(m -> m.getPayloadType().getSimpleName()).collect(Collectors.joining(", ")))); CurrentUnitOfWork.ifStarted(unitOfWork -> { Message<?> message = unitOfWork.getMessage(); if (message == null) { sb.append(" while processing an operation not tied to an incoming message"); } else { sb.append(String.format(" while processing a [%s]", message.getPayloadType().getSimpleName())); } ExecutionResult executionResult = unitOfWork.getExecutionResult(); if (executionResult != null) { if (executionResult.isExceptionResult()) { @SuppressWarnings("ThrowableResultOfMethodCallIgnored") Throwable exception = executionResult.getExceptionResult(); exception = exception instanceof ExecutionException ? exception.getCause() : exception; sb.append(String.format(" which failed with a [%s]", exception.getClass().getSimpleName())); } else if (executionResult.getResult() != null) { sb.append(String.format(" which yielded a [%s] return value", executionResult.getResult().getClass().getSimpleName())); } } }); logger.info(sb.toString()); return (i, m) -> m; } }
@Override public BiFunction<Integer, EventMessage<?>, EventMessage<?>> handle(List<? extends EventMessage<?>> messages) { StringBuilder sb = new StringBuilder(String.format("Events published: [%s]", messages.stream().map(m -> m.getPayloadType().getSimpleName()).collect(Collectors.joining(", ")))); CurrentUnitOfWork.ifStarted(unitOfWork -> { Message<?> message = unitOfWork.getMessage(); if (message == null) { sb.append(" while processing an operation not tied to an incoming message"); } else { sb.append(String.format(" while processing a [%s]", message.getPayloadType().getSimpleName())); } ExecutionResult executionResult = unitOfWork.getExecutionResult(); if (executionResult != null) { if (executionResult.isExceptionResult()) { @SuppressWarnings("ThrowableResultOfMethodCallIgnored") Throwable exception = executionResult.getExceptionResult(); exception = exception instanceof ExecutionException ? exception.getCause() : exception; sb.append(String.format(" which failed with a [%s]", exception.getClass().getSimpleName())); } else if (executionResult.getResult() != null) { sb.append(String.format(" which yielded a [%s] return value", executionResult.getResult().getClass().getSimpleName())); } } }); logger.info(sb.toString()); return (i, m) -> m; } }
@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; } } }
@Override protected void appendEvents(List<? extends EventMessage<?>> events, Serializer serializer) { AppendEventTransaction sender; if (CurrentUnitOfWork.isStarted()) { sender = CurrentUnitOfWork.get().root().getOrComputeResource(APPEND_EVENT_TRANSACTION, k -> { AppendEventTransaction appendEventTransaction = eventStoreClient.createAppendEventConnection(); CurrentUnitOfWork.get().root().onRollback( u -> appendEventTransaction.rollback(u.getExecutionResult().getExceptionResult()) ); CurrentUnitOfWork.get().root().onCommit(u -> commit(appendEventTransaction)); return appendEventTransaction; }); } else { sender = eventStoreClient.createAppendEventConnection(); } for (EventMessage<?> eventMessage : events) { sender.append(map(eventMessage, serializer)); } if (!CurrentUnitOfWork.isStarted()) { commit(sender); } }