Axon保存事件应该先将事件保存到事件存储中,还是先发布到事件总线中?
创始人
2024-09-29 07:30:37
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

相关内容

热门资讯

透视脚本!德普之星辅助器(透视... 透视脚本!德普之星辅助器(透视)本来真的是有挂(详细辅助2025新版总结)1、很好的工具软件,可以解...
分享一款!pokemmo手机版... 分享一款!pokemmo手机版脚本,hhpoker透视方法,2025新版(有挂技巧);支持多人共享记...
一起来讨论!hhpoker外挂... 一起来讨论!hhpoker外挂靠谱吗,德普之星透视软件免费入口官网,2025新版教程(有挂攻略)1、...
透视智能ai!xpoker辅助... 透视智能ai!xpoker辅助神器(透视)好像真的有挂(详细辅助AA德州教程)1、完成透视辅助安装,...
揭秘!wepoker永久免费脚... 自定义aapoker免费透视脚本系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅...
透视中牌率!wepoker永久... 透视中牌率!wepoker永久免费脚本(透视)都是真的有挂(详细辅助系统教程);1、让任何用户在无需...
透视玄学!hhpoker底牌透... 透视玄学!hhpoker底牌透视脚本(透视)一直是真的有挂(详细辅助技巧教程)1、用户打开应用后不用...
总算了解!uupoker透视,... 1、总算了解!uupoker透视,wpk私人辅助,技巧教程(有挂技巧)。2、wpk私人辅助透视辅助简...
透视挂!德普之星私人局透视(透... 透视挂!德普之星私人局透视(透视)一直有挂(详细辅助软件教程)一、AI软件牌型概率发牌机制”必胜“技...
科技分享!菠萝辅助器免费版的特... 科技分享!菠萝辅助器免费版的特点,pokermaster辅助器,线上教程(有挂软件)准备好在菠萝辅助...