<h2 class="wp-block-heading">Overview</h2>



<p class="wp-block-paragraph">In this article, we will explore Spring Scheduler how we could use it by @Scheduled annotation in the Spring Boot application. We can use it to configure and schedule tasks.</p>



<div class="wp-block-image is-style-rounded"><figure class="aligncenter size-full is-resized"><img src="https://dineshonjava.com/wp-content/uploads/2022/02/Spring-Boot-Scheduled.png" class="wp-image-4620" width="349" height="282"/></figure></div>



<h2 class="wp-block-heading">@Scheduled Annotation Spring Boot</h2>



<p class="wp-block-paragraph">This annotation can be used in a Spring Boot application to schedule any task. But there are some simple rules that we need to follow to annotate a method with @Scheduled are:</p>



<ul class="wp-block-list"><li>Scheduler method should be void return type if the method has return then returned value will be ignored.</li><li>There should not be any parameters in the scheduler method</li></ul>



<p class="wp-block-paragraph">Next, let&#8217;s see how we could use the @Scheduled Annotation in the Spring Boot application.</p>



<h2 class="wp-block-heading">Enable Scheduling in Spring Boot application</h2>



<p class="wp-block-paragraph">The @EnableScheduling annotation enables support for scheduling tasks and the @Scheduled annotation in Spring Boot as below in Java-based configuration:</p>



<pre class="wp-block-code"><code>@Configuration
@EnableScheduling
public class SchedulerConfig {
 ...
}</code></pre>



<p class="wp-block-paragraph">Let&#8217;s move to use @Scheduled Annotation in the Spring Boot application.</p>



<h2 class="wp-block-heading">Using @Scheduled Annotation in Spring Boot</h2>



<p class="wp-block-paragraph">We can use this annotation @Scheduled in Spring Boot with several defined attributes based on the requirements.</p>



<h3 class="wp-block-heading">Schedule a Task at Fixed Delay</h3>



<p class="wp-block-paragraph">Let&#8217;s start by configuring a task to run after a fixed delay:</p>



<pre class="wp-block-code"><code>@Scheduled(fixedDelay = 1000)
public void scheduleFixedDelayTask() {

 //Scheduled work here

}</code></pre>



<p class="wp-block-paragraph">In the above code, the <strong><em>fixedDelay</em></strong> attribute has a duration (1000 milliseconds). This duration is fixed as the end of the last execution and the start of the next execution. In this case, the task always waits until the previous one is finished. This option is better when you want that the previous execution is completed before running again.</p>



<p class="wp-block-paragraph">Let&#8217;s explore another way of configuring a task to run with a fixed rate:</p>



<h3 class="wp-block-heading">Schedule a Task at a Fixed Rate</h3>



<p class="wp-block-paragraph">Let&#8217;s now execute a task at a fixed interval of time:</p>



<pre class="wp-block-code"><code>@Scheduled(fixedRate = 1000)
public void scheduleFixedRateTask() {

 //Scheduled work here

}</code></pre>



<p class="wp-block-paragraph">In this case, the scheduled task will start running again after crossing the fixed-rate value of duration (1000 milliseconds). This option is good for the execution of the task independently. But <strong>note here, scheduled tasks don&#8217;t run in parallel by default.</strong> If you want to make parallel then you have to add the @Async annotation as below:</p>



<pre class="wp-block-code"><code>@EnableAsync
public class ScheduledFixedRateExample {

 @Async
 @Scheduled(fixedRate = 1000)
 public void scheduleFixedRateTaskAsync() throws InterruptedException {
 
 //Scheduled work here

 }

}</code></pre>



<p class="wp-block-paragraph">Now this asynchronous task will be invoked each second, even if the previous task isn&#8217;t done.</p>



<h3 class="wp-block-heading">The fixedDelay vs fixedRate</h3>



<p class="wp-block-paragraph">There are the following key differences between the <strong><em>fixedDelay</em></strong> and <strong><em>fixedRate</em></strong> attributes of @Scheduled annotation in Spring Boot.</p>



<ul class="wp-block-list"><li>The fixedDelay property makes sure that there is a delay of n millisecond between the finish time of an execution of a task and the start time of the next execution of the task.</li><li>The fixedRate property runs the scheduled task at every n millisecond. It doesn&#8217;t check for any previous executions of the task.</li></ul>



<h3 class="wp-block-heading">Schedule a Task With Initial Delay</h3>



<p class="wp-block-paragraph">Next, let&#8217;s schedule a task with a delay:</p>



<pre class="wp-block-code"><code>@Scheduled(fixedDelay = 1000, initialDelay = 1000)
public void scheduleFixedRateWithInitialDelayTask() {
 
 //Scheduled work here

}</code></pre>



<p class="wp-block-paragraph">Note how we&#8217;re using both <strong><em>fixedDelay</em></strong> as well as <strong><em>initialDelay</em></strong> in this example. The task will be executed the first time after the <strong><em>initialDelay</em></strong> value, and it will continue to be executed according to the <strong><em>fixedDelay</em></strong>.</p>



<h2 class="wp-block-heading">Schedule a Task Using Cron Expressions</h2>



<p class="wp-block-paragraph">Sometimes we want to schedule a task for a particular time of every day or every month etc. In this case, delays and rates are not enough. Spring provide support to use cron expression to control the schedule of our tasks:</p>



<pre class="wp-block-code"><code>@Scheduled(cron = "0 45 23 * * *")
public void scheduleTaskUsingCronExpression() {
 
 //Scheduled work here
}</code></pre>



<p class="wp-block-paragraph">In this example, we are scheduling a task to be executed at <strong>11:45 PM every day</strong>.</p>



<h2 class="wp-block-heading">Conclusion</h2>



<p class="wp-block-paragraph">In this article, we explored Spring scheduler using the @Scheduled annotation in the Spring Boot application. It is a simple way to configure and schedule a task that you want to run as per your need automatically.</p>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/configure-multiple-databases-spring-jpa-spring-boot/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '4619'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore a simple Spring Boot application to implement a…