Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Task Execution and Scheduling in Spring Boot
Nilanchal
Nilanchal

Posted on • Originally published atstacktips.com

     

Task Execution and Scheduling in Spring Boot

Spring Scheduler is used for running repetitive tasks or to automate tasks that need to run at specific times or at specific intervals. For example such as sending our email newsletters to your customers, generating daily reports, or updating a database periodically.

In this crash course, we will cover everything you need to know about Spring Scheduler – including the annotations, examples, and other things to consider.

Enable Scheduling

To enable Spring's scheduled task execution capability, just annotate any of your@Configuration classes with@EnableScheduling annotation.

Example:

@Configuration@EnableSchedulingpublicclassSchedulerConfig{//TODO Here it goes your configuration}
Enter fullscreen modeExit fullscreen mode

Cron expression

A Cron expression consists of six sequential fields and is declared assecond, minute, hour, day of the month, month, day(s) of the week.

┌───────────── second (0-59)│ ┌───────────── minute (0 - 59)│ │ ┌───────────── hour (0 - 23)│ │ │ ┌───────────── day of the month (1 - 31)│ │ │ │ ┌───────────── month (1 - 12) (or JAN-DEC)│ │ │ │ │ ┌───────────── day of the week (0 - 7)│ │ │ │ │ │              (0 or 7 is Sunday or MON-SUN)│ │ │ │ │ │* * * * * *
Enter fullscreen modeExit fullscreen mode

Ref:https://crontab.guru/

And is declared as follows:

@Slf4j@ComponentpublicclassMyScheduler{privatestaticfinalSimpleDateFormatdateFormat=newSimpleDateFormat("HH:mm:ss");@Scheduled(cron="*/5 * * * * *")publicvoidcurrentTime(){log.info("Current Time = {}",dateFormat.format(newDate()));}}
Enter fullscreen modeExit fullscreen mode

We can also set the timezone as:https://docs.oracle.com/cd/B13866_04/webconf.904/b10877/timezone.htm

@Scheduled(cron="* * * * * *",zone="Europe/London")
Enter fullscreen modeExit fullscreen mode

Fixed delay

ThefixedDelay property makes sure that there is a delay of n milliseconds between the finish time of an execution of a task and the start time of the next execution of the task.

@ComponentpublicclassMyScheduler{@Scheduled(fixedDelay=5000)publicvoiddoSomething(){//This will execute periodically, after the one before finishes}}
Enter fullscreen modeExit fullscreen mode

By default, milliseconds will be used as the time unit for fixed delay, fixed rate, and initial delay values. If you would like to use a different time unit such as seconds or minutes, you can configure this via the timeUnit attribute in @Scheduled.

For example, the previous example can also be written as follows.

@ComponentpublicclassMyScheduler{@Scheduled(fixedDelay=5,timeUnit=TimeUnit.SECONDS)publicvoiddoSomething(){//This will execute periodically, after the one before finishes}}
Enter fullscreen modeExit fullscreen mode

Fixed Rate

ThefixedRate is used when we want to execute a task periodically at every n millisecond without checking for any previous executions of the task.

@ComponentpublicclassMyScheduler{@Scheduled(fixedRate=5000)publicvoiddoSomething(){//This will execute periodically}}
Enter fullscreen modeExit fullscreen mode

For bothfixedDelay andfixedRate tasks, you can specify anintialDelay by indicating the amount of time to wait before the first execution of the method.

@ComponentpublicclassMyScheduler{@Scheduled(initailDelay=1000,fixedRate=5000)publicvoiddoSomething(){//This will execute periodically}}
Enter fullscreen modeExit fullscreen mode

Schedule Tasks and Concurrency

Spring Boot uses aThreadPoolTaskScheduler to execute scheduled tasks. By default, this thread pool has a single thread. This means that only one scheduled task can be executed at a time.

If you need to execute multiple tasks concurrently, then you need to configure theThreadPoolTaskScheduler to have the required thread pool size.

@BeanpublicThreadPoolTaskSchedulerthreadPoolTaskScheduler(){ThreadPoolTaskSchedulerthreadPoolTaskScheduler=newThreadPoolTaskScheduler();threadPoolTaskScheduler.setPoolSize(5);returnthreadPoolTaskScheduler;}
Enter fullscreen modeExit fullscreen mode

Also, we need to use the@Async annotation to mark the scheduled task as asynchronous. This will make Spring Boot execute the tasks in separate threads.

@Async@Scheduled(fixedDelay=1000)publicvoidstartUsingFixedDelay(){log.info("startUsingFixedDelay:: Task started at {}",DATE_FORMAT.format(newDate()));}
Enter fullscreen modeExit fullscreen mode

Aggregate Scheduled annotations

The@Schedules annotation is a container annotation that aggregates several Scheduled annotations.

@Schedules({@Scheduled(fixedRate=10000),@Scheduled(cron="0 * * * * MON-FRI")})publicvoiddoSomething(){//This will execute periodically}
Enter fullscreen modeExit fullscreen mode

Before Java 8, a wrapper/container annotation was required to use multiple instances of the same annotation. But Java 8 supports repeatable annotations so wrapper annotation is no longer necessary. Multiple annotations can be used without a wrapper.

@Scheduled(fixedRate=10000)@Scheduled(cron="0 * * * * MON-FRI")publicvoiddoSomethingElse4(){//This will execute periodically}
Enter fullscreen modeExit fullscreen mode

This rule is automatically disabled when the project’s sonar.java.source is lower than 8 as repeating annotations were introduced in Java 8.

Testing the Scheduler using Awaitility

Testing the scheduler can be a little tricky. We can test this by manually waiting forx number of minutes usingThread.sleep() , but for complex scenarios, this will be hard to scale.

This is where we can use a framework likeAwaitility to wait for a certain condition to be met while running your test. In this case, we want to wait for1000 milliseconds before we assess the test results.

Let's first add theawaitility test dependency on yourbuild.gradle file.

testImplementation'org.awaitility:awaitility:3.1.2'
Enter fullscreen modeExit fullscreen mode

Now,

@SpringJUnitConfig(SchedulerConfig.class)classMySchedulerTest{@SpyBeanMySchedulerscheduler;@TestvoidgivenSleepBy1000ms_whenStartTask1(){Awaitility.await().atMost(1000,TimeUnit.MILLISECONDS).untilAsserted(()->{verify(scheduler,atLeast(1)).startUsingFixedDelay();});}}
Enter fullscreen modeExit fullscreen mode

Task Monitoring:

Spring bootactuators provides the/scheduledtasks endpoint to monitor the list of tasks scheduled and their configurations. To enable this we need to add the actuator starter dependencies.

implementation'org.springframework.boot:spring-boot-starter-actuator'
Enter fullscreen modeExit fullscreen mode

Once Actuator dependencies is added, we need to explicitly include thescheduledtasks endpoint by using the following property

management.endpoints.web.exposure.include=scheduledtasks
Enter fullscreen modeExit fullscreen mode

Did you know?

What happens if a scheduled task throws an exception?
If any exception is encountered during the scheduled task execution and if it is not handled gracefully using a try-catch block then the Spring Logging Error Handler will handle the exception and log the error details.

The next instances of that task will continue to execute as per the schedule.

What happens if a scheduled task takes longer than its scheduled interval?
In the case of a fixed rate, if a scheduled task takes longer than its scheduled interval, the Spring Scheduler will start the next execution of the task immediately after the previous one is completed.

This can cause tasks to overlap, which may impact performance.

Limitations of Spring Scheduler

  • No support for dynamic Scheduling: The@Scheduled annotations are typically configured at application startup and do not supportdynamic scheduling without redeploying the application.
  • No support for Job Persistence: It does not offer built-in support for Job persistence, as a result, job recovery in the event of an application restart is not possible.
  • No clustering, load balancing: It does not support clustering and load balancing, The tasks are typically run on a single node.
  • Limited control: It does not allow fine-grained control over managing the tasks. For example, we cannot pause, resume, and unscheduled jobs individually.

Spring Scheduler is sufficient for many simple scheduling tasks, but if you have complex scheduling requirements, job management, and monitoring, then you would need another framework likeQuartz.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software architect. Expertise in Java, Spring, Python, Django, OOP, Microservices & AWS.
  • Location
    localhost
  • Joined

More fromNilanchal

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp