Eventhandler outside aggregate is raising classcastexception on runtime,

Without registerinng events are not received, Grails 3 , boot axon 3.0.5

Can you help me?

Kind regards

ERROR

org.springframework.context.ApplicationContextException: Failed to start bean ‘eventHandlerRegistrar’; nested exception is java.lang.ClassCastException: org.springframework.beans.factory.config.MethodInvokingFactoryBean$$EnhancerBySpringCGLIB$$773ccb1d cannot be cast to org.axonframework.eventhandling.EventBus
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178)
at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:50)
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:348)
at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:151)
at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:114)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:879)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:372)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
at grails.boot.GrailsApp.run(GrailsApp.groovy:83)
at grails.boot.GrailsApp.run(GrailsApp.groovy:388)
at grails.boot.GrailsApp.run(GrailsApp.groovy:375)
at grails.boot.GrailsApp$run.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
at es.wealth.backoffice.Application.main(Application.groovy:12)
Caused by: java.lang.ClassCastException: org.springframework.beans.factory.config.MethodInvokingFactoryBean$$EnhancerBySpringCGLIB$$773ccb1d cannot be cast to org.axonframework.eventhandling.EventBus
at org.axonframework.spring.config.AxonConfiguration$$EnhancerBySpringCGLIB$$783e9336.eventBus()
at org.axonframework.config.EventHandlingConfiguration.subscribingEventProcessor(EventHandlingConfiguration.java:83)
at org.axonframework.config.EventHandlingConfiguration.defaultEventProcessor(EventHandlingConfiguration.java:72)
at org.axonframework.config.EventHandlingConfiguration.lambda$initialize$13(EventHandlingConfiguration.java:332)
at java.util.HashMap.forEach(HashMap.java:1280)
at org.axonframework.config.EventHandlingConfiguration.initialize(EventHandlingConfiguration.java:330)
at org.axonframework.spring.config.EventHandlerRegistrar.start(EventHandlerRegistrar.java:84)
Disconnected from the target VM, address: ‘127.0.0.1:64639’, transport: ‘socket’
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:175)
… 19 common frames omitted

CONFIG

package es.wealth.backoffice.config

import com.mongodb.MongoClient
import com.mongodb.MongoCredential
import com.mongodb.ServerAddress
import es.wealth.backoffice.domain.model.familygroup.FamilyGroup
import es.wealth.backoffice.domain.model.familygroup.FamilyGroupRepository
import es.wealth.backoffice.domain.model.familygroup.event.FamiliyGroupEventHandler
import es.wealth.backoffice.domain.service.FamilyGroupDomainService
import org.axonframework.config.Configurer
import org.axonframework.config.EventHandlingConfiguration
import org.axonframework.eventsourcing.AggregateFactory
import org.axonframework.eventsourcing.EventCountSnapshotTriggerDefinition
import org.axonframework.eventsourcing.EventSourcingRepository
import org.axonframework.eventsourcing.eventstore.EmbeddedEventStore
import org.axonframework.eventsourcing.eventstore.EventStore
import org.axonframework.mongo.eventsourcing.eventstore.DefaultMongoTemplate
import org.axonframework.mongo.eventsourcing.eventstore.MongoEventStorageEngine
import org.axonframework.mongo.eventsourcing.eventstore.MongoTemplate
import org.axonframework.mongo.eventsourcing.eventstore.documentperevent.DocumentPerEventStorageStrategy
import org.axonframework.serialization.json.JacksonSerializer
import org.axonframework.spring.eventsourcing.SpringAggregateSnapshotter
import org.axonframework.spring.eventsourcing.SpringAggregateSnapshotterFactoryBean
import org.axonframework.spring.eventsourcing.SpringPrototypeAggregateFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.ApplicationContext
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Scope

@Configuration
class AxonConfiguration {

    @Autowired
    FamilyGroupRepository familyGroupRepository
    @Autowired
    FamilyGroupDomainService familyGroupDomainService

    @Bean(name = "axonMongoTemplate")
    MongoTemplate axonMongoTemplate() {
        // TODO config
        MongoClient mongoClient = new MongoClient(new ServerAddress("mongodb", 27017), [MongoCredential.createCredential("wealth-eventstore", "wealth-eventstore", "wealth-eventstore".getChars())])
        MongoTemplate template = new DefaultMongoTemplate(mongoClient,
                "wealth-eventstore", "familyGroup", "familyGroup-snapshot");
        template
    }

    @Bean
    JacksonSerializer jacksonSerializer() {
        new JacksonSerializer()
    }

    @Bean
    EventStore embeddedEventStore() {
        MongoEventStorageEngine storageEngine = new MongoEventStorageEngine(jacksonSerializer(), null, axonMongoTemplate(), new DocumentPerEventStorageStrategy())
        EventStore eventStore = new EmbeddedEventStore(storageEngine)
        eventStore
    }

    @Bean
    SpringAggregateSnapshotterFactoryBean springAggregateSnapshotterFactoryBean() {
        new SpringAggregateSnapshotterFactoryBean()
    }

    @Bean
    @Scope("prototype")
    AggregateFactory<FamilyGroup> transactionAggregateFactory() {
        // TODO generic?
        AggregateFactory<FamilyGroup> aggregateFactory = new SpringPrototypeAggregateFactory<FamilyGroup>()
        aggregateFactory.setPrototypeBeanName("familyGroup")
        aggregateFactory;
    }

    @Bean
    @Scope("prototype")
    FamilyGroup familyGroup() {
        // TODO generic?
        new FamilyGroup(familyGroupRepository, familyGroupDomainService)
    }

    @Bean
    EventSourcingRepository<FamilyGroup> axonFamilyGroupRepository(EventStore eventStore, SpringAggregateSnapshotterFactoryBean springAggregateSnapshotterFactoryBean, ApplicationContext applicationContext) {
        SpringAggregateSnapshotter snapshotter = springAggregateSnapshotterFactoryBean.getObject()
        snapshotter.setApplicationContext(applicationContext)
        EventCountSnapshotTriggerDefinition snapshotTriggerDefinition = new EventCountSnapshotTriggerDefinition(
                snapshotter,
                2);

        new EventSourcingRepository(FamilyGroup, eventStore, snapshotTriggerDefinition)
    }

    @Autowired configure(Configurer configurer,EventHandlingConfiguration ehConfiguration){
        // define an EventHandlingConfiguration

        ehConfiguration.registerEventHandler({conf -> new FamiliyGroupEventHandler()});

        // the module needs to be registered with the Axon Configuration
        configurer.registerModule(ehConfiguration);
    }

    /*
    @Bean
    AggregateAnnotationCommandHandler<FamilyGroup> commandHandler(EventSourcingRepository eventSourcingRepository) {
        new AggregateAnnotationCommandHandler<FamilyGroup>(FamilyGroup, eventSourcingRepository)
    }
    */

}

package es.wealth.backoffice.domain.model.familygroup.event

import groovy.util.logging.Slf4j
import org.axonframework.eventhandling.EventHandler
@Slf4j
class FamiliyGroupEventHandler {

    @EventHandler
    void handle(FamilyGroupCreatedEvent familyGroupCreatedEvent) {
        log.info "onCreate: ${familyGroupCreatedEvent}"
        //familyGroupRepository.create(this)
    }

}

TO enhace some info its important to mention that our aim is to receive events outside the aggregate: we werent able at all with tons of differents configuration

Can you help us to be able to handle events on an non aggregate pojo with axon + springboot?

regards

There’s a lot of configuration that doesn’t make a lot of sense to me. It doesn’t need to be wrong, but when having problems like what you’re describing, less is more. Classcast exceptions like these typically occur in messy Spring configuration, where a beandefinition overrides another one, which happens to deliver a different type of bean.

Instead of defining a bean of type EventStore, just define the EventStorageEngine. The Axon Spring Boot Starter module will autoconfigure the rest.
If you want to use Event Sourcing, don’t specify any repositories. Just annotate your aggregate root class with @Aggregate. Also, don’t define the aggregate itself, nor the aggregateFactory as beans. That’s all done automatically.

Try to avoid registering event handlers using the eventHandlingConfiguration. If they’re Spring @Component annotated and have @EventHandler methods, they are autodiscovered.

Once everything works as expected, you can provide additional configuration for snapshotting.

Cheers,

Allard

If i do this the following error raises. I cannot hide reactor dependevy. It is used

