@Override public Object proceed() throws Exception { if (chain.hasNext()) { return chain.next().handle(unitOfWork, this); } else { return handler.handle(unitOfWork.getMessage()); } } }
@Override public Object handle(UnitOfWork<? extends T> unitOfWork, InterceptorChain interceptorChain) throws Exception { handle(unitOfWork.getMessage()); return interceptorChain.proceed(); }
@Override public Object handle(UnitOfWork<? extends T> unitOfWork, InterceptorChain interceptorChain) throws Exception { T message = unitOfWork.getMessage(); logger.info("Incoming message: [{}]", message.getPayloadType().getSimpleName()); try { Object returnValue = interceptorChain.proceed(); logger.info("[{}] executed successfully with a [{}] return value", message.getPayloadType().getSimpleName(), returnValue == null ? "null" : returnValue.getClass().getSimpleName()); return returnValue; } catch (Exception t) { logger.warn(format("[%s] execution failed:", message.getPayloadType().getSimpleName()), t); throw t; } } }
@Override public Object handle(UnitOfWork<? extends CommandMessage<?>> unitOfWork, InterceptorChain interceptorChain) throws Exception { InterceptorChainParameterResolverFactory.initialize(interceptorChain); Object result = delegate.handle(unitOfWork.getMessage(), target); if (delegate.unwrap(CommandHandlerInterceptorHandlingMember.class) .map(CommandHandlerInterceptorHandlingMember::shouldInvokeInterceptorChain).orElse(false)) { result = interceptorChain.proceed(); } return result; } }
@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; } }
@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); }); }
@Test public void testFirstTokenIsStoredWhenUnitOfWorkIsRolledBackOnSecondEvent() throws Exception { List<? extends EventMessage<?>> events = createEvents(2); CountDownLatch countDownLatch = new CountDownLatch(2); testSubject.registerHandlerInterceptor(((unitOfWork, interceptorChain) -> { unitOfWork.onCommit(uow -> { if (uow.getMessage().equals(events.get(1))) { throw new MockException(); } }); return interceptorChain.proceed(); })); testSubject.registerHandlerInterceptor(((unitOfWork, interceptorChain) -> { unitOfWork.onCleanup(uow -> countDownLatch.countDown()); return interceptorChain.proceed(); })); testSubject.start(); // give it a bit of time to start Thread.sleep(200); eventBus.publish(events); assertTrue("Expected Unit of Work to have reached clean up phase", countDownLatch.await(5, TimeUnit.SECONDS)); verify(tokenStore, atLeastOnce()).storeToken(any(), any(), anyInt()); assertNotNull(tokenStore.fetchToken(testSubject.getName(), 0)); }
ResultMessage<?> resultMessage = unitOfWork.executeWithResult(() -> { MessageMonitor.MonitorCallback monitorCallback = messageMonitor.onMessageIngested(unitOfWork.getMessage()); return new DefaultInterceptorChain<>(unitOfWork, interceptors, m -> { try {
if (uow.getMessage().equals(events.get(1))) { throw new MockException();
@Test public void testMultiThreadTokensAreStoredWhenUnitOfWorkIsRolledBackOnSecondEvent() throws Exception { List<? extends EventMessage<?>> events = createEvents(2); CountDownLatch countDownLatch = new CountDownLatch(2); //noinspection Duplicates testSubject.registerHandlerInterceptor(((unitOfWork, interceptorChain) -> { unitOfWork.onCommit(uow -> { if (uow.getMessage().equals(events.get(1))) { throw new MockException(); } }); return interceptorChain.proceed(); })); testSubject.registerHandlerInterceptor(((unitOfWork, interceptorChain) -> { unitOfWork.onCleanup(uow -> countDownLatch.countDown()); return interceptorChain.proceed(); })); testSubject.start(); eventBus.publish(events); assertTrue("Expected Unit of Work to have reached clean up phase", countDownLatch.await(5, SECONDS)); assertNotNull(tokenStore.fetchToken(testSubject.getName(), 0)); assertNotNull(tokenStore.fetchToken(testSubject.getName(), 1)); }
@Override public Object proceed() throws Exception { if (chain.hasNext()) { return chain.next().handle(unitOfWork, this); } else { return handler.handle(unitOfWork.getMessage()); } } }
@Override public Object proceed() throws Exception { if (chain.hasNext()) { return chain.next().handle(unitOfWork, this); } else { return handler.handle(unitOfWork.getMessage()); } } }
@Override public Object handle(UnitOfWork<? extends T> unitOfWork, InterceptorChain interceptorChain) throws Exception { T message = unitOfWork.getMessage(); logger.info("Incoming message: [{}]", message.getPayloadType().getSimpleName()); try { Object returnValue = interceptorChain.proceed(); logger.info("[{}] executed successfully with a [{}] return value", message.getPayloadType().getSimpleName(), returnValue == null ? "null" : returnValue.getClass().getSimpleName()); return returnValue; } catch (Exception t) { logger.warn(format("[%s] execution failed:", message.getPayloadType().getSimpleName()), t); throw t; } } }
@Override public Object handle(UnitOfWork<? extends T> unitOfWork, InterceptorChain interceptorChain) throws Exception { T message = unitOfWork.getMessage(); logger.info("Incoming message: [{}]", message.getPayloadType().getSimpleName()); try { Object returnValue = interceptorChain.proceed(); logger.info("[{}] executed successfully with a [{}] return value", message.getPayloadType().getSimpleName(), returnValue == null ? "null" : returnValue.getClass().getSimpleName()); return returnValue; } catch (Exception t) { logger.warn(format("[%s] execution failed:", message.getPayloadType().getSimpleName()), t); throw t; } } }
@Override public Object handle(UnitOfWork<? extends T> unitOfWork, InterceptorChain interceptorChain) throws Exception { handle(unitOfWork.getMessage()); return interceptorChain.proceed(); }
@Override public Object handle(UnitOfWork<? extends T> unitOfWork, InterceptorChain interceptorChain) throws Exception { handle(unitOfWork.getMessage()); return interceptorChain.proceed(); }
@Override public Object handle(UnitOfWork<? extends CommandMessage<?>> unitOfWork, InterceptorChain interceptorChain) throws Exception { InterceptorChainParameterResolverFactory.initialize(interceptorChain); Object result = delegate.handle(unitOfWork.getMessage(), target); if (delegate.unwrap(CommandHandlerInterceptorHandlingMember.class) .map(CommandHandlerInterceptorHandlingMember::shouldInvokeInterceptorChain).orElse(false)) { result = interceptorChain.proceed(); } return result; } }
@Override public Object handle(UnitOfWork<? extends CommandMessage<?>> unitOfWork, InterceptorChain interceptorChain) throws Exception { InterceptorChainParameterResolverFactory.initialize(interceptorChain); Object result = delegate.handle(unitOfWork.getMessage(), target); if (delegate.unwrap(CommandHandlerInterceptorHandlingMember.class) .map(CommandHandlerInterceptorHandlingMember::shouldInvokeInterceptorChain).orElse(false)) { result = interceptorChain.proceed(); } 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); }); }