Is there any example showing usage of Axon Framework's Event without involving Event Sourcing?

Hi All

I’m very new to CQRS and Axon Framework.

I have been able to successfully use the following elements for Axon Framework in an existing system:

  • Commands
  • Command Handlers
    What I will like to find out is how can I leverage on Axon Framework’s Event if I do not want to use the Event Sourcing portion?

Thanks for any advice.

Regards
Ian Lim

Hi Ian,
Axon doesn’t require you to use event sourcing. Just publish an event on the eventBus and use the @EventHandler annotation for subscribers.

The easiest in terms of infrastructure is when your event bus is a simple synchronous event bus. This way, the publishing and the handling of the event all happen in the same thread in the same transaction. Be careful however that your event handlers only do things that fall under the transaction and don’t do anything that cannot be rolled back, like sending emails. In the latter case, you must make sure the event handling occurs after the transaction that published the event. This can be done using an XA transaction and storing the event in a durable JMS topic. Or you can do this by keeping the event in a table until the event was successfully handled by all registered subscribers. Axon doen’t really add an out-of-the-box solution for that so you would need to write your own eventBus implementation.

Greetings
Jan

Hi Ian,

your Aggregate root can simply extend from AbstractAggregateRoot if you don’t care about event sourcing. If you’re using JPA, you can use the GenericJpaRepository to reconstruct your aggregate based on the data in a database. To publish events, you can use the “registerEvent” method. This will use the UnitOfWork to ensure the events are published at an appropriate time.

If you don’t use Aggregates at all, and wish to publish events from an @CommandHandler directly, consider using the EventTemplate instead of using the EventBus directly. The former will use the UnitOfWork to coordinate activity. The Events will be published when the Unit of Work commits.

If you have activity with side effects, as Jan mentioned, consider using a UnitOfWorkListener to perform this action when the Unit of Work has finished committing (onAfterCommit). This way, your side effect will only happen when the transaction has successfully completed. You can use the static CurrentUnitOfWork.get() to get a reference to the Unit of Work. If you use @EventHandler anntotated methods, you can add a parameter of type UnitOfWork to your method as well. Axon will inject the current UoW for you. On this UnitOfWork, use registerListener(…) to register a listener.

Hope this helps.
Cheers,

Allard

Hi Allard,

Do you need any versioned repository in this case? Also, how can you publish events from within the AbstractAggregateRoot?

Thanks,
-p

Hi,

you’ll always need a repository. Even when aggregates publish events (which is possible).
To publish an event from an aggregate (that extends AbstractAggregateRoot) simply call registerEvent(…) to register an event for publication. When the Unit of Work commits, those events are published to the event bus.

Cheers,

Allard