Added to this if we finally make it work, how can we configure snapshotting? Ive tested to get the eventstore but no bean defined…

g.springframework.context.ApplicationContextException: Failed to start bean ‘org.axonframework.spring.config.AxonConfiguration’; nested exception is java.lang.ClassCastException: reactor.bus.EventBus cannot be cast to org.axonframework.eventhandling.EventBus
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178)
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:167)
at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:50)
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:348)
at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:151)
at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:114)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:879)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:372)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
at grails.boot.GrailsApp.run(GrailsApp.groovy:83)
at grails.boot.GrailsApp.run(GrailsApp.groovy:388)
at grails.boot.GrailsApp.run(GrailsApp.groovy:375)
at grails.boot.GrailsApp$run.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
at es.wealth.backoffice.Application.main(Application.groovy:12)
Caused by: java.lang.ClassCastException: reactor.bus.EventBus cannot be cast to org.axonframework.eventhandling.EventBus
at org.axonframework.spring.config.SpringAxonAutoConfigurer.lambda$null$5(SpringAxonAutoConfigurer.java:122)
at org.axonframework.config.Component.get(Component.java:73)
at org.axonframework.config.DefaultConfigurer$ConfigurationImpl.getComponent(DefaultConfigurer.java:344)
at org.axonframework.config.Configuration.getComponent(Configuration.java:114)
at org.axonframework.config.Configuration.eventBus(Configuration.java:50)
at org.axonframework.config.AggregateConfigurer.lambda$new$9(AggregateConfigurer.java:98)
at org.axonframework.config.Component.get(Component.java:73)
at org.axonframework.config.AggregateConfigurer.lambda$new$11(AggregateConfigurer.java:110)
at org.axonframework.config.Component.get(Component.java:73)
at org.axonframework.config.AggregateConfigurer.start(AggregateConfigurer.java:175)
at java.util.ArrayList.forEach(ArrayList.java:1249)
at org.axonframework.config.DefaultConfigurer.invokeStartHandlers(DefaultConfigurer.java:292)
at org.axonframework.config.DefaultConfigurer$ConfigurationImpl.start(DefaultConfigurer.java:355)
at org.axonframework.spring.config.AxonConfiguration.start(AxonConfiguration.java:127)
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:175)
… 20 common frames omitted

package es.wealth.backoffice.config

import com.mongodb.MongoClient
import com.mongodb.MongoCredential
import com.mongodb.ServerAddress
import es.wealth.backoffice.domain.model.familygroup.FamilyGroup
import org.axonframework.eventsourcing.EventCountSnapshotTriggerDefinition
import org.axonframework.eventsourcing.EventSourcingRepository
import org.axonframework.eventsourcing.eventstore.EmbeddedEventStore
import org.axonframework.eventsourcing.eventstore.EventStorageEngine
import org.axonframework.eventsourcing.eventstore.EventStore
import org.axonframework.mongo.eventsourcing.eventstore.DefaultMongoTemplate
import org.axonframework.mongo.eventsourcing.eventstore.MongoEventStorageEngine
import org.axonframework.mongo.eventsourcing.eventstore.MongoTemplate
import org.axonframework.mongo.eventsourcing.eventstore.documentperevent.DocumentPerEventStorageStrategy
import org.axonframework.serialization.json.JacksonSerializer
import org.axonframework.spring.eventsourcing.SpringAggregateSnapshotter
import org.axonframework.spring.eventsourcing.SpringAggregateSnapshotterFactoryBean
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.ApplicationContext
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
class AxonConfiguration {

    @Bean(name = "axonMongoTemplate")
    MongoTemplate axonMongoTemplate() {
        // TODO config
        MongoClient mongoClient = new MongoClient(new ServerAddress("mongodb", 27017), [MongoCredential.createCredential("wealth-eventstore", "wealth-eventstore", "wealth-eventstore".getChars())])
        MongoTemplate template = new DefaultMongoTemplate(mongoClient,
                "wealth-eventstore", "familyGroup", "familyGroup-snapshot");
        template
    }

    @Bean
    JacksonSerializer jacksonSerializer() {
        new JacksonSerializer()
    }

