我有一个实体List<Entity> entitiesList
的列表。我需要发布和存储每个实体的事件列表。
我有一个Entity的汇总,所有必需的处理程序,CreateEntityCommand和EntityCreatedEvent。
目前我正在做什么:
1.在循环中创建命令,并通过命令网关为entityList中的每个实体发送这些命令。
for (Entity entity : entitiesList) {
CreateEntityCommand createEntityCommand = new CreateEntityCommand();
… here I set command’s fields …
commandGateway.send(createEntityCommand);
}
@CommandHandler
public EntityAggregate(CreateEntityCommand createAlertCommand) {
EntityCreatedEvent entityCreatedEvent = new EntityCreatedEvent();
…. here I set event’s fields
AggregateLifecycle.apply(entityCreatedEvent);
}
结果是,事件被创建为发布并逐一保存到循环内的DomainEventEntry表中。 如果我有10000个实体,那么此过程将花费很多时间... 我的问题是–如何改善创建,发布和保存实体列表的过程?
我使用的是该版本的轴突
<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>
带有注释@SpringBootApplication的SpringBoot配置。 我尚未配置有关Axon的任何特定内容。
答案 0 :(得分:0)
我认为您需要的是并行处理命令以加快工作速度。有两种方法可以实现此目的:
CommandBus
我假设您在指针1上,因此我的回答将针对此要求。
当您使用Spring Boot获得Axon应用程序的单个实例时,将自动为您配置SimpleCommandBus
。这不会为并发工作提供任何可能性。因此,配置其他CommandBus
bean应该是解决之道。
我建议先开始使用AsynchronousCommandBus
。此实现使用Executor
(如果需要,可以进一步配置)来增加线程以分派(和处理)正在执行的每个命令。
如果这仍然使您不喜欢它,请尝试使用DisruptorCommandBus
(有关“ Disruptor”的具体信息,您可以查看here)。此CommandBus
实现将使用两个线程池。一个用于处理命令的池,另一个用于存储事件的池。
最后,如果您已经在使用CommandBus
的分布式版本(例如Axon Server或DistributedCommandBus
),则必须为CommandBus
bean提供附加到它的限定符“ localSegment”。要快速了解Axon提供的命令总线,请查看参考指南(上here)。