From 12c7fefbf7eee086ab605636b935b8c3b9e7fad3 Mon Sep 17 00:00:00 2001 From: Vladyslav Baidak Date: Thu, 9 May 2019 16:50:05 +0300 Subject: [PATCH] #6 ErrorHandler and ListenerInvocationErrorHandler recognition --- CHANGELOG.md | 4 + .../axonframework/cdi/CdiExtension.java | 88 ++++++++++++++++++- build.gradle.kts | 2 +- 3 files changed, 91 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f447a4a..84f0d2e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 4.1.1-RC2 + +* [**#6** ErrorHandler and ListenerInvocationErrorHandler recognition](https://github.com/Scalified/axonframework-cdi/issues/6) + # 4.1.1-RC1 * [**#3** Axon Properties And Events](https://github.com/Scalified/axonframework-cdi/issues/3) diff --git a/axonframework-cdi/src/main/java/com/scalified/axonframework/cdi/CdiExtension.java b/axonframework-cdi/src/main/java/com/scalified/axonframework/cdi/CdiExtension.java index 30b04b2..35d3f63 100644 --- a/axonframework-cdi/src/main/java/com/scalified/axonframework/cdi/CdiExtension.java +++ b/axonframework-cdi/src/main/java/com/scalified/axonframework/cdi/CdiExtension.java @@ -41,8 +41,10 @@ import org.axonframework.common.AxonConfigurationException; import org.axonframework.common.transaction.TransactionManager; import org.axonframework.config.*; +import org.axonframework.eventhandling.ErrorHandler; import org.axonframework.eventhandling.EventBus; import org.axonframework.eventhandling.EventHandler; +import org.axonframework.eventhandling.ListenerInvocationErrorHandler; import org.axonframework.eventsourcing.eventstore.EventStorageEngine; import org.axonframework.messaging.MessageDispatchInterceptor; import org.axonframework.messaging.correlation.CorrelationDataProvider; @@ -182,6 +184,16 @@ public class CdiExtension implements Extension { */ private Component queryUpdateEmitterComponent; + /** + * {@link ErrorHandler} component + */ + private Component errorHandlerComponent; + + /** + * {@link ListenerInvocationErrorHandler} component + */ + private Component listenerInvocationErrorHandlerComponent; + /** * {@link CommandDispatchInterceptor} components */ @@ -364,6 +376,12 @@ private void initAxonComponent(AxonComponent annotation, Component component) { if (TypeUtils.isAssignable(actualType, QueryUpdateEmitter.class)) { initialized = initQueryUpdateEmitterComponent(component) || initialized; } + if (TypeUtils.isAssignable(actualType, ErrorHandler.class)) { + initialized = initErrorHandlerComponent(component) || initialized; + } + if (TypeUtils.isAssignable(actualType, ListenerInvocationErrorHandler.class)) { + initialized = initListenerInvocationErrorHandlerComponent(component) || initialized; + } if (TypeUtils.isAssignable(actualType, CommandDispatchInterceptor.class)) { initialized = initCommandDispatchInterceptorComponent(component) || initialized; @@ -637,7 +655,39 @@ private boolean initQueryUpdateEmitterComponent(Component component) { throw new AxonConfigurationException("Multiple QueryUpdateEmitter components declared"); } queryUpdateEmitterComponent = component; - log.trace("Initialized QueryUpdateEmitterConfigurable producer: {}", component); + log.trace("Initialized QueryUpdateEmitter component: {}", component); + return true; + } + + /** + * Initializes {@code errorHandlerComponent} from the given {@code component} + * + * @param component component to initialize + * @return {@code true} if the {@code component} was initialized, {@code false} otherwise + * @throws AxonConfigurationException in case of initialization error + */ + private boolean initErrorHandlerComponent(Component component) { + if (nonNull(errorHandlerComponent)) { + throw new AxonConfigurationException("Multiple ErrorHandler components declared"); + } + errorHandlerComponent = component; + log.trace("Initialized ErrorHandler component: {}", component); + return true; + } + + /** + * Initializes {@code listenerInvocationErrorHandlerComponent} from the given {@code component} + * + * @param component component to initialize + * @return {@code true} if the {@code component} was initialized, {@code false} otherwise + * @throws AxonConfigurationException in case of initialization error + */ + private boolean initListenerInvocationErrorHandlerComponent(Component component) { + if (nonNull(listenerInvocationErrorHandlerComponent)) { + throw new AxonConfigurationException("Multiple ListenerInvocationErrorHandler components declared"); + } + listenerInvocationErrorHandlerComponent = component; + log.trace("Initialized ListenerInvocationErrorHandler component: {}", component); return true; } @@ -762,6 +812,8 @@ void afterDeploymentValidation(@Observes AfterDeploymentValidation event, BeanMa configureQueryBus(beanManager); configureResourceInjector(beanManager); configureQueryUpdateEmitter(beanManager); + configureErrorHandler(beanManager); + configureListenerInvocationErrorHandler(beanManager); configureEventUpcasters(beanManager); configureModules(beanManager); @@ -1067,7 +1119,39 @@ private void configureQueryUpdateEmitter(BeanManager beanManager) { Configurable configurable = ConfigurableComponentResolver.of(queryUpdateEmitterComponent).resolve(beanManager); configurer = configurer.configureQueryUpdateEmitter(configurable); - log.debug("Configured QueryUpdateEmitter: {}", configurable); + log.debug("Configured QueryUpdateEmitter: {}", queryUpdateEmitterComponent); + } + } + + /** + * Configures Axon default {@link ErrorHandler} + * + * @param beanManager current {@link BeanManager} + * @see EventProcessingConfigurer#registerDefaultErrorHandler(Function) + */ + private void configureErrorHandler(BeanManager beanManager) { + if (nonNull(errorHandlerComponent)) { + Configurable configurable = + ConfigurableComponentResolver.of(errorHandlerComponent).resolve(beanManager); + configurer = configurer.eventProcessing(eventProcessingConfigurer -> + eventProcessingConfigurer.registerDefaultErrorHandler(configurable)); + log.debug("Configured default ErrorHandler: {}", errorHandlerComponent); + } + } + + /** + * Configures Axon default {@link ListenerInvocationErrorHandler} + * + * @param beanManager current {@link BeanManager} + * @see EventProcessingConfigurer#registerDefaultListenerInvocationErrorHandler(Function) + */ + private void configureListenerInvocationErrorHandler(BeanManager beanManager) { + if (nonNull(listenerInvocationErrorHandlerComponent)) { + Configurable configurable = + ConfigurableComponentResolver.of(listenerInvocationErrorHandlerComponent).resolve(beanManager); + configurer = configurer.eventProcessing(eventProcessingConfigurer -> + eventProcessingConfigurer.registerDefaultListenerInvocationErrorHandler(configurable)); + log.debug("Configured default ListenerInvocationErrorHandler: {}", listenerInvocationErrorHandlerComponent); } } diff --git a/build.gradle.kts b/build.gradle.kts index 40248a7..5a18a42 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -22,7 +22,7 @@ allprojects { val axonVersion by extra("4.1.1") group = "com.scalified" - version = "$axonVersion-RC1" + version = "$axonVersion-RC2" repositories { mavenCentral()