    @Bean
    EventStorageEngine embeddedEventStore() {
        MongoEventStorageEngine storageEngine = new MongoEventStorageEngine(jacksonSerializer(), null, axonMongoTemplate(), new DocumentPerEventStorageStrategy())
        //EventStore eventStore = new EmbeddedEventStore(storageEngine)
        storageEngine
    }

    /*@Bean
    EventSourcingRepository<FamilyGroup> axonFamilyGroupRepository(EventStore eventStore, SpringAggregateSnapshotterFactoryBean springAggregateSnapshotterFactoryBean, ApplicationContext applicationContext) {
        SpringAggregateSnapshotter snapshotter = springAggregateSnapshotterFactoryBean.getObject()
        snapshotter.setApplicationContext(applicationContext)
        EventCountSnapshotTriggerDefinition snapshotTriggerDefinition = new EventCountSnapshotTriggerDefinition(
                snapshotter,
                2)

        new EventSourcingRepository(FamilyGroup, eventStore, snapshotTriggerDefinition)
    }*/

    /*
    @Bean
    AggregateAnnotationCommandHandler<FamilyGroup> commandHandler(EventSourcingRepository eventSourcingRepository) {
        new AggregateAnnotationCommandHandler<FamilyGroup>(FamilyGroup, eventSourcingRepository)
    }
    */

}

Enviado con Mailtrack

Any help will be very welcome :slight_smile:

It seems that you’re injecting a bean of type reactor.bus.EventBus in a place where an Axon Event Bus (org.axonframework.eventhandling.EventBus) is expected.

The exception occurs when Axon tries to get the EventBus it has previously defined from the application context. However, in your context, another bean has overridden the EventBus with a bean of type reactor.bus.EventBus.
By default, Axon calls it’s EventBus (or EventStore) bean “eventBus”. Either change the bean name for the reactor.bus.EventBus to avoid this conflict, or explicitly define another bean name for the Axon EventBus/Store.

Cheers,

Allard

Eventbus problem, but after cleaning up configuration, same problem raises when defining @component con eventhandler

Any ideas?

package es.wealth.backoffice.config

import com.mongodb.MongoClient
import com.mongodb.MongoCredential
import com.mongodb.ServerAddress
import es.wealth.backoffice.domain.model.client.event.ClientEventHandler
import es.wealth.backoffice.domain.model.contract.Contract
import es.wealth.backoffice.domain.model.familygroup.FamilyGroup
import es.wealth.backoffice.domain.model.kyc.Kyc
import es.wealth.backoffice.domain.model.person.NaturalPerson
import org.axonframework.config.EventHandlingConfiguration
import org.axonframework.eventhandling.EventBus
import org.axonframework.eventhandling.EventHandler
import org.axonframework.eventhandling.SimpleEventHandlerInvoker
import org.axonframework.eventsourcing.EventCountSnapshotTriggerDefinition
import org.axonframework.eventsourcing.EventSourcingRepository
import org.axonframework.eventsourcing.eventstore.EmbeddedEventStore
import org.axonframework.eventsourcing.eventstore.EventStore
import org.axonframework.mongo.eventsourcing.eventstore.DefaultMongoTemplate
import org.axonframework.mongo.eventsourcing.eventstore.MongoEventStorageEngine
import org.axonframework.mongo.eventsourcing.eventstore.MongoTemplate
import org.axonframework.mongo.eventsourcing.eventstore.documentperevent.DocumentPerEventStorageStrategy
import org.axonframework.serialization.json.JacksonSerializer
import org.axonframework.spring.eventsourcing.SpringAggregateSnapshotter
import org.axonframework.spring.eventsourcing.SpringAggregateSnapshotterFactoryBean
import org.axonframework.spring.stereotype.Aggregate
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.ApplicationContext
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Primary

@Configuration
class AxonConfiguration {

    @Bean(name = "axonMongoTemplate")
    MongoTemplate axonMongoTemplate() {
        // TODO config
        MongoClient mongoClient = new MongoClient(new ServerAddress("mongodb", 27017), [MongoCredential.createCredential("wealth-eventstore", "wealth-eventstore", "wealth-eventstore".getChars())])
        MongoTemplate template = new DefaultMongoTemplate(mongoClient,
                "wealth-eventstore", "events", "events-snapshot");
        template
    }

