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 提供支持
在本页
  • Scheduling a Deadline
  • Handling a Deadline
  • Using Time In Your Application
  1. Axon Framework
  2. Deadlines

Deadline Managers

Deadline Managers

上一页Deadlines下一页Event Schedulers

最后更新于2年前

Deadlines can be scheduled from sagas and aggregates. The DeadlineManager component is responsible for scheduling deadlines and invoking @DeadlineHandlerwhen the deadline is met. The DeadlineManager can be injected as a resource. It has two flavors: SimpleDeadlineManager and QuartzDeadlineManager

Scheduling a Deadline

A deadline can be scheduled by providing a Duration after which it will be triggered (or an Instant at which it will be triggered) and a deadline name.

Scheduled Events or Scheduled Deadlines

Unlike , when a deadline is triggered there will be no storing of the published message. Scheduling/Triggering a deadline does not involve an EventBus (or EventStore), hence the message is not stored.

class DeadlineSchedulingComponent {
    void scheduleMyDeadline() {
        String deadlineId = 
            deadlineManager.schedule(Duration.ofMillis(500), "myDeadline");
        // For example store the `deadlineId`
    }
}

As a result we receive a deadlineId which can be used to cancel the deadline. In most cases, storing this deadlineId as a field within your Aggregate/Saga is the most convenient. Cancelling a deadline could come in handy when a certain event means that the previously scheduled deadline has become obsolete (e.g. there is a deadline for paying the invoice, but the client payed the amount which means that the deadline is obsolete and can be canceled).

class DeadlineCancelingComponent {
    void cancelMyDeadline(String deadlineId) {
        deadlineManager.cancelSchedule("myDeadline", deadlineId);
    }
}

Note that there are more options to cancel a deadline next to the previously mentioned:

  • cancelAll(String deadlineName)

    Cancels every scheduled deadline matching the given deadlineName.

    Note that this thus also cancels deadlines from other aggregate and/or saga instances matching the name.

  • cancelAllWithinScope(String deadlineName)

    Cancels a scheduled deadline matching the given deadlineName, within the Scope the method is invoked in.

    For example, if this operation is performed from within "aggregate instance X",

    the ScopeDescriptor from "aggregate instance X" will be used to cancel.

  • cancelAllWithinScope(String deadlineName, ScopeDescriptor scope)

    Cancels a scheduled deadline matching the given deadlineName and ScopeDescriptor.

    This allows canceling a deadline by name from differing scopes then the one it's executed in.

If you need contextual data about the deadline when the deadline is being handled, you can attach a deadline payload when scheduling a deadline:

class DeadlineSchedulingWithPayloadComponent {
    void scheduleMyDeadlineWithPayload() {
        String deadlineId = deadlineManager.schedule(
            Duration.ofMillis(500), "myDeadline", 
            new MyDeadlinePayload(/* some user specific parameters */)
        );
        // For example store the `deadlineId`
    }
}

Handling a Deadline

We have now seen how to schedule a deadline. When the scheduled time is met, the corresponding @DeadlineHandler is invoked. A @DeadlineHandler is a message handler like any other in Axon - it is possible to inject parameters for which ParameterResolvers exist.

The Scope of a Deadline

When scheduling a deadline, the context from where it was scheduled is taken into account. This means a scheduled deadline will only be triggered in its originating context. Thus any @DeadlineHandler annotated function you wish to be called on a met deadline, must be in the same Aggregate/Saga from which is was scheduled.

Axon calls this context a Scope. If necessary, implementing and providing your own Scope will allow you to schedule deadlines in your custom, 'scoped' components.

A Saga can end its lifecycle when @EndSaga is added on a deadline handler.

A @DeadlineHandler is matched based on the deadline name and the deadline payload.

@DeadlineHandler(deadlineName = "myDeadline")
public void on(MyDeadlinePayload deadlinePayload) {
    // handle the deadline
}

If the deadline's name is not defined in the @DeadlineHandler, matching will proceed based on the deadline payload alone.

@DeadlineHandler
public void on(MyDeadlinePayload deadlinePayload) {
    // handle the deadline
}

If we scheduled a deadline without a specific payload, the @DeadlineHandler does not have to specify the payload.

@DeadlineHandler(deadlineName = "payloadlessDeadline")
public void on() {
    // handle the deadline
}

Using Time In Your Application

public void handle(PublishTime cmd) {
    apply(new TimePublishedEvent(GenericEventMessage.clock.instant()));
}

In cases where applications need to access the clock, they can take advantage of the clock used in the EventMessage, by accessing GenericEventMessage.clock. This clock is set to Clock.systemUTC at runtime, and manipulated to simulate time during .

Note that the current timestamp is automatically added to the EventMessage. If handlers only need to rely on the timestamp the event was published, they can access that timestamp directly, as described in .

Event Scheduling
testing
Handling Events