时刻 — 时间事件的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.