@Override public Object handle(UnitOfWork<? extends T> unitOfWork, InterceptorChain interceptorChain) throws Exception { correlationDataProviders.forEach(unitOfWork::registerCorrelationDataProvider); 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 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; } }
private void invokeInterceptorChain(CommandHandlingEntry entry) { CommandResultMessage<?> commandResultMessage; try { commandResultMessage = asCommandResultMessage(entry.getPublisherInterceptorChain().proceed()); } catch (Exception throwable) { commandResultMessage = asCommandResultMessage(throwable); } entry.setResult(commandResultMessage); }
@Override public Object handle(UnitOfWork<? extends T> unitOfWork, InterceptorChain interceptorChain) throws Exception { Transaction transaction = transactionManager.startTransaction(); unitOfWork.onCommit(u -> transaction.commit()); unitOfWork.onRollback(u -> transaction.rollback()); return interceptorChain.proceed(); } }
@CommandHandlerInterceptor public static void interceptAll(Object command, InterceptorChain chain) throws Exception { apply(new AnyCommandInterceptedEvent("StaticNestedNested" + command.getClass().getName())); chain.proceed(); }
@CommandHandlerInterceptor(commandNamePattern = ".*Nested.*") public void interceptAllMatchingPattern(Object command, InterceptorChain interceptorChain) throws Exception { apply(new AnyCommandMatchingPatternInterceptedEvent(command.getClass().getName())); interceptorChain.proceed(); }
@Test public void testMultiThreadTokenIsStoredWhenEventIsRead() throws Exception { CountDownLatch countDownLatch = new CountDownLatch(2); testSubject.registerHandlerInterceptor(((unitOfWork, interceptorChain) -> { unitOfWork.onCleanup(uow -> countDownLatch.countDown()); return interceptorChain.proceed(); })); testSubject.start(); eventBus.publish(createEvents(2)); assertTrue("Expected Unit of Work to have reached clean up phase", countDownLatch.await(5, SECONDS)); verify(tokenStore, atLeastOnce()).storeToken(any(), any(), anyInt()); assertThat(tokenStore.fetchToken(testSubject.getName(), 0), notNullValue()); assertThat(tokenStore.fetchToken(testSubject.getName(), 1), notNullValue()); }
@Test public void testTokenIsStoredWhenEventIsRead() throws Exception { CountDownLatch countDownLatch = new CountDownLatch(1); 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(createEvent()); assertTrue("Expected Unit of Work to have reached clean up phase", countDownLatch.await(5, TimeUnit.SECONDS)); verify(tokenStore).extendClaim(eq(testSubject.getName()), anyInt()); verify(tokenStore).storeToken(any(), any(), anyInt()); assertNotNull(tokenStore.fetchToken(testSubject.getName(), 0)); }
@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)); }
@Test public void testTokenIsNotStoredWhenUnitOfWorkIsRolledBack() throws Exception { CountDownLatch countDownLatch = new CountDownLatch(1); testSubject.registerHandlerInterceptor(((unitOfWork, interceptorChain) -> { unitOfWork.onCommit(uow -> { 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(createEvent()); assertTrue("Expected Unit of Work to have reached clean up phase", countDownLatch.await(5, TimeUnit.SECONDS)); assertNull(tokenStore.fetchToken(testSubject.getName(), 0)); }
@Test public void testTokenIsStoredOncePerEventBatch() throws Exception { testSubject = TrackingEventProcessor.builder() .name("test") .eventHandlerInvoker(eventHandlerInvoker) .messageSource(eventBus) .tokenStore(tokenStore) .transactionManager(NoTransactionManager.INSTANCE) .build(); CountDownLatch countDownLatch = new CountDownLatch(2); 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(createEvents(2)); assertTrue("Expected Unit of Work to have reached clean up phase for 2 messages", countDownLatch.await(5, TimeUnit.SECONDS)); InOrder inOrder = inOrder(tokenStore); inOrder.verify(tokenStore, times(1)).extendClaim(eq(testSubject.getName()), anyInt()); inOrder.verify(tokenStore, times(1)).storeToken(any(), any(), anyInt()); assertNotNull(tokenStore.fetchToken(testSubject.getName(), 0)); }
@CommandHandlerInterceptor public void intercept(ClearMyAggregateStateCommand command, InterceptorChain interceptorChain) throws Exception { if (command.isProceed()) { interceptorChain.proceed(); } else { apply(new MyAggregateStateNotClearedEvent(command.getId())); } }
@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 void onEvent(CommandHandlingEntry entry, long sequence, boolean endOfBatch) { if (entry.isRecoverEntry()) { removeEntry(entry.getAggregateIdentifier()); } else if (entry.getInvokerId() == segmentId) { entry.start(); try { Object result = entry.getInvocationInterceptorChain().proceed(); entry.setResult(asCommandResultMessage(result)); } catch (Exception throwable) { entry.setResult(asCommandResultMessage(throwable)); } finally { entry.pause(); } } }
unitOfWork.getResource(segmentIdResourceKey))); return interceptorChain.proceed(); });
@Test public void testHandlerInterceptorOnAggregate() { configuration.deadlineManager().registerHandlerInterceptor((uow, chain) -> { uow.transformMessage(deadlineMessage -> GenericDeadlineMessage .asDeadlineMessage(deadlineMessage.getDeadlineName(), new DeadlinePayload("fakeId"))); return chain.proceed(); }); configuration.commandGateway().sendAndWait(new CreateMyAggregateCommand(IDENTIFIER)); assertPublishedEvents(new MyAggregateCreatedEvent(IDENTIFIER), new DeadlineOccurredEvent(new DeadlinePayload("fakeId"))); }
@Test public void testHandlerInterceptorOnSaga() { EventMessage<Object> testEventMessage = asEventMessage(new SagaStartingEvent(IDENTIFIER, DO_NOT_CANCEL_BEFORE_DEADLINE)); configuration.deadlineManager().registerHandlerInterceptor((uow, chain) -> { uow.transformMessage(deadlineMessage -> GenericDeadlineMessage .asDeadlineMessage(deadlineMessage.getDeadlineName(), new DeadlinePayload("fakeId"))); return chain.proceed(); }); configuration.eventStore().publish(testEventMessage); assertPublishedEvents(new SagaStartingEvent(IDENTIFIER, DO_NOT_CANCEL_BEFORE_DEADLINE), new DeadlineOccurredEvent(new DeadlinePayload("fakeId"))); }