Batch Processing Using Axon

Currently I’m using axon.version 3.4.2 and spring boot 2.1.1
I have an array of Records, where each record has a particular state, each state has its own event manager
Something like that:

aggregate identifier | secuenceNumber | payLoadType
2fcb 0 ModifyEvent
2fcb 1 AssignEvent
2fcb 2 ApprovalEvent

next event CloseEvent

Questions

1.- How to ignore the sequence and get the last event? in this case, I should only get the last ApprovalEvent to continue with closeEvent.

2.- The execution time in the example takes too much for each command, which is the best solution for
implement batch processing (eg send 100 records to change states) using Axon?

`
@Aggregate

`
public class Allocate extends BaseAggregate {
@CommandHandler
public void on(ModifyEvent command, GetBeanService service){

}
@EventSourcingHandler
public void on(ModifyEvent event){

}
@CommandHandler
public Allocate(AssignEvent command, GetBeanService service) {

}
@EventSourcingHandler
public void on(AssignEvent event) {

}
@CommandHandler
public void on(ApprovalEvent command, GetBeanService service) {

}
@EventSourcingHandler
public void on(ApprovalEvent event) {

}
@CommandHandler
public void on(CloseEvent command, GetBeanService service) {

}
@EventSourcingHandler
public void on(CloseEvent event){

}
}

Any ideas to improve our solution? Any workaround?

thank you very much for your help.

Best Regards
Nelson

Hi Nelson,

From your snippet it seems that you’re using event messages as command messages.
I would strongly suggest to decouple the command messages in your system from the event messages, following the suggested routes based on their characteristics.

Thus, a command should be send to a single source, typically an Aggregate, which decides whether to perform a certain operation.

The command handler of that command is thus the decision making process, which can decide against the requested operation by not doing anything or throwing an exception or it can approve the in coming command.
Upon approval you’d typically publish an Event, notifying everybody that a decision has been made.

I share the above to point out the difference between a command and event:

  • Command: the request to perform some action
  • Event: the notification something has happened

In your example, you thus have a CloseAllocationCommand, requesting to close the Allocation.
If the targeted Allocation Aggregate agrees with this request, it will publish an AllocationClosedEvent.
Notice that commands are present tense and events passed tense, pointing out that the first is the desire to close and the latter stating it has been closed.

Now, to actually end up with your questions:

  1. To ignore an Event, you would simply not handle it. Thus, not providing an Event (Sourcing) Handler for a given Event will ensure it is not taken into account.
  2. You cannot do a batch dispatch of commands, at all. You can however handle events in batches, but only for your Query Model. So your cannot have your Aggregate, the Command Model, handle commands in batches. Events can be handled in batches, by specifying the batch size for the Tracking Event Processor (Event Processor is the mechanism which provides events towards the your classes which have Event Handlers in them).
    If all this feels a little to vague, I could recommend to first read some architectural principles regarding the usages of CQRS, Event Sourcing and DDD, as specified here.
    If you’d want to dive into code with some minimal explanation of the concepts, I’d like to refer you to this video.

I hope this gives you some needed insight Nelson!

Cheers,
Steven