Moments is an implementation of the Passage of Time Event idea, originally presented by Mathias Verraes. It enables Spring Boot applications to consume the following events to attach business logic to them:
-
DayHasPassed
- signals the end of a day -
WeekHasPassed
- signals the end of a week -
MonthHasPassed
- signals the end of a month -
QuarterHasPassed
- signals the end of a quarter -
YearHasPassed
- signals the end of a year
This allows event listeners to be registered that react on the publication of such events. They allow to decouple from the actual triggering mechanism (e.g. Spring’s built-in scheduling) and can thus be tested more easily.
Moments will auto-activate in a Spring Boot application if you put it on the classpath of your application.
<dependency>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-moments</artifactId>
<version>…</version>
</dependency>
The Moments module enables scheduling for the application it is applied to.
It registers a bean of type Moments
that takes care of publishing the above mentioned events.
By setting moduliths.moments.enable-time-machine
you can also rather expose a bean of type TimeMachine
(which extends Moments
), which exposes a ….shift(Duration)
method which allows to move what constitutes "now" by the given Duration
.
Moving time forward will cause all events published that would occur during the delta.
Moments works with Java’s standard Clock
abstraction to determine the current time.
By default, Clock.standardUTC()
is used.
To use a different clock, just register a Spring Bean of type Clock
.
Moments will pick it up automatically.
Moments exposes configuration properties to tweak its behavior under the moduliths.moments
namespace.
Property | Default value | Description |
---|---|---|
|
|
Whether to enable Moments in the first place. |
|
|
Set to true to expose |
|
|
At which granularity to publish events. Switch to |
|
|
The |
|
January |
The month in which the first quarter starts. Customize via the month’s name you like to start the quarters at (e.g. |
|
|
The time zone to determine the dates and times attached to the events published. Use standardized region time zone descriptors (e.g. |