DEV Community

Cover image for Introduction to Spring Scheduled and monitoring the task with Spring Actuator ️🕛️📈️
Ichwan Sholihin
Ichwan Sholihin

Posted on

Introduction to Spring Scheduled and monitoring the task with Spring Actuator ️🕛️📈️

In software development, task scheduling is a critical component for executing specific tasks periodically or according to the application’s needs. The Spring Framework provides a robust scheduling mechanism using the @Scheduled annotation. Additionally, for monitoring and managing application performance, Spring Actuator can be employed as an effective tool. This article will discuss how to combine task scheduling with Spring Actuator to create a reliable scheduling system and monitor task executions.

To begin using it, we first need to enable this feature in the configuration class or the main Spring class with an annotation @EnableScheduling.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@SpringBootApplication
public class SpringAsyncApplication {

 public static void main(String[] args) {
  SpringApplication.run(SpringAsyncApplication.class, args);
 }

}
Enter fullscreen mode Exit fullscreen mode

This allows the scheduling feature to run automatically when Spring is in operation. The scheduling feature itself is provided by the Spring Framework, so there is no need to manually register dependencies.

Next, we can create a method responsible for executing tasks either periodically or at specific times. Simply add the @Scheduled annotation at the method level.

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class Job {

    @Scheduled
    public void cronJob() {
        log.info("run cron job");
    }
}
Enter fullscreen mode Exit fullscreen mode

Is the @Scheduled annotation sufficient? It should be configured beforehand to specify when the task should be executed by adding arguments to the above-mentioned scheduled annotation. In that method, there is also a log implementation using SLF4j. To use it, first install the Lombok library in the pom.xml file.

<dependencies>
 ...
  <dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <version>1.18.30</version>
  </dependency>
 ...
 </dependencies>
Enter fullscreen mode Exit fullscreen mode

There are numerous implementations for running a task periodically in Spring scheduling. As in the example below, the cronJob() method will output log information every 2 seconds with an initial delay of 3 seconds.

@Scheduled(timeUnit = TimeUnit.SECONDS, initialDelay = 3, fixedDelay = 2)
public void cronJob() {
  log.info("run cron job");
}
Enter fullscreen mode Exit fullscreen mode

Initial delay is the delay time that occurs before the first task is executed after the Spring application is started or after the scheduler is activated. Fixed delay is the time between the completion of one task execution and the start of the next task execution. In addition to using the values of the above arguments, we can also use cron expressions. Cron expression is similar to time scheduling in Linux, aiming to determine the scheduling in as much detail as possible.

@Scheduled(cron = "*/2 * * * * *")
public void cronJob() {
  log.info("run cron job");
}
Enter fullscreen mode Exit fullscreen mode

By default, a cron expression has six asterisks, each representing a specific unit of time. In the above expression, the cronJob() method will be executed every 2 seconds. For a more detailed explanation on how to read a cron expression, please visit the following website Crontab. Then, if you want to delve into the detailed workings of how scheduling works in Spring, refer to the following documentation Spring Scheduled

Now, lets talk about monitoring. Monitoring is a crucial aspect to understand the performance and health of an application. Spring Actuator is part of the Spring framework project that provides a set of ready-to-use endpoints for monitoring Spring Boot applications, including the scheduling we discussed earlier. To enable the Actuator feature in Spring, simply add the following library to the pom.xml file:

<dependencies>
 ...
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
 ...
 </dependencies>
Enter fullscreen mode Exit fullscreen mode

Then, in the application.properties file, add some configurations to view which schedulers are working in our Spring application.

management.endpoints.web.exposure.include=scheduledtasks
management.endpoint.scheduledtasks.enabled=true
Enter fullscreen mode Exit fullscreen mode

Spring Actuator actually has many pre-defined endpoints that can be accessed through URLs. For more details, please visit the following documentation Actuator

Just access the endpoint localhost:8080/actuator and click on the available endpoints, then a JSON containing the running scheduled tasks in Spring will appear.

By combining task scheduling using the @Scheduled annotation from the Spring Framework and leveraging Spring Actuator, we can create an efficient and easily monitorable application. This provides better control over task execution and enables real-time monitoring of the application’s performance.

If you want to visualize Spring Actuator and monitor your application’s performance simultaneously, use Micrometer (maybe I will share about this in another time).

Micrometer.io is an efficient and reliable performance monitoring tool designed for Java-based applications. With easy integration, diverse features, and support for various monitoring systems, Micrometer.io provides a comprehensive solution for the performance monitoring needs of modern applications.

Visit another post in Medium
if you excited about this post, please follow and react for more tutorial.

Top comments (0)