時刻 — 時間事件的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 暴露以下應用程式屬性以進行高階自定義

表1. 可用的應用程式屬性
財產 預設值 描述

spring.modulith.moments.enable-time-machine

如果設定為 true,則 Moments 例項將是一個 TimeMachine,它暴露用於向前移動時間的 API。對於期望由時間流逝事件觸發功能的整合測試非常有用。

spring.modulith.moments.granularity

小時

要觸發的事件的最小粒度。替代值 days 以避免每小時事件。

spring.modulith.moments.locale

Locale.getDefault()

用於確定周邊界的 Locale

spring.modulith.moments.quarter-start-month

Months.JANUARY

季度開始的月份。

spring.modulith.moments.zone-id

ZoneOffset#UTC

用於確定附加到已釋出事件的時間的 ZoneId

© . This site is unofficial and not affiliated with VMware.