Axon 2-$ migration: how to get next sequence number for an entity

Good day,

I'm in the process of migrating Axon 2->4, and am wondering how to go about the following change: 

Let's say, we want to apply the following change to an Aggregate (class extends AbstractAnnotatedAggregateRoot in v.2): 

apply(new ContractCreatedEvent(
public long getNextEventSequenceNumber() {
 if (getLastCommittedEventSequenceNumber() == null) {
 return getUncommittedEventCount();
 } else {
 return getLastCommittedEventSequenceNumber() + getUncommittedEventCount() + 1;

In v.4 I could not find any way to get last entity's sequence number. 

Is there a way to do that? Or is it all now done automatically?

Many thanks,


Hi Nestor,

You are no longer required to populate an event’s sequence number your self.
The ContractCreatedEvent in your sample is what I’d regard as the event payload.

This payload will be wrapped in an EventMessage - it is this EventMessage which will carry the sequence number.

It will be set automatically if you perform the AggregateLifecycle#apply(Object...) function.
So, another short note - in Axon 3 (and 4) you no longer extend from the AbstractAnnotatedAggregateRoot to create an aggregate.
In a Spring environment, you’d simply annotate the aggregate with @Aggregate.
In a non-Spring environment, you should get around with correctly configuring your aggregate with the AggregateConfigurer.

The earlier mentioned static apply function is what you should use to publish events in Axon 3 and up - sequence numbers will be added automatically.

Hope this helps!