This uses Spring's powerful Trigger
interface to build a simple scheduling system that triggers when any of the points in time from a series of Instant
objects comes due.
Add the dependency (a Spring Boot _starter) to your build
<dependency>
<groupId>com.joshlong</groupId>
<artifactId>schedule-trigger-starter</artifactId>
<version>0.0.1</version>
</dependency>
There's now an engine running alongside your code. If you want dispatches at two different Instant
s in the future, you may either:
- inject the
SchedulingService
and callschedule(Collection\<Instant\> instants)
method - publish an event of type
ScheduleRefreshEvent
containing zero or moreInstant
references
Either one of these will have the effect of clearing whatever outstanding callbacks were planned and installing the new callbacks. That is, the effect is not additive. If you had two callbacks for some point in the future but want to add three more, you'll have to resubmit all five.
Clients can listen for a ScheduleEvent
in the normal ways:
- implement
ApplicationListener\<ScheduleEvent\>
- use
@EventListener
If you use @EventListener
, be sure to define a parameter of type ScheduleEvent
for your handler method. ScheduleEvent#getSource()
returns the Instant
that you specified corresponding to the time (roughly) of this callback.
This code is provided as-is. As far as I know, it works! But don't rely on something like this for anything strictly realtime