Cancel execution of command and subsequent events in axon 2.4

Hi all.

I have the following scenario

I issue CreateGameCommand

which triggers 3 events:

apply(event1);
apply(event2);

apply(event3);

Is it possible if handling of event1 throws RuntimeException to cancel all subsequent events (event2 and event3) ?

I use SequentialPerAggregatePolicy .

I currently don’t see such option in axon to cancel execution of next events for this command.

Thanks in advance.

Hi Borislav,

If your event handler is registered with a SimpleCluster an exception in the event handler will result in a rollback of the unit of work and database transaction. This is because the SimpleCluster processes events in the publishing thread.

However, your event handler is part of an AsynchronousCluster which processes events asynchronously. In that case an exception from an event handler will not result in a rollback.

So, to answer your question: yes, it is possible and is actually the default behavior. However, it is a very bad idea to depend on this in your application for many reasons:

  1. Event handlers should not be able to decide whether or not the event is ‘allowed’. The event is to register that something has happened and therefore should never be rolled back.

  2. Side effects. If another handler has performed an action based on the event that cannot be rolled back, eg the event was forwarded to a web client or the other handler was part of an async cluster, your application will be (permanently) inconsistent.

  3. If you change the configuration of your application without changing any code the behavior may be different. Eg if you move this handler from a SimpleCluster back to an AsynchronousCluster your application will behave differently.

  4. The order in which event handlers receive the event may be different each time you run the application so its behavior may vary from run to run.

So, don’t do this ;). There are better ways. What’s the real issue you want to solve here?

Best,
Rene

Many Thanks.
I will find another way to do my work.

вторник, 12 април 2016 г., 19:37:47 UTC+3, René de Waele написа: