Spring Boot

Scheduler @Scheduled Annotation Spring Boot

&NewLine;<h2 class&equals;"wp-block-heading">Overview<&sol;h2>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">In this article&comma; we will explore Spring Scheduler how we could use it by &commat;Scheduled annotation in the Spring Boot application&period; We can use it to configure and schedule tasks&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<div class&equals;"wp-block-image is-style-rounded"><figure class&equals;"aligncenter size-full is-resized"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2022&sol;02&sol;Spring-Boot-Scheduled&period;png" class&equals;"wp-image-4620" width&equals;"349" height&equals;"282"&sol;><&sol;figure><&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<h2 class&equals;"wp-block-heading">&commat;Scheduled Annotation Spring Boot<&sol;h2>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">This annotation can be used in a Spring Boot application to schedule any task&period; But there are some simple rules that we need to follow to annotate a method with &commat;Scheduled are&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li>Scheduler method should be void return type if the method has return then returned value will be ignored&period;<&sol;li><li>There should not be any parameters in the scheduler method<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">Next&comma; let&&num;8217&semi;s see how we could use the &commat;Scheduled Annotation in the Spring Boot application&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h2 class&equals;"wp-block-heading">Enable Scheduling in Spring Boot application<&sol;h2>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">The &commat;EnableScheduling annotation enables support for scheduling tasks and the &commat;Scheduled annotation in Spring Boot as below in Java-based configuration&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-code"><code>&commat;Configuration&NewLine;&commat;EnableScheduling&NewLine;public class SchedulerConfig &lbrace;&NewLine; &period;&period;&period;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">Let&&num;8217&semi;s move to use &commat;Scheduled Annotation in the Spring Boot application&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h2 class&equals;"wp-block-heading">Using &commat;Scheduled Annotation in Spring Boot<&sol;h2>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">We can use this annotation &commat;Scheduled in Spring Boot with several defined attributes based on the requirements&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading">Schedule a Task at Fixed Delay<&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">Let&&num;8217&semi;s start by configuring a task to run after a fixed delay&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-code"><code>&commat;Scheduled&lpar;fixedDelay &equals; 1000&rpar;&NewLine;public void scheduleFixedDelayTask&lpar;&rpar; &lbrace;&NewLine;&NewLine; &sol;&sol;Scheduled work here&NewLine;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">In the above code&comma; the <strong><em>fixedDelay<&sol;em><&sol;strong> attribute has a duration &lpar;1000 milliseconds&rpar;&period; This duration is fixed as the end of the last execution and the start of the next execution&period; In this case&comma; the task always waits until the previous one is finished&period; This option is better when you want that the previous execution is completed before running again&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">Let&&num;8217&semi;s explore another way of configuring a task to run with a fixed rate&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading">Schedule a Task at a Fixed Rate<&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">Let&&num;8217&semi;s now execute a task at a fixed interval of time&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-code"><code>&commat;Scheduled&lpar;fixedRate &equals; 1000&rpar;&NewLine;public void scheduleFixedRateTask&lpar;&rpar; &lbrace;&NewLine;&NewLine; &sol;&sol;Scheduled work here&NewLine;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">In this case&comma; the scheduled task will start running again after crossing the fixed-rate value of duration &lpar;1000 milliseconds&rpar;&period; This option is good for the execution of the task independently&period; But <strong>note here&comma; scheduled tasks don&&num;8217&semi;t run in parallel by default&period;<&sol;strong> If you want to make parallel then you have to add the &commat;Async annotation as below&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-code"><code>&commat;EnableAsync&NewLine;public class ScheduledFixedRateExample &lbrace;&NewLine;&NewLine; &commat;Async&NewLine; &commat;Scheduled&lpar;fixedRate &equals; 1000&rpar;&NewLine; public void scheduleFixedRateTaskAsync&lpar;&rpar; throws InterruptedException &lbrace;&NewLine; &NewLine; &sol;&sol;Scheduled work here&NewLine;&NewLine; &rcub;&NewLine;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">Now this asynchronous task will be invoked each second&comma; even if the previous task isn&&num;8217&semi;t done&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading">The fixedDelay vs fixedRate<&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">There are the following key differences between the <strong><em>fixedDelay<&sol;em><&sol;strong> and <strong><em>fixedRate<&sol;em><&sol;strong> attributes of &commat;Scheduled annotation in Spring Boot&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"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&period;<&sol;li><li>The fixedRate property runs the scheduled task at every n millisecond&period; It doesn&&num;8217&semi;t check for any previous executions of the task&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading">Schedule a Task With Initial Delay<&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">Next&comma; let&&num;8217&semi;s schedule a task with a delay&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-code"><code>&commat;Scheduled&lpar;fixedDelay &equals; 1000&comma; initialDelay &equals; 1000&rpar;&NewLine;public void scheduleFixedRateWithInitialDelayTask&lpar;&rpar; &lbrace;&NewLine; &NewLine; &sol;&sol;Scheduled work here&NewLine;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">Note how we&&num;8217&semi;re using both <strong><em>fixedDelay<&sol;em><&sol;strong> as well as <strong><em>initialDelay<&sol;em><&sol;strong> in this example&period; The task will be executed the first time after the <strong><em>initialDelay<&sol;em><&sol;strong> value&comma; and it will continue to be executed according to the <strong><em>fixedDelay<&sol;em><&sol;strong>&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h2 class&equals;"wp-block-heading">Schedule a Task Using Cron Expressions<&sol;h2>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">Sometimes we want to schedule a task for a particular time of every day or every month etc&period; In this case&comma; delays and rates are not enough&period; Spring provide support to use cron expression to control the schedule of our tasks&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-code"><code>&commat;Scheduled&lpar;cron &equals; "0 45 23 &ast; &ast; &ast;"&rpar;&NewLine;public void scheduleTaskUsingCronExpression&lpar;&rpar; &lbrace;&NewLine; &NewLine; &sol;&sol;Scheduled work here&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">In this example&comma; we are scheduling a task to be executed at <strong>11&colon;45 PM every day<&sol;strong>&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h2 class&equals;"wp-block-heading">Conclusion<&sol;h2>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">In this article&comma; we explored Spring scheduler using the &commat;Scheduled annotation in the Spring Boot application&period; It is a simple way to configure and schedule a task that you want to run as per your need automatically&period;<&sol;p>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;configure-multiple-databases-spring-jpa-spring-boot&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '4619'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design PatternsĀ using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Configure Multiple Databases Spring JPA Spring Boot

Overview In this article, we will explore a simple Spring Boot application to implement a…

4 years ago