question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ClassCastException occurs when configuring DistributedCommandBus with SpringCloud router&connector

See original GitHub issue

I’m using version 3.0.2, and when configuring DistributedCommandBus as below

@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);
    }

I got a ClassCastException.

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$$e22a596b.CGLIB$distributedCommandBus$0(<generated>)
	at com.edi.learn.cloud.command.config.SpringCloudConfiguration$$EnhancerBySpringCGLIB$$e22a596b$$FastClassBySpringCGLIB$$5539d102.invoke(<generated>)
	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$$e22a596b.distributedCommandBus(<generated>)
	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```

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
SidduSidcommented, Jun 30, 2020

i update my code as per your guidance like this,

//spring cloud settings - distributed command bus
    @Bean
    public SpringCloudCommandRouter springCloudCommandRouter(DiscoveryClient client, org.springframework.cloud.client.serviceregistry.Registration localServiceInstance) {
        SpringCloudCommandRouter.Builder spBuilder = new SpringCloudCommandRouter.Builder();
        spBuilder.discoveryClient(client);
        spBuilder.routingStrategy(new AnnotationRoutingStrategy());
        spBuilder.localServiceInstance(localServiceInstance);
        return spBuilder.build();
    }

and its working fine.

Thanks @smcvb

0reactions
SidduSidcommented, Jun 30, 2020

no im am configuring SpringCloudCommandRouter like this

@Autowire
DiscoveryClient discoveryClient;
//spring cloud settings - distributed command bus
@Bean
public SpringCloudCommandRouter springCloudCommandRouter() {
    SpringCloudCommandRouter.Builder spBuilder = new SpringCloudCommandRouter.Builder();
    spBuilder.discoveryClient(discoveryClient);
    spBuilder.routingStrategy(new AnnotationRoutingStrategy());
//spBuilder.localServiceInstance(localServiceInstance); this is not added 

    return spBuilder.build();

}
Read more comments on GitHub >

github_iconTop Results From Across the Web

ClassCastException when using DistributedCommandBus ...
Hi, I'm trying to integrate my Axon application with Spring cloud, ... new DistributedCommandBus(router, connector); return commandBus; ...
Read more >
ClassCastException with Spring Cloud and Kafka Streams
Looks like you don't have the Kafka Streams binder in the classpath, but the message channel binder. Make sure that you have the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found