    @Bean
    JacksonSerializer jacksonSerializer() {
        new JacksonSerializer()
    }

    @Primary
    @Bean(name = "eventBusser")
    EventStore embeddedEventStore() {
        MongoEventStorageEngine storageEngine = new MongoEventStorageEngine(jacksonSerializer(), null, axonMongoTemplate(), new DocumentPerEventStorageStrategy())
        EventStore eventStore = new EmbeddedEventStore(storageEngine)
        eventStore
    }
 
    /*@Bean
    SpringAggregateSnapshotterFactoryBean springAggregateSnapshotterFactoryBean() {
        new SpringAggregateSnapshotterFactoryBean()
    }

    @Bean
    EventSourcingRepository<FamilyGroup> axonFamilyGroupRepository(EventStore eventStore, SpringAggregateSnapshotterFactoryBean springAggregateSnapshotterFactoryBean, ApplicationContext applicationContext) {
        SpringAggregateSnapshotter snapshotter = springAggregateSnapshotterFactoryBean.getObject()
        snapshotter.setApplicationContext(applicationContext)
        EventCountSnapshotTriggerDefinition snapshotTriggerDefinition = new EventCountSnapshotTriggerDefinition(
                snapshotter,
                2)

        new EventSourcingRepository(FamilyGroup, eventStore, snapshotTriggerDefinition)
    }

    @Bean
    EventSourcingRepository<NaturalPerson> axonNaturalPersonRepository(EventStore eventStore, SpringAggregateSnapshotterFactoryBean springAggregateSnapshotterFactoryBean, ApplicationContext applicationContext) {
        SpringAggregateSnapshotter snapshotter = springAggregateSnapshotterFactoryBean.getObject()
        snapshotter.setApplicationContext(applicationContext)
        EventCountSnapshotTriggerDefinition snapshotTriggerDefinition = new EventCountSnapshotTriggerDefinition(
                snapshotter,
                2)

        new EventSourcingRepository(NaturalPerson, eventStore, snapshotTriggerDefinition)
    }

    @Bean
    EventSourcingRepository<Contract> axonContractRepository(EventStore eventStore, SpringAggregateSnapshotterFactoryBean springAggregateSnapshotterFactoryBean, ApplicationContext applicationContext) {
        SpringAggregateSnapshotter snapshotter = springAggregateSnapshotterFactoryBean.getObject()
        snapshotter.setApplicationContext(applicationContext)
        EventCountSnapshotTriggerDefinition snapshotTriggerDefinition = new EventCountSnapshotTriggerDefinition(
                snapshotter,
                2)

        new EventSourcingRepository(Contract, eventStore, snapshotTriggerDefinition)
    }

    @Bean
    EventSourcingRepository<Kyc> axonKycRepository(EventStore eventStore, SpringAggregateSnapshotterFactoryBean springAggregateSnapshotterFactoryBean, ApplicationContext applicationContext) {
        SpringAggregateSnapshotter snapshotter = springAggregateSnapshotterFactoryBean.getObject()
        snapshotter.setApplicationContext(applicationContext)
        EventCountSnapshotTriggerDefinition snapshotTriggerDefinition = new EventCountSnapshotTriggerDefinition(
                snapshotter,
                2)

        new EventSourcingRepository(Kyc, eventStore, snapshotTriggerDefinition)
    }*/

    /*
    @Bean
    AggregateAnnotationCommandHandler<FamilyGroup> commandHandler(EventSourcingRepository eventSourcingRepository) {
        new AggregateAnnotationCommandHandler<FamilyGroup>(FamilyGroup, eventSourcingRepository)
    }
    */

}

package es.wealth.backoffice.domain.model.client.event

import groovy.util.logging.Slf4j
import org.axonframework.eventhandling.EventHandler
import org.springframework.stereotype.Component

@Component
@Slf4j
class ClientEventHandler {

    @EventHandler
    void onEvent(ClientCreatedEvent clientCreatedEvent){
        log.info("IT WORKSS!!!!!!YEAHH")
    }
}

