ClassCastException when using DistributedCommandBus within Spring Cloud

Hi,

I’m trying to integrate my Axon application with Spring cloud, and I see there’s a axon-distributed-commandbus-springcloud providing the router & connector for DistributedCommandBus.
I used the below configuration for the DistributedCommandBus:

@Primary
@Bean
public DistributedCommandBus distributedCommandBus(CommandRouter router, CommandBusConnector connector) {
    DistributedCommandBus commandBus = new DistributedCommandBus(router, connector);
    return commandBus;
}

@Bean
public SpringCloudCommandRouter springCloudCommandRouter(DiscoveryClient discoveryClient) {
    return new SpringCloudCommandRouter(discoveryClient, new AnnotationRoutingStrategy());
}

@Bean
public SpringHttpCommandBusConnector springHttpCommandBusConnector(@Qualifier("localSegment") CommandBus localSegment,
                                                                   RestTemplate restTemplate,
                                                                   Serializer serializer) {
    return new SpringHttpCommandBusConnector(localSegment, restTemplate, serializer);
}

But,during the integration, I got the error

17:41:53.273 [main] ERROR o.s.boot.SpringApplication - Application startup failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘productController’: Unsatisfied dependency expressed through field ‘commandGateway’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘commandGateway’ defined in org.axonframework.spring.config.AxonConfiguration: Unsatisfied dependency expressed through method ‘commandGateway’ parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘distributedCommandBus’ defined in class path resource [com/edi/learn/cloud/command/config/SpringCloudConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.axonframework.commandhandling.distributed.DistributedCommandBus]: Factory method ‘distributedCommandBus’ threw exception; nested exception is java.lang.ClassCastException: org.axonframework.commandhandling.distributed.commandfilter.DenyAll cannot be cast to org.axonframework.commandhandling.distributed.commandfilter.CommandNameFilter
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)
at com.edi.learn.cloud.main.Application.main(Application.java:21)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘commandGateway’ defined in org.axonframework.spring.config.AxonConfiguration: Unsatisfied dependency expressed through method ‘commandGateway’ parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘distributedCommandBus’ defined in class path resource [com/edi/learn/cloud/command/config/SpringCloudConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.axonframework.commandhandling.distributed.DistributedCommandBus]: Factory method ‘distributedCommandBus’ threw exception; nested exception is java.lang.ClassCastException: org.axonframework.commandhandling.distributed.commandfilter.DenyAll cannot be cast to org.axonframework.commandhandling.distributed.commandfilter.CommandNameFilter
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:467)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
… 19 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘distributedCommandBus’ defined in class path resource [com/edi/learn/cloud/command/config/SpringCloudConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.axonframework.commandhandling.distributed.DistributedCommandBus]: Factory method ‘distributedCommandBus’ threw exception; nested exception is java.lang.ClassCastException: org.axonframework.commandhandling.distributed.commandfilter.DenyAll cannot be cast to org.axonframework.commandhandling.distributed.commandfilter.CommandNameFilter
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
… 32 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.axonframework.commandhandling.distributed.DistributedCommandBus]: Factory method ‘distributedCommandBus’ threw exception; nested exception is java.lang.ClassCastException: org.axonframework.commandhandling.distributed.commandfilter.DenyAll cannot be cast to org.axonframework.commandhandling.distributed.commandfilter.CommandNameFilter
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
… 45 common frames omitted
Caused by: java.lang.ClassCastException: org.axonframework.commandhandling.distributed.commandfilter.DenyAll cannot be cast to org.axonframework.commandhandling.distributed.commandfilter.CommandNameFilter
at org.axonframework.springcloud.commandhandling.SpringCloudCommandRouter.lambda$updateMemberships$4(SpringCloudCommandRouter.java:116)
at java.util.Collections$SingletonSet.forEach(Collections.java:4767)
at org.axonframework.springcloud.commandhandling.SpringCloudCommandRouter.updateMemberships(SpringCloudCommandRouter.java:103)
at org.axonframework.springcloud.commandhandling.SpringCloudCommandRouter.updateMembership(SpringCloudCommandRouter.java:86)
at org.axonframework.commandhandling.distributed.DistributedCommandBus.updateLoadFactor(DistributedCommandBus.java:182)
at com.edi.learn.cloud.command.config.SpringCloudConfiguration.distributedCommandBus(SpringCloudConfiguration.java:44)
at com.edi.learn.cloud.command.config.SpringCloudConfiguration$$EnhancerBySpringCGLIB$$66e868af.CGLIB$distributedCommandBus$0()
at com.edi.learn.cloud.command.config.SpringCloudConfiguration$$EnhancerBySpringCGLIB$$66e868af$$FastClassBySpringCGLIB$$6b415af0.invoke()
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358)
at com.edi.learn.cloud.command.config.SpringCloudConfiguration$$EnhancerBySpringCGLIB$$66e868af.distributedCommandBus()
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
… 46 common frames omitted

Can anyone help?

Thanks

Hi,

this appears to be a bug in the SpringCloudCommandRouter. It assumes a CommandNameFilter when deserializing messages. In your case (considering it’s trying to case a DenyAll instance), you node doesn’t accept any commands.

I have updated issue #316.

Cheers,

Allard

Hi Allard,

Thanks for clarification. BTW, when v3.0.3 will be released? Any plan for this?

Cheers,

在 2017年4月3日星期一 UTC+8下午8:03:12,Allard Buijze写道:

FYI: Version 3.0.3 should have been released last Friday, 7th of April :slight_smile: