時刻 — 時間事件的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 暴露以下應用程式屬性以進行高階自定義
| 財產 | 預設值 | 描述 |
|---|---|---|
|
假 |
如果設定為 |
|
小時 |
要觸發的事件的最小粒度。替代值 |
|
|
用於確定周邊界的 |
|
|
季度開始的月份。 |
|
|
用於確定附加到已釋出事件的時間的 |