【问题标题】:Axon framework- storing a list of eventsAxon 框架 - 存储事件列表
【发布时间】:2020-05-06 08:42:56
【问题描述】:

我有一个实体列表List<Entity> entitiesList。我需要发布和存储每个实体的事件列表。 我有一个 Entity 聚合,所有必要的处理程序,CreateEntityCommand 和 EntityCreatedEvent。 目前我在做什么: 1. 在循环中创建命令,并通过命令网关为实体列表中的每个实体发送这些命令。

for (Entity entity : entitiesList) {
               CreateEntityCommand createEntityCommand = new CreateEntityCommand();
                …   here I set command’s fields  …
               commandGateway.send(createEntityCommand);
}
  1. 在我的聚合中
@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 周围配置任何特定的东西。

【问题讨论】:

  • 所以,在真正回答您的问题之前,我需要您提供一些额外的信息: 1. 您使用的是哪个版本的 Axon? 2.你使用的是Spring Boot自动配置吗? 3. 您是否针对 Axon 配置了任何特定内容?我建议用这些问题的答案更新你的问题,这样任何人都可以立即看到它们。
  • 我使用 axon-spring-boot-starter 4.3。带有注解 @SpringBootApplication 的 SpringBoot 配置。我没有在 Axon 周围配置任何特定的东西。

标签: axon


【解决方案1】:

我认为您需要的是并行处理命令以加快工作速度。有两种方法可以实现:

  1. 更改本地CommandBus
  2. 分发您的应用程序

我假设您在指针 1 上,因此我的答案将针对此进行调整。

当您有一个使用 Spring Boot 的 Axon 应用程序实例时,将为您自动配置 SimpleCommandBus。这不会为并发工作提供任何可能性。因此配置不同的CommandBus bean 应该是可行的方法。

我建议首先开始使用AsynchronousCommandBus。此实现使用Executor(如果您愿意,可以进一步配置)来启动线程以调度(和处理)每个通过的命令。

如果这仍然让你觉得慢,我会试试DisruptorCommandBus(关于什么是“Disruptor”的细节,你可以看看here)。这个CommandBus 实现将使用两个线程池;一个用于处理命令,另一个用于存储事件。

最后,如果您已经在使用 CommandBus 的分布式版本(如 Axon 服务器,或使用 DistributedCommandBus),则必须提供带有限定符“localSegment”的 CommandBus bean它。要快速了解 Axon 提供的命令总线,我会查看参考指南(here)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    相关资源
    最近更新 更多