Command handler interceptor throws exception when I call method aggregate.handle() with CommandMessage as the argument

I have an event listener as follows. When some events occurred, it will load an aggregate from the repository, and call the aggraget.handle() method with a CommandMessage as argument:

`

@EventHandler
private void handle(SomeEvent event, MetaData metaData) throws Exception {
Aggregate aggregate = orderRepository.load(event.orderId().toString());
aggregate.handle(GenericCommandMessage.asCommandMessage(
SomeCommand.builder()
.orderId(event.orderId())
.build())
.withMetaData(metaData));
}

`

In the Order aggregate, I have a command handler interceptor:

`

@Aggregate
public class ShippingOrder {
@CommandHandlerInterceptor
private Object checkStatus(Object command, InterceptorChain interceptorChain) throws Exception {
if (this.status == OrderStatus.ACTIVE) {
return interceptorChain.proceed();
} else {
log.info(“Order {} is not in ACTIVE state, command {} ignored.”, this.id, command.getClass().getName());
return null;
}
}

@CommandHandler
private void handle(SomeCommand command) {
apply(SomeNewEvent.builder()
.orderId(this.id)
.build());
}
}

`

Then, I got the following exception:

`

2019-03-01 16:46:10.293 ERROR 29719 — [mand.service]-0] o.a.eventhandling.LoggingErrorHandler : EventListener [AggregateCoordinator] failed to handle event [fbfbfb63-03ee-43dc-967c-3089dc9cbad5] (io.smartcargo.forwarder.message.event.CustomsDeclarationDeleted). Continuing processing with next listener

java.lang.ClassCastException: org.axonframework.eventhandling.GenericTrackedDomainEventMessage cannot be cast to org.axonframework.commandhandling.CommandMessage
at org.axonframework.messaging.DefaultInterceptorChain.proceed(DefaultInterceptorChain.java:57) ~[axon-messaging-4.0.3.jar:4.0.3]
at io.smartcargo.forwarder.oms.command.domain.model.ShippingOrder.checkStatus(ShippingOrder.java:87) ~[main/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_161]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_161]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_161]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_161]
at org.axonframework.messaging.annotation.AnnotatedMessageHandlingMember.handle(AnnotatedMessageHandlingMember.java:127) ~[axon-messaging-4.0.3.jar:4.0.3]
at org.axonframework.messaging.annotation.WrappedMessageHandlingMember.handle(WrappedMessageHandlingMember.java:61) ~[axon-messaging-4.0.3.jar:4.0.3]
at org.axonframework.modelling.command.inspection.AnnotatedCommandHandlerInterceptor.handle(AnnotatedCommandHandlerInterceptor.java:55) ~[axon-modelling-4.0.3.jar:4.0.3]
at org.axonframework.messaging.DefaultInterceptorChain.proceed(DefaultInterceptorChain.java:55) ~[axon-messaging-4.0.3.jar:4.0.3]
at org.axonframework.modelling.command.inspection.AnnotatedAggregate.handle(AnnotatedAggregate.java:433) ~[axon-modelling-4.0.3.jar:4.0.3]
at org.axonframework.modelling.command.inspection.AnnotatedAggregate.lambda$handle$3(AnnotatedAggregate.java:400) ~[axon-modelling-4.0.3.jar:4.0.3]
at org.axonframework.messaging.Scope.executeWithResult(Scope.java:111) ~[axon-messaging-4.0.3.jar:4.0.3]
at org.axonframework.modelling.command.inspection.AnnotatedAggregate.handle(AnnotatedAggregate.java:407) ~[axon-modelling-4.0.3.jar:4.0.3]
at org.axonframework.modelling.command.LockAwareAggregate.handle(LockAwareAggregate.java:82) ~[axon-modelling-4.0.3.jar:4.0.3]
at io.smartcargo.forwarder.oms.command.service.AggregateCoordinator.handle(AggregateCoordinator.java:40) ~[main/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_161]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_161]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_161]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_161]
at org.axonframework.messaging.annotation.AnnotatedMessageHandlingMember.handle(AnnotatedMessageHandlingMember.java:127) ~[axon-messaging-4.0.3.jar:4.0.3]
at org.axonframework.eventhandling.AnnotationEventHandlerAdapter.handle(AnnotationEventHandlerAdapter.java:81) ~[axon-messaging-4.0.3.jar:4.0.3]
at org.axonframework.eventhandling.SimpleEventHandlerInvoker.handle(SimpleEventHandlerInvoker.java:111) ~[axon-messaging-4.0.3.jar:4.0.3]
at org.axonframework.eventhandling.MultiEventHandlerInvoker.handle(MultiEventHandlerInvoker.java:79) [axon-messaging-4.0.3.jar:4.0.3]
at org.axonframework.eventhandling.AbstractEventProcessor.lambda$null$1(AbstractEventProcessor.java:141) [axon-messaging-4.0.3.jar:4.0.3]
at org.axonframework.messaging.DefaultInterceptorChain.proceed(DefaultInterceptorChain.java:57) ~[axon-messaging-4.0.3.jar:4.0.3]
at org.axonframework.messaging.interceptors.CorrelationDataInterceptor.handle(CorrelationDataInterceptor.java:65) ~[axon-messaging-4.0.3.jar:4.0.3]
at org.axonframework.messaging.DefaultInterceptorChain.proceed(DefaultInterceptorChain.java:55) ~[axon-messaging-4.0.3.jar:4.0.3]
at org.axonframework.eventhandling.TrackingEventProcessor.lambda$new$1(TrackingEventProcessor.java:132) ~[axon-messaging-4.0.3.jar:4.0.3]
at org.axonframework.messaging.DefaultInterceptorChain.proceed(DefaultInterceptorChain.java:55) ~[axon-messaging-4.0.3.jar:4.0.3]
at org.axonframework.eventhandling.AbstractEventProcessor.lambda$processInUnitOfWork$2(AbstractEventProcessor.java:148) [axon-messaging-4.0.3.jar:4.0.3]
at org.axonframework.messaging.unitofwork.BatchingUnitOfWork.executeWithResult(BatchingUnitOfWork.java:86) ~[axon-messaging-4.0.3.jar:4.0.3]
at org.axonframework.eventhandling.AbstractEventProcessor.processInUnitOfWork(AbstractEventProcessor.java:136) [axon-messaging-4.0.3.jar:4.0.3]
at org.axonframework.eventhandling.TrackingEventProcessor.processBatch(TrackingEventProcessor.java:259) ~[axon-messaging-4.0.3.jar:4.0.3]
at org.axonframework.eventhandling.TrackingEventProcessor.processingLoop(TrackingEventProcessor.java:181) ~[axon-messaging-4.0.3.jar:4.0.3]
at org.axonframework.eventhandling.TrackingEventProcessor$TrackingSegmentWorker.run(TrackingEventProcessor.java:661) ~[axon-messaging-4.0.3.jar:4.0.3]
at org.axonframework.eventhandling.TrackingEventProcessor$WorkerLauncher.run(TrackingEventProcessor.java:771) ~[axon-messaging-4.0.3.jar:4.0.3]
at org.axonframework.eventhandling.TrackingEventProcessor$CountingRunnable.run(TrackingEventProcessor.java:588) ~[axon-messaging-4.0.3.jar:4.0.3]
at java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_161]

`

However, if I remove the command handler interceptor, everything works fine!

So is there a bug in the Axon command handler interceptor processing code?

Hi Xi,

To simplify your set up, I’d suggest to simply publish a command when you handle the SomeEvent, instead of loading the Aggregate manually.
Doing that will ensure the interceptors are called as you’d expect them to be called.

Additionally, you will have decoupled the notion of the Aggregate entirely from your Event Handler.
I’d view this as a good thing, as your Event Handling Classes shouldn’t be bothered who’s going to handle the given command, you just want the command to be handled by something.

A part from my suggestion though, it’s weird that the DefaultInterceptorChain is dealing with a GenericTrackedDomainEventMessage to begin with.
I’d agree this can be regarded as a bug.
If you’d be up to sharing this on the issue tracker for further investigation on our part, that would be very helpful.

Hope this helps you out Xi!

Cheers,
Steven