Using M5

Hello

I was wondering if someone can help me with the following:

I have a commandhandler that is called using @CommandHandler annotation. When I use the Apply static method the event is not getting triggered. Debugging the framework it is failing in the following place

protected static AggregateLifecycle getInstance() {
    AggregateLifecycle instance = CURRENT.get();
    if (instance == null && CurrentUnitOfWork.isStarted()) {
        UnitOfWork<?> unitOfWork = CurrentUnitOfWork.get();
        Set<AggregateLifecycle> managedAggregates = unitOfWork.getResource("ManagedAggregates"); --> this is null
        if (managedAggregates != null && managedAggregates.size() == 1) {
            instance = managedAggregates.iterator().next();
        }
    }
    if (instance == null) {
        throw new IllegalStateException("Cannot retrieve current AggregateLifecycle; none is yet defined"); -->this is the error I am getting
    }
    return instance;
}

Could you please help me what I am doing wrong. The command and the event are very simple and are located in different files.

Richard

Hi Richard,

the apply() method may only be used in Aggregates, whise lifecycle is managed by Axon. This exception occurs when using apply() in an @CommandHandler method on a Spring bean, for example.
Instead, put the methid on an Aggregate and simply tell Axon to manage it for you (using the Configuration API or by annotating your Agrregate Root with @Aggregate when using Spring. Axon will do the rest.

Cheers,

Allard