Moments — a Passage of Time Events API
spring-modulith-moments
是一个受到 Matthias Verraes blog post 大量启发的经过时间事件实现。它是一种基于时间的事件方式,可触发与特定已过去时间段相关的操作。
spring-modulith-moments
is a Passage of Time Events implementation heavily inspired by Matthias Verraes blog post.
It’s an event-based approach to time to trigger actions that are tied to a particular period of time having passed.
要使用摘要,请在您的项目中包含以下依赖项:
To use the abstraction, include the following dependency in your project:
-
Maven
-
Gradle
<dependency>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-moments</artifactId>
</dependency>
dependencies {
implementation 'org.springframework.modulith:spring-modulith-moments'
}
添加到项目类路径的依赖项会在您的应用程序中导致以下情况:
The dependency added to the project’s classpath causes the following things in your application:
-
如果已经过了一段时间,应用程序代码可以在 Spring 事件侦听器中引用
HourHasPassed
,DayHasPassed
,WeekHasPassed
,MonthHasPassed
,QuarterHasPassed
,YearHasPassed
类型以获取通知。 -
Application code can refer to
HourHasPassed
,DayHasPassed
,WeekHasPassed
,MonthHasPassed
,QuarterHasPassed
,YearHasPassed
types in Spring event listeners to get notified if a certain amount of time has passed. -
ApplicationContext
中提供了类型为org.springframework.modulith.Moments
的 Bean,该 Bean 包含触发这些事件的逻辑。 -
A bean of type
org.springframework.modulith.Moments
is available in theApplicationContext
that contains the logic to trigger these events. -
如果将
spring.modulith.moments.enable-time-machine
设置为true
,该实例将成为org.springframework.modulith.TimeMachine
,该实例允许“转移”时间,并由此触发所有的中间事件,这对集成测试由事件触发的功能很有用。 -
If
spring.modulith.moments.enable-time-machine
is set totrue
, that instance will be aorg.springframework.modulith.TimeMachine
which allows to "shift" time and by that triggers all intermediate events, which is useful to integration test functionality that is triggered by the events.
默认情况下,Moments 使用 Clock.systemUTC()
实例。要自定义此设置,请声明类型为 Clock
的 bean。
By default, Moments uses a Clock.systemUTC()
instance. To customize this, declare a bean of type Clock
.
-
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 公开以下应用程序属性以进行高级自定义:
Moments exposes the following application properties for advanced customization:
Property | Default value | Description |
---|---|---|
|
false |
If set to |
|
hours |
The minimum granularity of events to be fired. Alternative value |
|
|
The |
|
|
The month at which quarters start. |
|
|
The |