时刻 — 时间事件传递 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类型,以便在经过特定时间后收到通知。 -
类型为
org.springframework.modulith.Moments的 Bean 在ApplicationContext中可用,其中包含触发这些事件的逻辑。 -
如果将
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 暴露了以下应用程序属性以进行高级定制:
| 属性 | 默认值 | 描述 |
|---|---|---|
|
false |
如果设置为 |
|
小时 |
触发事件的最小粒度。替代值 |
|
|
确定周边界时要使用的 |
|
|
季度开始的月份。 |
|
|
|