@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; }
@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; }
@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); }); }
throw exception; }).proceed(); }, rollbackConfiguration);
@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; }
@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; }
@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; }
@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; }
@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); }); }
throw throwable; }).proceed(); }, rollbackConfiguration); } catch (Exception e) {
throw exception; }).proceed(); }, rollbackConfiguration);