@SuppressWarnings("unchecked") @Override public Object handle(Message<?> message, P target) throws Exception { C childEntity = childEntityResolver.apply((CommandMessage<?>) message, target); if (childEntity == null) { throw new AggregateEntityNotFoundException( "Aggregate cannot handle this command, as there is no entity instance to forward it to." ); } List<AnnotatedCommandHandlerInterceptor<? super C>> interceptors = childHandlingInterceptors.stream() .filter(chi -> chi.canHandle(message)) .sorted((chi1, chi2) -> Integer.compare(chi2.priority(), chi1.priority())) .map(chi -> new AnnotatedCommandHandlerInterceptor<>(chi, childEntity)) .collect(Collectors.toList()); Object result; if (interceptors.isEmpty()) { result = childHandler.handle(message, childEntity); } else { result = new DefaultInterceptorChain<>((UnitOfWork<CommandMessage<?>>) CurrentUnitOfWork.get(), interceptors, m -> childHandler.handle(message, childEntity)).proceed(); } return result; }
this.result = null; this.aggregateIdentifier = null; this.invocationInterceptorChain = new DefaultInterceptorChain<>( this, invokerInterceptors, newCommandHandler ); this.publisherInterceptorChain = new DefaultInterceptorChain<>( this, publisherInterceptors, repeatingCommandHandler
/** * 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); }
@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); }); }
@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)); } }
@SuppressWarnings("unchecked") private Object handle(CommandMessage<?> commandMessage) throws Exception { List<AnnotatedCommandHandlerInterceptor<? super T>> interceptors = inspector.commandHandlerInterceptors() .stream() .filter(chi -> chi.canHandle(commandMessage)) .sorted((chi1, chi2) -> Integer.compare(chi2.priority(), chi1.priority())) .map(chi -> new AnnotatedCommandHandlerInterceptor<>(chi, aggregateRoot)) .collect(Collectors.toList()); MessageHandlingMember<? super T> handler = inspector.commandHandlers() .stream() .filter(mh -> mh.canHandle(commandMessage)) .findFirst() .orElseThrow(() -> new NoHandlerForCommandException(format("No handler available to handle command [%s]", commandMessage.getCommandName()))); Object result; if (interceptors.isEmpty()) { result = handler.handle(commandMessage, aggregateRoot); } else { result = new DefaultInterceptorChain<>( (UnitOfWork<CommandMessage<?>>) CurrentUnitOfWork.get(), interceptors, m -> handler.handle(commandMessage, aggregateRoot) ).proceed(); } if (aggregateRoot == null) { aggregateRoot = (T) result; return identifierAsString(); } return result; }
unitOfWork.attachTransaction(transactionManager); InterceptorChain chain = new DefaultInterceptorChain<>(unitOfWork, handlerInterceptors, interceptedDeadlineMessage -> {
MessageMonitor.MonitorCallback monitorCallback = messageMonitor.onMessageIngested(unitOfWork.getMessage()); return new DefaultInterceptorChain<>(unitOfWork, interceptors, m -> { try { eventHandlerInvoker.handle(m, segment); throw exception; }).proceed(); }, rollbackConfiguration);
private DeadlineMessage<?> consumeDeadline(DeadlineConsumer deadlineConsumer, ScheduledDeadlineInfo scheduledDeadlineInfo) { DefaultUnitOfWork<? extends DeadlineMessage<?>> uow = DefaultUnitOfWork.startAndGet(scheduledDeadlineInfo.deadlineMessage()); InterceptorChain chain = new DefaultInterceptorChain<>(uow, handlerInterceptors, deadlineMessage -> { deadlineConsumer.consume(scheduledDeadlineInfo.getDeadlineScope(), deadlineMessage); return deadlineMessage; }); ResultMessage<?> resultMessage = uow.executeWithResult(chain::proceed); if (resultMessage.isExceptional()) { Throwable e = resultMessage.exceptionResult(); throw new FixtureExecutionException("Exception occurred while handling the deadline", e); } return (DeadlineMessage<?>) resultMessage.getPayload(); } }
@SuppressWarnings("unchecked") @Override public Object handle(Message<?> message, P target) throws Exception { C childEntity = childEntityResolver.apply((CommandMessage<?>) message, target); if (childEntity == null) { throw new AggregateEntityNotFoundException( "Aggregate cannot handle this command, as there is no entity instance to forward it to." ); } List<AnnotatedCommandHandlerInterceptor<? super C>> interceptors = childHandlingInterceptors.stream() .filter(chi -> chi.canHandle(message)) .sorted((chi1, chi2) -> Integer.compare(chi2.priority(), chi1.priority())) .map(chi -> new AnnotatedCommandHandlerInterceptor<>(chi, childEntity)) .collect(Collectors.toList()); Object result; if (interceptors.isEmpty()) { result = childHandler.handle(message, childEntity); } else { result = new DefaultInterceptorChain<>((UnitOfWork<CommandMessage<?>>) CurrentUnitOfWork.get(), interceptors, m -> childHandler.handle(message, childEntity)).proceed(); } return result; }
this.exceptionResult = null; this.aggregateIdentifier = null; this.invocationInterceptorChain = new DefaultInterceptorChain<>( this, invokerInterceptors, newCommandHandler ); this.publisherInterceptorChain = new DefaultInterceptorChain<>( this, publisherInterceptors, repeatingCommandHandler
@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); }); }
this.result = null; this.aggregateIdentifier = null; this.invocationInterceptorChain = new DefaultInterceptorChain<>( this, invokerInterceptors, newCommandHandler ); this.publisherInterceptorChain = new DefaultInterceptorChain<>( this, publisherInterceptors, repeatingCommandHandler
@SuppressWarnings("unchecked") @Override public Object handle(Message<?> message, P target) throws Exception { C childEntity = childEntityResolver.apply((CommandMessage<?>) message, target); if (childEntity == null) { throw new AggregateEntityNotFoundException( "Aggregate cannot handle this command, as there is no entity instance to forward it to." ); } List<AnnotatedCommandHandlerInterceptor<? super C>> interceptors = childHandlingInterceptors.stream() .filter(chi -> chi.canHandle(message)) .sorted((chi1, chi2) -> Integer.compare(chi2.priority(), chi1.priority())) .map(chi -> new AnnotatedCommandHandlerInterceptor<>(chi, childEntity)) .collect(Collectors.toList()); Object result; if (interceptors.isEmpty()) { result = childHandler.handle(message, childEntity); } else { result = new DefaultInterceptorChain<>((UnitOfWork<CommandMessage<?>>) CurrentUnitOfWork.get(), interceptors, m -> childHandler.handle(message, childEntity)).proceed(); } return result; }
/** * 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); }
@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, 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); } }
@SuppressWarnings("unchecked") private Object handle(CommandMessage<?> commandMessage) throws Exception { List<AnnotatedCommandHandlerInterceptor<? super T>> interceptors = inspector.commandHandlerInterceptors() .stream() .filter(chi -> chi.canHandle(commandMessage)) .sorted((chi1, chi2) -> Integer.compare(chi2.priority(), chi1.priority())) .map(chi -> new AnnotatedCommandHandlerInterceptor<>(chi, aggregateRoot)) .collect(Collectors.toList()); MessageHandlingMember<? super T> handler = inspector.commandHandlers() .get(commandMessage.getCommandName()); Object result; if (interceptors.isEmpty()) { result = handler.handle(commandMessage, aggregateRoot); } else { result = new DefaultInterceptorChain<>( (UnitOfWork<CommandMessage<?>>) CurrentUnitOfWork.get(), interceptors, m -> handler.handle(commandMessage, aggregateRoot) ).proceed(); } if (aggregateRoot == null) { aggregateRoot = (T) result; return identifierAsString(); } return result; }
@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)); } }
@SuppressWarnings("unchecked") private Object handle(CommandMessage<?> commandMessage) throws Exception { List<AnnotatedCommandHandlerInterceptor<? super T>> interceptors = inspector.commandHandlerInterceptors() .stream() .filter(chi -> chi.canHandle(commandMessage)) .sorted((chi1, chi2) -> Integer.compare(chi2.priority(), chi1.priority())) .map(chi -> new AnnotatedCommandHandlerInterceptor<>(chi, aggregateRoot)) .collect(Collectors.toList()); MessageHandlingMember<? super T> handler = inspector.commandHandlers() .stream() .filter(mh -> mh.canHandle(commandMessage)) .findFirst() .orElseThrow(() -> new NoHandlerForCommandException(format("No handler available to handle command [%s]", commandMessage.getCommandName()))); Object result; if (interceptors.isEmpty()) { result = handler.handle(commandMessage, aggregateRoot); } else { result = new DefaultInterceptorChain<>( (UnitOfWork<CommandMessage<?>>) CurrentUnitOfWork.get(), interceptors, m -> handler.handle(commandMessage, aggregateRoot) ).proceed(); } if (aggregateRoot == null) { aggregateRoot = (T) result; return identifierAsString(); } return result; }