<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ichwan Sholihin</title>
    <description>The latest articles on DEV Community by Ichwan Sholihin (@ichwansh).</description>
    <link>https://dev.to/ichwansh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F357497%2F9396d747-3b55-43dd-8c7c-ba8e31612000.png</url>
      <title>DEV Community: Ichwan Sholihin</title>
      <link>https://dev.to/ichwansh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ichwansh"/>
    <language>en</language>
    <item>
      <title>Introduction to Spring Scheduled and monitoring the task with Spring Actuator ️🕛️📈️</title>
      <dc:creator>Ichwan Sholihin</dc:creator>
      <pubDate>Sat, 03 Feb 2024 00:37:30 +0000</pubDate>
      <link>https://dev.to/ichwansh/introduction-to-spring-scheduled-and-monitoring-the-task-with-spring-actuator-5emj</link>
      <guid>https://dev.to/ichwansh/introduction-to-spring-scheduled-and-monitoring-the-task-with-spring-actuator-5emj</guid>
      <description>&lt;p&gt;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 &lt;code&gt;@Scheduled&lt;/code&gt; 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.&lt;/p&gt;

&lt;p&gt;To begin using it, we first need to enable this feature in the configuration class or the main Spring class with an annotation &lt;code&gt;@EnableScheduling&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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);
 }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Next, we can create a method responsible for executing tasks either periodically or at specific times. Simply add the &lt;code&gt;@Scheduled&lt;/code&gt; annotation at the method level.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Is the &lt;code&gt;@Scheduled&lt;/code&gt; 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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;dependencies&amp;gt;
 ...
  &amp;lt;dependency&amp;gt;
   &amp;lt;groupId&amp;gt;org.projectlombok&amp;lt;/groupId&amp;gt;
   &amp;lt;artifactId&amp;gt;lombok&amp;lt;/artifactId&amp;gt;
   &amp;lt;version&amp;gt;1.18.30&amp;lt;/version&amp;gt;
  &amp;lt;/dependency&amp;gt;
 ...
 &amp;lt;/dependencies&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Scheduled(timeUnit = TimeUnit.SECONDS, initialDelay = 3, fixedDelay = 2)
public void cronJob() {
  log.info("run cron job");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Scheduled(cron = "*/2 * * * * *")
public void cronJob() {
  log.info("run cron job");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default, a cron expression has six asterisks, each representing a specific unit of time. In the above expression, the &lt;code&gt;cronJob()&lt;/code&gt; method will be executed every 2 seconds. For a more detailed explanation on how to read a cron expression, please visit the following website &lt;a href="https://crontab.guru/" rel="noopener noreferrer"&gt;Crontab&lt;/a&gt;. Then, if you want to delve into the detailed workings of how scheduling works in Spring, refer to the following documentation &lt;a href="https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/annotation/Scheduled.html?source=post_page-----b61aa38417eb--------------------------------" rel="noopener noreferrer"&gt;Spring Scheduled&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;dependencies&amp;gt;
 ...
  &amp;lt;dependency&amp;gt;
   &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
   &amp;lt;artifactId&amp;gt;spring-boot-starter-actuator&amp;lt;/artifactId&amp;gt;
  &amp;lt;/dependency&amp;gt;
 ...
 &amp;lt;/dependencies&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, in the application.properties file, add some configurations to view which schedulers are working in our Spring application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;management.endpoints.web.exposure.include=scheduledtasks
management.endpoint.scheduledtasks.enabled=true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Spring Actuator actually has many pre-defined endpoints that can be accessed through URLs. For more details, please visit the following documentation &lt;a href="https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/?source=post_page-----b61aa38417eb--------------------------------#actuator" rel="noopener noreferrer"&gt;Actuator&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;By combining task scheduling using the &lt;code&gt;@Scheduled&lt;/code&gt; 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.&lt;/p&gt;

&lt;p&gt;If you want to visualize Spring Actuator and monitor your application’s performance simultaneously, use &lt;a href="https://micrometer.io/" rel="noopener noreferrer"&gt;Micrometer&lt;/a&gt; (maybe I will share about this in another time).&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Visit another post in &lt;a href="https://ichwansholihin.medium.com/" rel="noopener noreferrer"&gt;Medium&lt;/a&gt;&lt;br&gt;
if you excited about this post, please follow and react for more tutorial.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>java</category>
      <category>spring</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
