What is the @Scheduled annotation
ReportPlease briefly explain why you feel this question should be reported .
- fixedRate makes Spring run the task on periodic intervals even if the last invocation may be still running.
- fixedDelay specifically controls the next execution time when the last execution finishes.
- cron is a feature originating from Unix cron utility and has various options based on your requirements.
Example usage can be as below:
@Scheduled(fixedDelay =30000)
public void demoServiceMethod () {… }
@Scheduled(fixedRate=30000)
public void demoServiceMethod () {… }
@Scheduled(cron=”0 0 * * * *”)
public void demoServiceMethod () {… }
To use @Scheduled in your spring application, you must first define below xml namespace and schema location definition in your application-config.xml file.
xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
Above additions are necessary because we will be using annotation based configurations. Now add below definition to enable annotations.
|
Next step is to create a class and a method inside the class like below:
|
Scheduling using cron expression from properties file
${cron.expression} –> cron expression will be defined in a properties (xxx.properties) file
sample class will look like this:
|
And application configuration will look like this:
Leave an answer