5. Moments — 时间流逝事件 API
spring-modulith-moments是一个 Passage of Time Events 实现,深受 Matthias Verraes 博客文章的启发。
这是一种基于事件的时间方法,用于触发与经过的特定时间段相关的作。
要使用抽象,请在项目中包括以下依赖项:
<dependency>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-moments</artifactId>
</dependency>
添加到项目 Classpath 的依赖项会导致应用程序中出现以下情况:
-
应用程序代码可以引用
HourHasPassed,DayHasPassed,WeekHasPassed,MonthHasPassed,QuarterHasPassed,YearHasPassed键入 Spring 事件侦听器,以便在经过一定时间时收到通知。 -
类型
org.springframework.modulith.Moments在ApplicationContext,其中包含触发这些事件的逻辑。 -
如果
spring.modulith.moments.enable-time-machine设置为true,则该实例将为org.springframework.modulith.TimeMachine它允许 “shift” 时间并触发所有中间事件,这对于由事件触发的集成测试功能很有用。
默认情况下,Moments 使用Clock.systemUTC()实例。要自定义此 Bean,请声明一个Clock.
@Configuration
class MyConfiguration {
@Bean
Clock myCustomClock() {
// Create a custom Clock here
}
}
Moments 公开了以下应用程序属性以进行高级自定义:
| 财产 | 默认值 | 描述 |
|---|---|---|
|
假 |
如果设置为 |
|
小时 |
要触发的事件的最小粒度。替代值 |
|
|
这 |
|
|
季度开始的月份。 |
|
|
这 |