org.springframework.context.ApplicationContextException: Failed to start bean ‘eventHandlerRegistrar’; nested exception is java.lang.ClassCastException: org.springframework.beans.factory.config.MethodInvokingFactoryBean$$EnhancerBySpringCGLIB$$773ccb1d cannot be cast to org.axonframework.eventhandling.EventBus
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178)
at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:50)
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:348)
at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:151)
at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:114)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:879)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:372)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
at grails.boot.GrailsApp.run(GrailsApp.groovy:83)
at grails.boot.GrailsApp.run(GrailsApp.groovy:388)
at grails.boot.GrailsApp.run(GrailsApp.groovy:375)
at grails.boot.GrailsApp$run.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
at es.wealth.backoffice.Application.main(Application.groovy:12)
Caused by: java.lang.ClassCastException: org.springframework.beans.factory.config.MethodInvokingFactoryBean$$EnhancerBySpringCGLIB$$773ccb1d cannot be cast to org.axonframework.eventhandling.EventBus
at org.axonframework.spring.config.AxonConfiguration$$EnhancerBySpringCGLIB$$783e9336.eventBus()
at org.axonframework.config.EventHandlingConfiguration.subscribingEventProcessor(EventHandlingConfiguration.java:83)
at org.axonframework.config.EventHandlingConfiguration.defaultEventProcessor(EventHandlingConfiguration.java:72)
at org.axonframework.config.EventHandlingConfiguration.lambda$initialize$13(EventHandlingConfiguration.java:332)
at java.util.HashMap.forEach(HashMap.java:1280)
at org.axonframework.config.EventHandlingConfiguration.initialize(EventHandlingConfiguration.java:330)
at org.axonframework.spring.config.EventHandlerRegistrar.start(EventHandlerRegistrar.java:84)
Disconnected from the target VM, address: ‘127.0.0.1:64639’, transport: ‘socket’
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:175)
… 19 common frames omitted

It seems that the bean called “eventBus” isn’t the Axon Event Bus at the end of the Spring Context initialization. Most likely because the bean is being overwritten with a bean of another type.

Are you sure the below is the only configuration in effect?

Yes sure. Search eventBus on all the project but no luck

Enviado con Mailtrack

Hi,

one thing that you could try to work around the problem, is to not define a bean for the EventStore, but only for the MongoEventStorageEngine. In that case, Axon will configure an EventStore/Bus for you.

Cheers,

Allard

Only with eventstoragengine

org.springframework.context.ApplicationContextException: Failed to start bean ‘org.axonframework.spring.config.AxonConfiguration’; nested exception is java.lang.ClassCastException: reactor.bus.EventBus cannot be cast to org.axonframework.eventhandling.EventBus
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178)
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:167)
at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:50)
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:348)
at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:151)
at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:114)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:879)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:372)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
at grails.boot.GrailsApp.run(GrailsApp.groovy:83)
at grails.boot.GrailsApp.run(GrailsApp.groovy:388)
at grails.boot.GrailsApp.run(GrailsApp.groovy:375)
at grails.boot.GrailsApp$run.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
at es.wealth.backoffice.Application.main(Application.groovy:12)
Caused by: java.lang.ClassCastException: reactor.bus.EventBus cannot be cast to org.axonframework.eventhandling.EventBus
at org.axonframework.spring.config.SpringAxonAutoConfigurer.lambda$null$5(SpringAxonAutoConfigurer.java:122)
at org.axonframework.config.Component.get(Component.java:73)
at org.axonframework.config.DefaultConfigurer$ConfigurationImpl.getComponent(DefaultConfigurer.java:344)
at org.axonframework.config.Configuration.getComponent(Configuration.java:114)
at org.axonframework.config.Configuration.eventBus(Configuration.java:50)
at org.axonframework.config.AggregateConfigurer.lambda$new$9(AggregateConfigurer.java:98)
at org.axonframework.config.Component.get(Component.java:73)
at org.axonframework.config.AggregateConfigurer.lambda$new$11(AggregateConfigurer.java:110)
at org.axonframework.config.Component.get(Component.java:73)
at org.axonframework.config.AggregateConfigurer.start(AggregateConfigurer.java:175)
at java.util.ArrayList.forEach(ArrayList.java:1249)
at org.axonframework.config.DefaultConfigurer.invokeStartHandlers(DefaultConfigurer.java:292)
at org.axonframework.config.DefaultConfigurer$ConfigurationImpl.start(DefaultConfigurer.java:355)
at org.axonframework.spring.config.AxonConfiguration.start(AxonConfiguration.java:127)
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:175)
… 20 common frames omitted

