The @EnableScheduling annotation is used to enable the scheduler for your application. This annotation should be added into the main Spring Boot application class file. @SpringBootApplication @EnableScheduling public class DemoApplication { public static void main(String[] args) { SpringApplication.
Spring Core. Spring provides excellent support for both task scheduling and asynchronous method execution based on cron expression using @Scheduled annotation. The @Scheduled annotation can be added to a method along with trigger metadata.
The Spring Expression Language (SpEL for short) is a powerful expression language that supports querying and manipulating an object graph at runtime. The language syntax is similar to Unified EL but offers additional features, most notably method invocation and basic string templating functionality.
The schedule (TimerTask task, Date time) method of Timer class is used to schedule the task for execution at the given time. If the time given is in the past, the task is scheduled at that movement for execution.
Spring v3.2.2 has added String parameters to the original 3 long parameters to handle this. fixedDelayString
, fixedRateString
and initialDelayString
are now available too.
@Scheduled(fixedDelayString = "${my.fixed.delay.prop}")
public void readLog() {
...
}
You can use the @Scheduled
annotation, but together with the cron
parameter only:
@Scheduled(cron = "${yourConfiguration.cronExpression}")
Your 5 seconds interval could be expressed as "*/5 * * * * *"
. However as I understand you cannot provide less than 1 second precision.
I guess the @Scheduled
annotation is out of question. So maybe a solution for you would be to use task-scheduled
XML configuration. Let's consider this example (copied from Spring doc):
<task:scheduled-tasks scheduler="myScheduler">
<task:scheduled ref="someObject" method="readLog"
fixed-rate="#{YourConfigurationBean.stringValue}"/>
</task:scheduled-tasks>
... or if the cast from String to Long didn't work, something like this would:
<task:scheduled-tasks scheduler="myScheduler">
<task:scheduled ref="someObject" method="readLog"
fixed-rate="#{T(java.lang.Long).valueOf(YourConfigurationBean.stringValue)}"/>
</task:scheduled-tasks>
Again, I haven't tried any of these setups, but I hope it might help you a bit.
In Spring Boot 2, we can use Spring Expression Language (SpPL) for @Scheduled
annotation properties:
@Scheduled(fixedRateString = "${fixed-rate.in.milliseconds}")
public void fixedRate() {
// do something here
}
@Scheduled(fixedDelayString = "${fixed-delay.in.milliseconds}")
public void fixedDelay() {
// do something here
}
@Scheduled(cron = "${cron.expression}")
public void cronExpression() {
// do something here
}
The application.properties
file will look like this:
fixed-rate.in.milliseconds=5000
fixed-delay.in.milliseconds=4000
cron.expression=0 15 5 * * FRI
That's it. Here is an article that explains task scheduling in detail.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With