Cannot retrieve current AggregateLifecycle with a cache

Hello all,

I am facing the famous “Cannot retrieve current AggregateLifecycle; none is yet defined” error.

I already checked all the possible errors: using an apply outside of the aggregate, creating myself an aggregate instead of letting the repository doing it etc… In theory everything has been correctly applied.

Yet I can see this error occurs only when an aggregate cache (a custom implementation of AbstractCacheAdapter using either a local map or Guava) has been set up. The weirdest thing is that when I use a Hazelcast map instead (by autowiring a Map), the same code is working.

I guess something is going wrong here. The resources map has only one key named org.axonframework.eventsourcing.CachingEventSourcingRepository@4fd63c43_AGGREGATES, not “ManagedAggregates”.

Thanks

Let me shed some light on how this AggregateLifecycle works. Because apply is a static method, there is no way for Axon to find out who’s calling it (in a regular instance method, it would be the same issue). For that, and some other reasons, Axon wraps your Aggregate Root in a different class, which implements the Aggregate interface. These classes typically extend from AggregateLifecycle. When invoking an execute() or handle() method on this Aggregate, it registers itself with the lifecycle as “the current aggregate”. That’s what the thread local variable CURRENT (here) is for. That should reasonable never be null. However, if it is, there is a second chance, which is the ManagedAggregates resource in the Unit of Work.

Since apparently both CURRENT and the ManagedAggregates are null, I expect there is a call to the AggregateRoot somewhere that didn’t flow through an Aggregate instance. Can you share the stacktrace of the exception?

Allard

I get the idea (I guess) thanks.

The stacktrace:

