Axon 参考指南
  • 介绍
  • 架构概览
    • DDD & CQRS 概念
    • 事件溯源
    • 事件驱动的微服务
  • Axon Server
  • 发行说明
    • Axon Framework
      • Major Releases
      • Minor Releases
    • Axon Server
      • Major Releases
      • Minor Releases Standard Edition
      • Minor Releases Enterprise Edition
    • Axon Framework Extensions
      • AMQP
        • Major Releases
      • CDI
        • Major Releases
      • JGroups
        • Major Releases
      • Kafka
        • Major Releases
        • Minor Releases
      • Kotlin
        • Experimental Releases
      • Mongo
        • Major Releases
        • Minor Releases
      • Reactor
        • Major Releases
        • Minor Releases
      • Spring Cloud
        • Major Releases
        • Minor Releases
      • Tracing
        • Major Releases
        • Minor Releases
  • Getting Started
    • 快速开始
  • Axon Framework
    • 介绍
    • 消息传递概念
      • 消息剖析
      • 消息关联
      • 消息拦截
      • 支持带注解的处理程序
      • 异常处理
      • 工作单元
    • 命令
      • 建模
        • 聚合
        • 多实体聚合
        • 聚合状态存储
        • 从另一个聚合创建聚合
        • 聚合多态性
        • 解决冲突
      • 命令调度器
      • 命令处理程序
      • 基础设施
      • 配置
    • 事件
      • 事件调度器
      • 事件处理程序
      • 事件处理器
        • 订阅事件处理器
        • 流式事件处理器
      • 事件总线和事件存储
      • 事件版本控制
    • 查询
      • 查询处理
      • 查询调度器
      • 查询处理程序
      • 实现
      • 配置
    • 长时处理过程(Sagas)
      • 实现
      • 关联
      • 基础设施
    • Deadlines
      • Deadline Managers
      • Event Schedulers
    • 测试
      • 命令 / 事件
      • 长时处理过程(Sagas)
    • 序列化
    • 调整
      • 事件快照
      • 事件处理
      • 命令处理
    • 监控和指标
    • Spring Boot 集成
    • 模块
  • Axon Server
    • 介绍
    • 安装
      • 本地安装
        • Axon Server SE
        • Axon Server EE
      • Docker / K8s
        • Axon Server SE
        • Axon Server EE
    • 管理
      • 配置
        • System Properties
        • Command Line Interface
        • REST API
        • GRPC API
      • Monitoring
        • Actuator Endpoints
        • gRPC Metrics
        • Heartbeat Monitoring
      • Clusters
      • Replication Groups
      • Multi-Context
      • Tagging
      • Backup and Messaging-only Nodes
      • Backups
      • Recovery
      • Plugins
      • Error Codes
    • 安全
      • SSL
      • 访问控制
      • 访问控制 - 标准版
      • 访问控制 - 企业版
      • 访问控制 - 客户端应用程序
      • 访问控制 - 命令行
      • 访问控制 - REST API
      • 访问控制 - LDAP
      • 访问控制 - OAuth 2.0
    • 性能
      • 事件段
      • 流量控制
    • 迁移
      • Standard to Enterprise Edition
      • Non-Axon Server to Axon Server
  • Extensions
    • Spring AMQP
    • JGroups
    • Kafka
    • Kotlin
    • Mongo
    • Reactor
      • Reactor Gateways
    • Spring Cloud
    • Tracing
  • Appendices
    • A. RDBMS Tuning
    • B. Message Handler Tuning
      • 参数解析器
      • 处理程序增强
    • C. 元数据注解
    • D. 标识符生成
    • E. Axon Server Query Language
由 GitBook 提供支持
在本页
  1. Axon Framework
  2. 事件

事件处理程序

Event Handlers

上一页事件调度器下一页事件处理器

最后更新于2年前

An Event Handler is a method that is capable of handling an EventMessage. As such, the method will react to the occurrences within an application.

In Axon, an object may declare several event handlers by annotating them with @EventHandler. This object is most often referred to as an Event Handling Component, or simply an Event Handler. When drafting an @EventHandler annotated method, the declared parameters of the method define which events it will receive.

Arguably the most important parameter of an event handler is the first parameter which refers to the payload of an EventMessage. If the event handler does not need access to the payload of the message, you can specify the expected payload type on the @EventHandler annotation.

Do not configure the payload type on the annotation if you want the payload to be passed as a parameter. For a complete list of all parameters, we refer to section.

In all circumstances, at most one event handler method is invoked per listener instance. Axon will search for the most specific method to invoke, using the following rules:

  1. On the actual instance level of the class hierarchy (as returned by this.getClass()), all annotated methods are evaluated.

  2. If the framework finds one or more methods for which it can resolve all parameters to a value, the method with the most specific type is chosen and invoked.

  3. If the framework finds no methods on this level of the class hierarchy, the supertype is evaluated the same way.

  4. When it reaches the top level of the hierarchy and no suitable event handler is found, the event is ignored.

Consider the following Event Handling Component sample to explain the event handler invocation rules further:

// assume EventB extends EventA 
// and    EventC extends EventB
// and    a single instance of SubListener is registered

public class TopListener {

    @EventHandler
    public void handle(EventA event) {
    }

    @EventHandler
    public void handle(EventC event) {
    }

}

public class SubListener extends TopListener {

    @EventHandler
    public void handle(EventB event) {
    }

}

In the example above, Axon will invoke the handler methods of SubListener for all instances of EventB and EventC (as it extends EventB). In other words, the handler methods of TopListener will not receive any invocations for EventC at all. Since EventA is not assignable to EventB (it is its superclass), the TopListener's handler method will process them.

Registering event handlers

Event handling Components are defined using an EventProcessingConfigurer, which can be accessed from the global Axon Configurer. EventProcessingConfigurer is used to configure an EventProcessingConfiguration. Typically, an application will have a single EventProcessingConfiguration defined, but larger, more modular applications may define one per module.

To register objects with @EventHandler methods, consider the following samples:

The Configurer exposes several options towards registering an Event Handling Component:

public class AxonConfig {

    // ...
    public void configureEventHandler(Configurer configurer) {
        configurer.registerEventHandler(config -> new MyEventHandlingComponent());

        // or, when the handling component contains several message handler types:
        configurer.registerMessageHandler(config -> new MyEventHandlingComponent());

        // or, directly on the EventProcessingConfigurer:
        configurer.eventProcessing()
                  .registerEventHandler(config -> new MyEventHandlingComponent());
    }
}

In a Spring Boot environment, simply adding the Event Handling Components to the Application Context is sufficient:

@Component
public class MyEventHandlingComponent {

    @EventHandler
    public void on(SomeEvent event) {
        // ...
    }
}
this