Axon保存事件应该先将事件保存到事件存储中,还是先发布到事件总线中?
创始人
2024-09-29 07:30:54
0

在 Axon 框架中,事件驱动架构(EDA)中的事件总线和事件存储是两个核心组件。当领域对象进行状态更改时,会生成一个或多个领域事件。这些事件将首先保存在事件存储中,然后通过事件总线进行传播。因此,Axon 的默认行为是首先将事件保存到事件存储中,然后将其发布到事件总线中。

下面是一个简单的代码示例,展示了如何使用 Axon 框架来处理事件:

首先,我们需要定义一个领域事件:

public class AccountCreatedEvent {
    private final String accountId;
    private final String ownerName;

    // Constructor and getters
}

接下来,我们需要定义一个领域对象:

@Aggregate
public class Account {
    @AggregateIdentifier
    private String accountId;

    private String ownerName;

    // Define command-handling methods that update the object state
    @CommandHandler
    public void createAccount(CreateAccountCommand command) {
        apply(new AccountCreatedEvent(command.getAccountId(), command.getOwnerName()));
    }

    // Define event-handling methods that update the object state in response to domain events
    @EventSourcingHandler
    public void on(AccountCreatedEvent event) {
        this.accountId = event.getAccountId();
        this.ownerName = event.getOwnerName();
    }
}

在这个示例中,Account 领域对象具有一个 createAccount 命令处理程序,它将 AccountCreatedEvent 领域事件应用于 AccountAccount 对象还具有一个事件处理程序,该处理程序将 AccountCreatedEvent 领域事件应用于内部状态。

最后,我们可以使用 Axon 注册我们的领域对象和命令处理程序,在这个过程中,Axon 将自动配置其默认的事件保存和发布行为:

@Configuration
public class AxonConfig {
    @Bean
    public Account account() {
        return new Account();
    }

    @Bean
    public CommandHandlerInterceptor commandHandlerInterceptor() {
        return new BeanValidationInterceptor<>();
    }

    // Configure the event store and event bus
    @Bean
    public EventStorageEngine eventStorageEngine() {
        return new InMemoryEventStorageEngine();
    }

    @Bean
    public EventBus eventBus

相关内容

热门资讯

安卓换鸿蒙系统会卡吗,体验流畅... 最近手机圈可是热闹非凡呢!不少安卓用户都在议论纷纷,说鸿蒙系统要来啦!那么,安卓手机换上鸿蒙系统后,...
app安卓系统登录不了,解锁登... 最近是不是你也遇到了这样的烦恼:手机里那个心爱的APP,突然就登录不上了?别急,让我来帮你一步步排查...
安卓系统拦截短信在哪,安卓系统... 你是不是也遇到了这种情况:手机里突然冒出了很多垃圾短信,烦不胜烦?别急,今天就来教你怎么在安卓系统里...
安卓系统要维护多久,安卓系统维... 你有没有想过,你的安卓手机里那个陪伴你度过了无数日夜的安卓系统,它究竟要陪伴你多久呢?这个问题,估计...
windows官网系统多少钱 Windows官网系统价格一览:了解正版Windows的购买成本Windows 11官方价格解析微软...
安卓系统如何卸载app,轻松掌... 手机里的App越来越多,是不是感觉内存不够用了?别急,今天就来教你怎么轻松卸载安卓系统里的App,让...
怎么复制照片安卓系统,操作步骤... 亲爱的手机控们,是不是有时候想把自己的手机照片分享给朋友,或者备份到电脑上呢?别急,今天就来教你怎么...
安卓系统应用怎么重装,安卓应用... 手机里的安卓应用突然罢工了,是不是让你头疼不已?别急,今天就来手把手教你如何重装安卓系统应用,让你的...
iwatch怎么连接安卓系统,... 你有没有想过,那款时尚又实用的iWatch,竟然只能和iPhone好上好?别急,今天就来给你揭秘,怎...
iphone系统与安卓系统更新... 最近是不是你也遇到了这样的烦恼?手机更新系统总是失败,急得你团团转。别急,今天就来给你揭秘为什么iP...