Enviado con Mailtrack

Any other clue?

Enviado con Mailtrack

To me, this is an indication that something is wrong in the Spring configuration. Apparently, Spring found and (Axon) EventBus and when Axon tries to get the event bus out of the Spring context to use it, it is suddenly become a reactor Event Bus. This means that bean definitions are being overwritten with beans of a different type.

Where does the reactor.bus.EventBus come from? What name did you give that bean? Perhaps there is a naming conflict between Axon and Reactor.

Allard

Its used on grails oauth plugin. Its defined on the library

Enviado con Mailtrack

This there any change you can override the bean name of that specific library? For example by specifying your own reactor.bus.EventBus for the oauth plugin, but with an added @Qualifier to adjust the name, to alleviate the conflict between Axon’s EventBus?

Otherwise, you’ll have to do that on the Axon side.
So create an Axon EventBus, with an added @Qualifier to the bean definition to give it a non-conflicting name with the plugin’s EventBus.
You’ll have to do the correct wiring of Axon’s EventBus yourself then though.

Cheers,

Steven

If i debug , on the messagesource.apply(conf) call, ConfigurationClassEnhancer is called on method, with beanName eventBud.

Is there anyway to search for eventbus con apply conf, with other name?

private SubscribingEventProcessor subscribingEventProcessor(Configuration conf, String name, List<?> eh,
                                                            Function<Configuration, SubscribableMessageSource<? extends EventMessage<?>>> messageSource) {
    return new SubscribingEventProcessor(name,
                                         new SimpleEventHandlerInvoker(eh,
                                                                       conf.parameterResolverFactory(),
                                                                       conf.getComponent(
                                                                               ListenerInvocationErrorHandler.class,
                                                                               LoggingErrorHandler::new)),
                                         messageSource.apply(conf),
                                         DirectEventProcessingStrategy.INSTANCE,
                                         PropagatingErrorHandler.INSTANCE,
                                         conf.messageMonitor(SubscribingEventProcessor.class,
                                                             name));
}
private static class BeanMethodInterceptor implements MethodInterceptor, ConditionalCallback {

