Total Pageviews

2018/11/13

[Spring Framework] @Scheduled Annotation in Spring

Scenario
If I have two batch services:
(1) execute a task every 5 minute, and execute 5 second delay before the first execution
(2) execute a task on 17:20 everyday 

How-To
Add dependency in your pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>


Create a scheduler service
package com.example.demo.service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class SchedulerService {

    private final DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

    /*
     * Schedule a Task with Initial Delay
     */
    @Scheduled(fixedRate = 300000, initialDelay = 5000)
    public void scheduleFixedRateTask() {
        System.out.println("Fixed rate task - " + formatter.format(new Date()));
    }

    /*
     * Schedule a Task using Cron Expressions
     */
    @Scheduled(cron = "0 20 17 * * ?")
    public void scheduleTaskUsingCronExpression() {
        System.out.println("scschedule Task Using Cron Expression - " + formatter.format(new Date()));
    }

}


Create a client application
package com.example.demo;

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

@EnableScheduling
@SpringBootApplication
public class DemoApplication {

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


Execution Log:
2018-06-04 17:11:22.474  INFO 14272 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 0.686 seconds (JVM running for 1.268)
Fixed rate task - 2018-06-04 17:11:27.473
Fixed rate task - 2018-06-04 17:16:27.472
scschedule Task Using Cron Expression - 2018-06-04 17:20:00.002
Fixed rate task - 2018-06-04 17:21:27.473
Fixed rate task - 2018-06-04 17:26:27.472
Fixed rate task - 2018-06-04 17:31:27.472

No comments: