0
votes

I have a list of entities List<Entity> entitiesList. I need to publish and store the list of events for each of entity. I have an aggregate for Entity , all necessary handlers, CreateEntityCommand and EntityCreatedEvent. Currently what I do: 1. Create commands in the loop and send these commands via command gateway for each entity from entitiesList.

for (Entity entity : entitiesList) {
               CreateEntityCommand createEntityCommand = new CreateEntityCommand();
                …   here I set command’s fields  …
               commandGateway.send(createEntityCommand);
}
  1. Inside the aggregate I have
@CommandHandler
    public EntityAggregate(CreateEntityCommand createAlertCommand) {
            EntityCreatedEvent entityCreatedEvent = new EntityCreatedEvent();
                   …. here I set event’s fields
            AggregateLifecycle.apply(entityCreatedEvent);

    }

As the result, the events are created published and saved into the DomainEventEntry table inside the loop one by one. If I have 10000 of entities – this process takes a lot of time … My question is – how can I improve this process of creating, publishing and saving a list of entities ?

I use this version of axon:

<dependency>
        <groupId>org.axonframework</groupId>
        <artifactId>axon-spring-boot-starter</artifactId>
        <version>4.3</version>
        <exclusions>
            <exclusion>
                <groupId>org.axonframework</groupId>
                <artifactId>axon-server-connector</artifactId>
            </exclusion>
        </exclusions>
 </dependency>

SpringBoot configuration with annotation @SpringBootApplication. I haven't configured anything specific around Axon.

1
So, prior to actually answering your question, I need to have some additional info from your part: 1. Which version of Axon are you using? 2. Are you using Spring Boot auto configuration? 3. Have you configured anything specific around Axon? I'd suggest to update your questions with the answers to these, so that they're immediately visible to anybody.Steven
I use axon-spring-boot-starter 4.3. SpringBoot configuration with annotation @SpringBootApplication. I haven't configured anything specific around Axon.Kseniia

1 Answers

0
votes

What I believe you need is to parallelize the processing of the commands to speed up the work. There are two ways to achieve this:

  1. Change the local CommandBus
  2. Distributed your application

I am assuming you are on pointer 1, hence my answer will be tailored towards this.

When you have a single instance of an Axon application using Spring Boot a SimpleCommandBus will be auto configured for you. This does not provide any possibility for concurrent work. Thus configuring a different CommandBus bean should be the way to go.

I'd suggest to first start using the AsynchronousCommandBus. This implementation uses an Executor (which you can further configure if you so desire) to spin up threads to dispatch (and handle) each command going through.

If this is still to slow to your liking, I'd try out the DisruptorCommandBus (for specifics on what "Disruptor" is, you can look here). This CommandBus implementation will use two threads pools; one pool for handling commands and another for storing the events.

Lastly, if you are already working with a distributed version of the CommandBus (like Axon Server, or with the DistributedCommandBus), you will have to provide a CommandBus bean with the qualifier "localSegment" attached to it. For a quick overview of the command buses provided by Axon, I'd have a look at there Reference Guide (up here).