Scheduling In Spring Boot For Beginners
Posted By : Gourav Kumar | 04-Jun-2020
In this blog, we will learn how can we perform scheduling in spring boot. For beginners, scheduling is a process where we schedule one or more tasks for future execution.
By default scheduling, related annotations present into spring-boot-starter-web maven dependency.
Enable Scheduling Support
To enable scheduling in spring boot we have to specify @EnableScheduling annotation in configuration class.
@Configuration
@EnableScheduling
public class SpringScheduleConfig{
......
}
In XML based spring configuration we need to use the given way to enable scheduling in the spring application.
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd">
<task:annotation-driven>
<!-- bean definitions here -->
</beans>
Now to perform scheduling we need to use an annotation @Scheduled. it is a method level annotation so it is applied to those methods where we want to specify the scheduled task.
Scheduling Tasks At a Fixed Rate
@Scheduled(fixedRate = 1000)
public void scheduledTask()
{
System.out.println("Current Time :: "+LocalTime.now());
}
In @Scheduled annotation, fixedRate attribute specified the fixed interval of time for the execution of the task.
Scheduling Tasks At a Fixed Delay
@Scheduled(fixedDelay=1000)
public void scheduledTask()
{
System.out.println("Current Time:: " + LocalTime.now());
}
In @Scheduled annotation, fixedDelay attribute use to specify the fixed delay between end of the previous task execution and start of the next task execution.
Scheduling Tasks At The Initial Delay
@Scheduled(fixedDelay=1000, initialDelay=1000)
public void scheduledTask()
{
System.out.println("Current Time:: " + LocalTime.now());
}
In this example, we are using both fixedDelay and initialDelay so first the task will be executed after the initial delay and then task execution use fixedDelay.
Scheduling Tasks With Cron Expression
@Scheduled(cron="0 30 15 * * ?")
public void scheduledTask()
{
System.out.println("Current Time:: " + LocalTime.now());
}
Before using cron expression in spring scheduling, we should understand this expression.
In our example, the task is scheduled for 3:30 pm every day.
Conclusion
In this blog, we learned spring task scheduling to schedule the task execution. This technique is extensively used in SaaS development services. I hope this blog will help you in the future to implement any scheduling functionality in your application.
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
Gourav Kumar
Gourav is a bright Web App Developer and has good knowledge of Core java, Spring and Hibernate and his hobbies listen and sing songs.