`

2017-11-17 12:38:46 ERROR c.s.f.h.flight.InitialCommandHandler - Error while applying initial command
java.lang.IllegalStateException: Cannot retrieve current AggregateLifecycle; none is yet defined
at org.axonframework.commandhandling.model.AggregateLifecycle.getInstance(AggregateLifecycle.java:118)
at org.axonframework.commandhandling.model.AggregateLifecycle.apply(AggregateLifecycle.java:67)
at com.x.domain.model.aggregate.flight.FlightPlanAggregate.initialWithExistingFP(FlightPlanAggregate.java:265)
at com.x.handler.flight.InitialCommandHandler.onInitialCommand(InitialCommandHandler.java:42)
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.axonframework.messaging.annotation.AnnotatedMessageHandlingMember.handle(AnnotatedMessageHandlingMember.java:127)
at org.axonframework.messaging.annotation.WrappedMessageHandlingMember.handle(WrappedMessageHandlingMember.java:61)
at org.axonframework.commandhandling.AnnotationCommandHandlerAdapter.handle(AnnotationCommandHandlerAdapter.java:94)
at org.axonframework.commandhandling.AnnotationCommandHandlerAdapter.handle(AnnotationCommandHandlerAdapter.java:38)
at org.axonframework.messaging.DefaultInterceptorChain.proceed(DefaultInterceptorChain.java:57)
at org.axonframework.messaging.unitofwork.DefaultUnitOfWork.executeWithResult(DefaultUnitOfWork.java:69)
at org.axonframework.commandhandling.SimpleCommandBus.doDispatch(SimpleCommandBus.java:143)
at org.axonframework.commandhandling.SimpleCommandBus.doDispatch(SimpleCommandBus.java:116)
at org.axonframework.commandhandling.AsynchronousCommandBus.access$001(AsynchronousCommandBus.java:40)
at org.axonframework.commandhandling.AsynchronousCommandBus$DispatchCommand.run(AsynchronousCommandBus.java:94)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
2017-11-17 12:38:46 DEBUG c.s.f.cache.AggregateCache - Aggregate cache put flightplan.0cb995b8-a228-4fde-bbbf-565b864a5801
2017-11-17 12:38:46 ERROR c.s.f.client.CommandService - Command execution exception
org.axonframework.commandhandling.CommandExecutionException: An exception occurred while executing a command
at org.axonframework.commandhandling.callbacks.FutureCallback.asRuntime(FutureCallback.java:111)
at org.axonframework.commandhandling.callbacks.FutureCallback.getResult(FutureCallback.java:72)
at org.axonframework.commandhandling.gateway.DefaultCommandGateway.sendAndWait(DefaultCommandGateway.java:114)
at com.x.client.CommandService.sendSyncCommand(CommandService.java:51)
at com.x.client.async.FlightObjectAsyncService.onInitial(FlightObjectAsyncService.java:85)
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.messaging.handler.invocation.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:181)
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:114)
at org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:114)
at org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:77)
at org.springframework.jms.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:744)
at org.springframework.jms.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:704)
at org.springframework.jms.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:674)
at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.doReceiveAndExecute(AbstractPollingMessageListenerContainer.java:318)
at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.receiveAndExecute(AbstractPollingMessageListenerContainer.java:257)
at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.invokeListener(DefaultMessageListenerContainer.java:1173)
at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.executeOngoingLoop(DefaultMessageListenerContainer.java:1165)
at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.run(DefaultMessageListenerContainer.java:1062)
at java.lang.Thread.run(Thread.java:745)
Caused by: com.x.exception.CommandHandlingException: Error while applying initial command
at com.x.handler.flight.InitialCommandHandler.onInitialCommand(InitialCommandHandler.java:45)
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.axonframework.messaging.annotation.AnnotatedMessageHandlingMember.handle(AnnotatedMessageHandlingMember.java:127)
at org.axonframework.messaging.annotation.WrappedMessageHandlingMember.handle(WrappedMessageHandlingMember.java:61)
at org.axonframework.commandhandling.AnnotationCommandHandlerAdapter.handle(AnnotationCommandHandlerAdapter.java:94)
at org.axonframework.commandhandling.AnnotationCommandHandlerAdapter.handle(AnnotationCommandHandlerAdapter.java:38)
at org.axonframework.messaging.DefaultInterceptorChain.proceed(DefaultInterceptorChain.java:57)
at org.axonframework.messaging.unitofwork.DefaultUnitOfWork.executeWithResult(DefaultUnitOfWork.java:69)
at org.axonframework.commandhandling.SimpleCommandBus.doDispatch(SimpleCommandBus.java:143)
at org.axonframework.commandhandling.SimpleCommandBus.doDispatch(SimpleCommandBus.java:116)
at org.axonframework.commandhandling.AsynchronousCommandBus.access$001(AsynchronousCommandBus.java:40)
at org.axonframework.commandhandling.AsynchronousCommandBus$DispatchCommand.run(AsynchronousCommandBus.java:94)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
… 1 common frames omitted
Caused by: java.lang.IllegalStateException: Cannot retrieve current AggregateLifecycle; none is yet defined
at org.axonframework.commandhandling.model.AggregateLifecycle.getInstance(AggregateLifecycle.java:118)
at org.axonframework.commandhandling.model.AggregateLifecycle.apply(AggregateLifecycle.java:67)
at com.x.domain.model.aggregate.flight.FlightPlanAggregate.initialWithExistingFP(FlightPlanAggregate.java:265)
at com.x.handler.flight.InitialCommandHandler.onInitialCommand(InitialCommandHandler.java:42)
… 17 common frames omitted
2017-11-17 12:38:46 ERROR c.s.f.c.a.FlightObjectAsyncService - Command exception
com.x.exception.CommandHandlingException: Error while applying initial command
at com.x.handler.flight.InitialCommandHandler.onInitialCommand(InitialCommandHandler.java:45)
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.axonframework.messaging.annotation.AnnotatedMessageHandlingMember.handle(AnnotatedMessageHandlingMember.java:127)
at org.axonframework.messaging.annotation.WrappedMessageHandlingMember.handle(WrappedMessageHandlingMember.java:61)
at org.axonframework.commandhandling.AnnotationCommandHandlerAdapter.handle(AnnotationCommandHandlerAdapter.java:94)
at org.axonframework.commandhandling.AnnotationCommandHandlerAdapter.handle(AnnotationCommandHandlerAdapter.java:38)
at org.axonframework.messaging.DefaultInterceptorChain.proceed(DefaultInterceptorChain.java:57)
at org.axonframework.messaging.unitofwork.DefaultUnitOfWork.executeWithResult(DefaultUnitOfWork.java:69)
at org.axonframework.commandhandling.SimpleCommandBus.doDispatch(SimpleCommandBus.java:143)
at org.axonframework.commandhandling.SimpleCommandBus.doDispatch(SimpleCommandBus.java:116)
at org.axonframework.commandhandling.AsynchronousCommandBus.access$001(AsynchronousCommandBus.java:40)
at org.axonframework.commandhandling.AsynchronousCommandBus$DispatchCommand.run(AsynchronousCommandBus.java:94)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: Cannot retrieve current AggregateLifecycle; none is yet defined
at org.axonframework.commandhandling.model.AggregateLifecycle.getInstance(AggregateLifecycle.java:118)
at org.axonframework.commandhandling.model.AggregateLifecycle.apply(AggregateLifecycle.java:67)
at com.x.domain.model.aggregate.flight.FlightPlanAggregate.initialWithExistingFP(FlightPlanAggregate.java:265)
at com.x.handler.flight.InitialCommandHandler.onInitialCommand(InitialCommandHandler.java:42)
… 17 common frames omitted

`

Oh I found the problem thanks to your response. Let me describe it if it might help other people.

Basically after having loaded my aggregate, I was doing a :

`
MyAggregate existingFlightPlanAggregate = agg.invoke(a -> a.self());

`

Just because I found it easier to manipulate directly my aggregate instead of doing each time an invoke or execute.
That was the problem actually. Removing this stupid statement and using only invoke or execute resolved the problem.

Thanks Allard.