   /**
    * Enhance a {@link Bean @Bean} method to check the supplied BeanFactory for the
    * existence of this bean object.
    * @throws Throwable as a catch-all for any exception that may be thrown when invoking the
    * super implementation of the proxied method i.e., the actual {@code @Bean} method
    */
   @Override
   public Object intercept(Object enhancedConfigInstance, Method beanMethod, Object[] beanMethodArgs,
            MethodProxy cglibMethodProxy) throws Throwable {

      ConfigurableBeanFactory beanFactory = getBeanFactory(enhancedConfigInstance);
      String beanName = BeanAnnotationHelper.determineBeanNameFor(beanMethod);

      // Determine whether this bean is a scoped-proxy
      Scope scope = AnnotatedElementUtils.findMergedAnnotation(beanMethod, Scope.class);
      if (scope != null && scope.proxyMode() != ScopedProxyMode.NO) {
         String scopedBeanName = ScopedProxyCreator.getTargetBeanName(beanName);
         if (beanFactory.isCurrentlyInCreation(scopedBeanName)) {
            beanName = scopedBeanName;
         }
      }

Enviado con Mailtrack

Sorry Fernando, but I’m not sure what you were going at with trying that debugging.

What I meant was, either create a reactor.bus.EventBus bean with another name tied to it (by using the @Qualifier({the-name-you-want-it-to-have})), or create an axon EventBus with another name. So adjust your configuration file to create either one of the EventBusses with a different name.

By doing that, there will not be a name clash in Spring, and thus the Axon EventBus will not get overwritten by the reactor.bus.EventBus.
And that should also mean you’ll not get the ClassCastException you’ve posted initially.

Hope this helps.

Cheers,

Steven

I did that already with no luck
Same error

El El vie, 4 ago 2017 a las 12:04, Steven van Beelen <steven.vanbeelen@axoniq.io> escribió:

Hi Fernando,

I’ve managed to reproduce an issue that is slightly different than this problem, but may be a sign of what’s going on here.

In the complex dance between Axon and Spring, there is a piece of configuration in Axon that makes Spring believe it should use the bean name “eventBus” to locate the Event Bus, even if another name has been explicitly assigned to it.

So if you name your Axon EventBus (or Store) to ‘eventBus’ and rename the other one, you should be able to work around the issue.

Cheers,

Allard

Finally we got it working with other bean name, and finally we needed to override default processor. tons of time of dbugging but reached the goal !!!

@Bean
EventStore embeddedEventStore() {
    MongoEventStorageEngine storageEngine = new MongoEventStorageEngine(jacksonSerializer(), null, axonMongoTemplate(), new
            DocumentPerEventStorageStrategy())
    EventStore eventStore = new EmbeddedEventStore(storageEngine)
    eventStore
}

@Bean(name = 'eventBas')
@Primary
EventBus eventBas(EventStore eventStore) {
    eventStore
}

@Bean
SpringAggregateSnapshotterFactoryBean springAggregateSnapshotterFactoryBean() {
    new SpringAggregateSnapshotterFactoryBean()
}

@Bean
EventSourcingRepository<FamilyGroup> axonFamilyGroupRepository(EventStore eventStore, SpringAggregateSnapshotterFactoryBean
        springAggregateSnapshotterFactoryBean, ApplicationContext applicationContext) {
    SpringAggregateSnapshotter snapshotter = springAggregateSnapshotterFactoryBean.object
    snapshotter.setApplicationContext(applicationContext)
    EventCountSnapshotTriggerDefinition snapshotTriggerDefinition = new EventCountSnapshotTriggerDefinition(
            snapshotter,
            SNAPSHOT_TRESHOLD)

    new EventSourcingRepository(FamilyGroup, eventStore, snapshotTriggerDefinition)
}

@Bean
EventSourcingRepository<NaturalPerson> axonNaturalPersonRepository(EventStore eventStore, SpringAggregateSnapshotterFactoryBean
        springAggregateSnapshotterFactoryBean, ApplicationContext applicationContext) {
    SpringAggregateSnapshotter snapshotter = springAggregateSnapshotterFactoryBean.object
    snapshotter.setApplicationContext(applicationContext)
    EventCountSnapshotTriggerDefinition snapshotTriggerDefinition = new EventCountSnapshotTriggerDefinition(
            snapshotter,
            SNAPSHOT_TRESHOLD)
    new EventSourcingRepository(NaturalPerson, eventStore, snapshotTriggerDefinition)
}

@Bean
EventSourcingRepository<Contract> axonContractRepository(EventStore eventStore, SpringAggregateSnapshotterFactoryBean
        springAggregateSnapshotterFactoryBean, ApplicationContext applicationContext) {
    SpringAggregateSnapshotter snapshotter = springAggregateSnapshotterFactoryBean.object
    snapshotter.setApplicationContext(applicationContext)
    EventCountSnapshotTriggerDefinition snapshotTriggerDefinition = new EventCountSnapshotTriggerDefinition(
            snapshotter,
            SNAPSHOT_TRESHOLD)

    new EventSourcingRepository(Contract, eventStore, snapshotTriggerDefinition)
}

@Bean
EventSourcingRepository<Kyc> axonKycRepository(EventStore eventStore, SpringAggregateSnapshotterFactoryBean
        springAggregateSnapshotterFactoryBean, ApplicationContext applicationContext) {
    SpringAggregateSnapshotter snapshotter = springAggregateSnapshotterFactoryBean.object
    snapshotter.setApplicationContext(applicationContext)
    EventCountSnapshotTriggerDefinition snapshotTriggerDefinition = new EventCountSnapshotTriggerDefinition(
            snapshotter,
            SNAPSHOT_TRESHOLD)

    new EventSourcingRepository(Kyc, eventStore, snapshotTriggerDefinition)
}

@Autowired
void configure(EventBus eventBus, EventHandlingConfiguration ehConfig) {
    ehConfig.registerSubscribingEventProcessor(PROCESSOR_CLASSPATH) { c -> eventBus }
    //ehConfig.registerEventHandler({c -> new ClientEventHandler()})
    ehConfig.byDefaultAssignTo(PROCESSOR_CLASSPATH)
}