/** * Execute the given {@code task} in the context of this Unit of Work. If the Unit of Work is not started yet * it will be started. * <p/> * If the task executes successfully the Unit of Work is committed and the result of the task is returned. If any * exception is raised while executing the task, the Unit of Work is rolled back and the exception is thrown. * * @param <R> the type of result that is returned after successful execution * @param task the task to execute * @return The result of the task wrapped in Result Message */ default <R> ResultMessage<R> executeWithResult(Callable<R> task) { return executeWithResult(task, RollbackConfigurationType.ANY_THROWABLE); }
/** * Execute the given {@code task} in the context of this Unit of Work. If the Unit of Work is not started yet * it will be started. * <p/> * If the task executes successfully the Unit of Work is committed. If an exception is raised while executing the * task, the {@code rollbackConfiguration} determines if the Unit of Work should be rolled back or committed, * and the exception is thrown. * * @param task the task to execute * @param rollbackConfiguration configuration that determines whether or not to rollback the unit of work when task * execution fails */ default void execute(Runnable task, RollbackConfiguration rollbackConfiguration) { ResultMessage<?> resultMessage = executeWithResult(() -> { task.run(); return null; }, rollbackConfiguration); if (resultMessage.isExceptional()) { throw (RuntimeException) resultMessage.exceptionResult(); } }
@SuppressWarnings("unchecked") private <Q, R> ResultMessage<CompletableFuture<QueryResponseMessage<R>>> interceptAndInvoke( UnitOfWork<QueryMessage<Q, R>> uow, MessageHandler<? super QueryMessage<?, R>> handler) { return uow.executeWithResult(() -> { ResponseType<R> responseType = uow.getMessage().getResponseType(); Object queryResponse = new DefaultInterceptorChain<>(uow, handlerInterceptors, handler).proceed(); if (queryResponse instanceof CompletableFuture) { return ((CompletableFuture) queryResponse).thenCompose( result -> buildCompletableFuture(responseType, result)); } else if (queryResponse instanceof Future) { return CompletableFuture.supplyAsync(() -> { try { return ((Future<QueryResponseMessage<R>>) queryResponse).get(); } catch (InterruptedException | ExecutionException e) { throw new QueryExecutionException("Error happened while trying to execute query handler", e); } }); } return buildCompletableFuture(responseType, queryResponse); }); }
/** * Performs the actual handling logic. * * @param command The actual command to handle * @param handler The handler that must be invoked for this command * @param callback The callback to notify of the result * @param <C> The type of payload of the command * @param <R> The type of result expected from the command handler */ @SuppressWarnings({"unchecked"}) protected <C, R> void handle(CommandMessage<C> command, MessageHandler<? super CommandMessage<?>> handler, CommandCallback<? super C, ? super R> callback) { if (logger.isDebugEnabled()) { logger.debug("Handling command [{}]", command.getCommandName()); } UnitOfWork<CommandMessage<?>> unitOfWork = DefaultUnitOfWork.startAndGet(command); unitOfWork.attachTransaction(transactionManager); InterceptorChain chain = new DefaultInterceptorChain<>(unitOfWork, handlerInterceptors, handler); CommandResultMessage<R> resultMessage = asCommandResultMessage(unitOfWork.executeWithResult(chain::proceed, rollbackConfiguration)); callback.onResult(command, resultMessage); }
@Override public void run() { if (logger.isDebugEnabled()) { logger.debug("Triggered deadline"); } try { Instant triggerInstant = GenericEventMessage.clock.instant(); UnitOfWork<DeadlineMessage<?>> unitOfWork = new DefaultUnitOfWork<>( new GenericDeadlineMessage<>(deadlineName, deadlineMessage, () -> triggerInstant)); unitOfWork.attachTransaction(transactionManager); InterceptorChain chain = new DefaultInterceptorChain<>(unitOfWork, handlerInterceptors(), deadlineMessage -> { executeScheduledDeadline(deadlineMessage, deadlineScope); return null; }); ResultMessage<?> resultMessage = unitOfWork.executeWithResult(chain::proceed); if (resultMessage.isExceptional()) { Throwable e = resultMessage.exceptionResult(); throw new DeadlineException(format("An error occurred while triggering the deadline %s %s", deadlineName, deadlineId), e); } } finally { scheduledTasks.remove(new DeadlineId(deadlineName, deadlineId)); } }
UnitOfWork<? extends EventMessage<?>> unitOfWork, Segment segment) throws Exception { ResultMessage<?> resultMessage = unitOfWork.executeWithResult(() -> { MessageMonitor.MonitorCallback monitorCallback = messageMonitor.onMessageIngested(unitOfWork.getMessage());
/** * Execute the given {@code task} in the context of this Unit of Work. If the Unit of Work is not started yet * it will be started. * <p/> * If the task executes successfully the Unit of Work is committed and the result of the task is returned. If any * exception is raised while executing the task, the Unit of Work is rolled back and the exception is thrown. * * @param <R> the type of result that is returned after successful execution * @param task the task to execute * @return The result of the task wrapped in Result Message */ default <R> ResultMessage<R> executeWithResult(Callable<R> task) { return executeWithResult(task, RollbackConfigurationType.ANY_THROWABLE); }
/** * Execute the given {@code task} in the context of this Unit of Work. If the Unit of Work is not started yet * it will be started. * <p/> * If the task executes successfully the Unit of Work is committed and the result of the task is returned. If any * exception is raised while executing the task, the Unit of Work is rolled back and the exception is thrown. * * @param <R> the type of result that is returned after successful execution * @param task the task to execute * @return The result of the task * @throws Exception if an Exception was raised while executing the task */ default <R> R executeWithResult(Callable<R> task) throws Exception { return executeWithResult(task, RollbackConfigurationType.ANY_THROWABLE); }
/** * Execute the given {@code task} in the context of this Unit of Work. If the Unit of Work is not started yet * it will be started. * <p/> * If the task executes successfully the Unit of Work is committed. If an exception is raised while executing the * task, the {@code rollbackConfiguration} determines if the Unit of Work should be rolled back or committed, * and the exception is thrown. * * @param task the task to execute * @param rollbackConfiguration configuration that determines whether or not to rollback the unit of work when task * execution fails */ default void execute(Runnable task, RollbackConfiguration rollbackConfiguration) { try { executeWithResult(() -> { task.run(); return null; }, rollbackConfiguration); } catch (Exception e) { throw (RuntimeException) e; } }
/** * Execute the given {@code task} in the context of this Unit of Work. If the Unit of Work is not started yet * it will be started. * <p/> * If the task executes successfully the Unit of Work is committed. If an exception is raised while executing the * task, the {@code rollbackConfiguration} determines if the Unit of Work should be rolled back or committed, * and the exception is thrown. * * @param task the task to execute * @param rollbackConfiguration configuration that determines whether or not to rollback the unit of work when task * execution fails */ default void execute(Runnable task, RollbackConfiguration rollbackConfiguration) { ResultMessage<?> resultMessage = executeWithResult(() -> { task.run(); return null; }, rollbackConfiguration); if (resultMessage.isExceptional()) { throw (RuntimeException) resultMessage.exceptionResult(); } }
@SuppressWarnings("unchecked") private <Q, R> ResultMessage<CompletableFuture<QueryResponseMessage<R>>> interceptAndInvoke( UnitOfWork<QueryMessage<Q, R>> uow, MessageHandler<? super QueryMessage<?, R>> handler) { return uow.executeWithResult(() -> { ResponseType<R> responseType = uow.getMessage().getResponseType(); Object queryResponse = new DefaultInterceptorChain<>(uow, handlerInterceptors, handler).proceed(); if (queryResponse instanceof CompletableFuture) { return ((CompletableFuture) queryResponse).thenCompose( result -> buildCompletableFuture(responseType, result)); } else if (queryResponse instanceof Future) { return CompletableFuture.supplyAsync(() -> { try { return ((Future<QueryResponseMessage<R>>) queryResponse).get(); } catch (InterruptedException | ExecutionException e) { throw new QueryExecutionException("Error happened while trying to execute query handler", e); } }); } return buildCompletableFuture(responseType, queryResponse); }); }
@SuppressWarnings("unchecked") private <Q, R> CompletableFuture<QueryResponseMessage<R>> interceptAndInvoke(UnitOfWork<QueryMessage<Q, R>> uow, MessageHandler<? super QueryMessage<?, R>> handler) throws Exception { return uow.executeWithResult(() -> { ResponseType<R> responseType = uow.getMessage().getResponseType(); Object queryResponse = new DefaultInterceptorChain<>(uow, handlerInterceptors, handler).proceed(); if (queryResponse instanceof CompletableFuture) { return ((CompletableFuture) queryResponse).thenCompose( result -> buildCompletableFuture(responseType, result)); } else if (queryResponse instanceof Future) { return CompletableFuture.supplyAsync(() -> { try { return ((Future) queryResponse).get(); } catch (InterruptedException | ExecutionException e) { throw new QueryExecutionException("Error happened while trying to execute query handler", e); } }); } return buildCompletableFuture(responseType, queryResponse); }); }
/** * Performs the actual handling logic. * * @param command The actual command to handle * @param handler The handler that must be invoked for this command * @param callback The callback to notify of the result * @param <C> The type of payload of the command * @param <R> The type of result expected from the command handler */ @SuppressWarnings({"unchecked"}) protected <C, R> void handle(CommandMessage<C> command, MessageHandler<? super CommandMessage<?>> handler, CommandCallback<? super C, ? super R> callback) { if (logger.isDebugEnabled()) { logger.debug("Handling command [{}]", command.getCommandName()); } UnitOfWork<CommandMessage<?>> unitOfWork = DefaultUnitOfWork.startAndGet(command); unitOfWork.attachTransaction(transactionManager); InterceptorChain chain = new DefaultInterceptorChain<>(unitOfWork, handlerInterceptors, handler); CommandResultMessage<R> resultMessage = asCommandResultMessage(unitOfWork.executeWithResult(chain::proceed, rollbackConfiguration)); callback.onResult(command, resultMessage); }
/** * Performs the actual handling logic. * * @param command The actual command to handle * @param handler The handler that must be invoked for this command * @param callback The callback to notify of the result * @param <C> The type of payload of the command * @param <R> The type of result expected from the command handler */ @SuppressWarnings({"unchecked"}) protected <C, R> void handle(CommandMessage<C> command, MessageHandler<? super CommandMessage<?>> handler, CommandCallback<? super C, R> callback) { if (logger.isDebugEnabled()) { logger.debug("Handling command [{}]", command.getCommandName()); } try { UnitOfWork<CommandMessage<?>> unitOfWork = DefaultUnitOfWork.startAndGet(command); unitOfWork.attachTransaction(transactionManager); InterceptorChain chain = new DefaultInterceptorChain<>(unitOfWork, handlerInterceptors, handler); R result = (R) unitOfWork.executeWithResult(chain::proceed, rollbackConfiguration); callback.onSuccess(command, result); } catch (Exception e) { callback.onFailure(command, e); } }
@Override public void run() { if (logger.isDebugEnabled()) { logger.debug("Triggered deadline"); } try { Instant triggerInstant = GenericEventMessage.clock.instant(); UnitOfWork<DeadlineMessage<?>> unitOfWork = new DefaultUnitOfWork<>( new GenericDeadlineMessage<>(deadlineName, deadlineMessage, () -> triggerInstant)); unitOfWork.attachTransaction(transactionManager); InterceptorChain chain = new DefaultInterceptorChain<>(unitOfWork, handlerInterceptors(), deadlineMessage -> { executeScheduledDeadline(deadlineMessage, deadlineScope); return null; }); ResultMessage<?> resultMessage = unitOfWork.executeWithResult(chain::proceed); if (resultMessage.isExceptional()) { Throwable e = resultMessage.exceptionResult(); throw new DeadlineException(format("An error occurred while triggering the deadline %s %s", deadlineName, deadlineId), e); } } finally { scheduledTasks.remove(new DeadlineId(deadlineName, deadlineId)); } }
Segment segment) throws Exception { try { unitOfWork.executeWithResult(() -> { MessageMonitor.MonitorCallback monitorCallback = messageMonitor.onMessageIngested(unitOfWork.getMessage());
UnitOfWork<? extends EventMessage<?>> unitOfWork, Segment segment) throws Exception { ResultMessage<?> resultMessage = unitOfWork.executeWithResult(() -> { MessageMonitor.MonitorCallback monitorCallback = messageMonitor.onMessageIngested(unitOfWork.getMessage());