How to schedule jobs using @Scheduled annotation in spring?
In spring, scheduling jobs are very easy using @Scheduled annotation. The @Scheduled annotation can be added to a method
along with trigger metadata. @Scheduled annotation has 3 trigger metadata. They are:
fixedDelay: Incase, if you specify fixed delay as 5 seconds, then the annotated method would be invoked at every
5 seconds with a fixed delay, meaning that the period will be measured from the completion time of each preceding invocation.
fixedRate: Incase, if you specify fixed rate as 5 seconds, then the annotated method would be executed every
5 seconds measured between the successive start times of each invocation.
cron: It supports cron expression.
Examples are given for all above cases. Before annotating any method with @Scheduled, make sure that you are making below
given xml based configurations. Add task:annotation-driven tag and also register your job bean where your annotated methods are presented.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<task:annotation-driven />
<bean id="myAppScheduleJobs" class="com.java2novice.jobs.InventoryPullerJob" />
</beans>
|
Here is the example for the fixed delay, the time should be specified in milli seconds.
package com.java2novice.jobs;
import org.springframework.scheduling.annotation.Scheduled;
public class InventoryPullerJob {
@Scheduled(fixedDelay=5000)
public void updateEmployeeInventory(){
System.out.println("Started fixed delay job");
/**
* add your scheduled job logic here
*/
}
}
|
Here is the sample code for fixedRate.
package com.java2novice.jobs;
import org.springframework.scheduling.annotation.Scheduled;
public class InventoryPullerJob {
@Scheduled(fixedRate=5000)
public void updateEmployeeInventory(){
System.out.println("Started fixed rate job");
/**
* add your scheduled job logic here
*/
}
}
|
Incase if you want to make an initial delay to start the job, you can specify "initialDelay". If you specify this
value, the the very first time job will be started after the given delay. In the below example, the method is scheduled to run at every
hour with initial start delay as 10 minutes.
package com.java2novice.jobs;
import org.springframework.scheduling.annotation.Scheduled;
public class InventoryPullerJob {
@Scheduled(fixedRate=60*60*1000, initialDelay=10*60*1000)
public void updateEmployeeInventory(){
System.out.println("Started fixed rate job");
/**
* add your scheduled job logic here
*/
}
}
|
Here is the sample code for cron. You can use cron expression in the annotation.
package com.java2novice.jobs;
import org.springframework.scheduling.annotation.Scheduled;
public class InventoryPullerJob {
@Scheduled(cron="*/2 * * * * MON-FRI")
public void updateEmployeeInventory(){
System.out.println("Started cron job");
/**
* add your scheduled job logic here
*/
}
}
|
|