时刻 — 时间事件的API
spring-modulith-moments 是一个时间流逝事件实现,它深受 Matthias Verraes 博客文章 的启发。它是一种基于事件的时间触发方法,用于触发与特定时间段流逝相关的操作。
要使用此抽象,请在项目中包含以下依赖项
-
Maven
-
Gradle
<dependency>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-moments</artifactId>
</dependency>
dependencies {
implementation 'org.springframework.modulith:spring-modulith-moments'
}
添加到项目类路径中的依赖项会在您的应用程序中导致以下情况
-
应用程序代码可以在 Spring 事件监听器中引用
HourHasPassed,DayHasPassed,WeekHasPassed,MonthHasPassed,QuarterHasPassed,YearHasPassed类型,以在特定时间量流逝后获得通知。 -
ApplicationContext中提供了一个类型为org.springframework.modulith.Moments的 bean,其中包含触发这些事件的逻辑。 -
如果
spring.modulith.moments.enable-time-machine设置为true,则该实例将是一个org.springframework.modulith.TimeMachine,它允许“移动”时间并触发所有中间事件,这对于集成测试由事件触发的功能非常有用。
默认情况下,Moments 使用 Clock.systemUTC() 实例。要自定义此项,请声明一个 Clock 类型的 bean。
-
Java
-
Kotlin
@Configuration
class MyConfiguration {
@Bean
Clock myCustomClock() {
// Create a custom Clock here
}
}
@Configuration
class MyConfiguration {
@Bean
fun myCustomClock(): Clock {
// Create a custom Clock here
}
}
Moments 暴露以下应用程序属性以进行高级自定义
| 财产 | 默认值 | 描述 |
|---|---|---|
|
假 |
如果设置为 |
|
小时 |
要触发的事件的最小粒度。替代值 |
|
|
用于确定周边界的 |
|
|
季度开始的月份。 |
|
|
用于确定附加到已发布事件的时间的 |