How to work cron job scheduler in spring boot
Posted By : Madhubala Choudhary | 06-Jan-2022
Scheduling is the process of executing a piece of logic at a specific time. Spring allows us to run scheduled jobs by using some simple annotations in the Spring container.
Here is work on annotation for enable scheduler :
1.@EnableScheduling annotation
2.@Scheduled annotations
@EnableScheduling : It is a Spring Context module annotation that is used to enable the scheduler at the class level.It internally imports the SchedulingConfiguration the @Import(SchedulingConfiguration.class) instruction.Here is an example
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class SpringBootWebApplication
{
public static void main(String[] args)
{
SpringApplication.run(SpringBootWebApplication.class, args);
}
}
@Scheduled annotations : It is a method-level annotation.It takes one attribute from cron, fixedDelay, or fixedRate for specifying the schedule of execution in different formats.
Two conditions necessary for scheduler :
1.The method should not have a return type and so return void.
2.The method should not accept any input parameters.
Here is an example of every 2 sec to print the current date
@Scheduled(cron = "*/2 * * * * *")
public void run() {
log.info("Current time is :: " + Calendar.getInstance().getTime());
}
Output is :
2022-01-04 23:00:54 - com.oodles.Scheduler - Current time is :: Tue Jan 04 17:30:54 UTC 2022
2022-01-04 23:00:56 - com.oodles.Scheduler - Current time is :: Tue Jan 04 17:30:56 UTC 2022
2022-01-04 23:00:58 - com.oodles.Scheduler - Current time is :: Tue Jan 04 17:30:58 UTC 2022
2022-01-04 23:01:00 - com.oodles.Scheduler - Current time is :: Tue Jan 04 17:31:00 UTC 2022
2022-01-04 23:01:02 - com.oodles.Scheduler - Current time is :: Tue Jan 04 17:31:02 UTC 2022
2022-01-04 23:01:04 - com.oodles.Scheduler - Current time is :: Tue Jan 04 17:31:04 UTC 2022
Schedule task at fixed delay :
Fixed delay specifies exact time gap between last method execution completion time and next method start time. Here is an example
@Scheduled(fixedDelay = 4000)
public void run() {
log.info("Current time is :: " + Calendar.getInstance().getTime());
}
Output is :
2022-01-04 23:26:22 - com.oodles.Scheduler - Current time is :: Tue Jan 04 17:56:22 UTC 2022
2022-01-04 23:26:26 - com.oodles.Scheduler - Current time is :: Tue Jan 04 17:56:26 UTC 2022
2022-01-04 23:26:30 - com.oodles.Scheduler - Current time is :: Tue Jan 04 17:56:30 UTC 2022
2022-01-04 23:26:34 - com.oodles.Scheduler - Current time is :: Tue Jan 04 17:56:34 UTC 2022
Schedule task at fixed rate :
The execution time of the method is not taken into consideration when deciding when to start the next job.Here is an example
@Scheduled(fixedRate = 3000)
public void run() {
log.info("Current time is :: " + Calendar.getInstance().getTime());
}
Output is :
2022-01-04 23:23:34 - com.oodles.Scheduler - Current time is :: Tue Jan 04 17:53:34 UTC 2022
2022-01-04 23:23:37 - com.oodles.Scheduler - Current time is :: Tue Jan 04 17:53:37 UTC 2022
2022-01-04 23:23:40 - com.oodles.Scheduler - Current time is :: Tue Jan 04 17:53:40 UTC 2022
2022-01-04 23:23:43 - com.oodles.Scheduler - Current time is :: Tue Jan 04 17:53:43 UTC 2022
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Madhubala Choudhary
Madhubala is an experienced Backend Developer with a wealth of industry experience. She possesses extensive hands-on expertise in Java, Spring Boot, and PostgresSQL technologies. Additionally, She have expertise in API implementations, allowing her to seamlessly integrate different systems and services. She has made significant contributions to various projects, including Pandojo, Tutorx, Musical School, and Croniz Mobile App. Her expertise and dedication have played a crucial role in the success of these projects.