- Spring Boot - Google OAuth2 Sign-In
- Spring Boot - Google Cloud Platform
- Spring Boot - OAuth2 with JWT
- Securing Web Applications
- Spring Boot - Database Handling
- Rest Controller Unit Test
- Spring Boot - Unit Test Cases
- Spring Boot - Twilio
- Spring Boot - Apache Kafka
- Spring Boot - Batch Service
- Spring Boot - Web Socket
- Spring Boot - Hystrix
- Spring Boot - Sending Email
- Spring Boot - Flyway Database
- Tracing Micro Service Logs
- Spring Boot - Creating Docker Image
- Spring Boot - Enabling Swagger2
- Spring Boot - Admin Client
- Spring Boot - Admin Server
- Spring Boot - Actuator
- Spring Cloud Configuration Client
- Spring Cloud Configuration Server
- Zuul Proxy Server and Routing
- Service Registration with Eureka
- Spring Boot - Eureka Server
- Spring Boot - Enabling HTTPS
- Spring Boot - Scheduling
- Spring Boot - Internationalization
- Spring Boot - CORS Support
- Consuming RESTful Web Services
- Spring Boot - Thymeleaf
- Spring Boot - Service Components
- Spring Boot - File Handling
- Spring Boot - Rest Template
- Spring Boot - Tomcat Port Number
- Spring Boot - Servlet Filter
- Spring Boot - Interceptor
- Spring Boot - Exception Handling
- Building RESTful Web Services
- Spring Boot - Logging
- Spring Boot - Application Properties
- Spring Boot - Runners
- Spring Beans & Dependency Injection
- Spring Boot - Code Structure
- Spring Boot - Build Systems
- Spring Boot - Tomcat Deployment
- Spring Boot - Bootstrapping
- Spring Boot - Quick Start
- Spring Boot - Introduction
- Spring Boot - Home
Spring Boot Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Spring Boot - Schedupng
Schedupng is a process of executing the tasks for the specific time period. Spring Boot provides a good support to write a scheduler on the Spring apppcations.
Java Cron Expression
Java Cron expressions are used to configure the instances of CronTrigger, a subclass of org.quartz.Trigger. For more information about Java cron expression you can refer to this pnk −
The @EnableSchedupng annotation is used to enable the scheduler for your apppcation. This annotation should be added into the main Spring Boot apppcation class file.
@SpringBootApppcation @EnableSchedupng pubpc class DemoApppcation { pubpc static void main(String[] args) { SpringApppcation.run(DemoApppcation.class, args); } }
The @Scheduled annotation is used to trigger the scheduler for a specific time period.
@Scheduled(cron = "0 * 9 * * ?") pubpc void cronJobSch() throws Exception { }
The following is a sample code that shows how to execute the task every minute starting at 9:00 AM and ending at 9:59 AM, every day
package com.tutorialspoint.demo.scheduler; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.schedupng.annotation.Scheduled; import org.springframework.stereotype.Component; @Component pubpc class Scheduler { @Scheduled(cron = "0 * 9 * * ?") pubpc void cronJobSch() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); Date now = new Date(); String strDate = sdf.format(now); System.out.println("Java cron job expression:: " + strDate); } }
The following screenshot shows how the apppcation has started at 09:03:23 and for every one minute from that time the cron job scheduler task has executed.
Fixed Rate
Fixed Rate scheduler is used to execute the tasks at the specific time. It does not wait for the completion of previous task. The values should be in milpseconds. The sample code is shown here −
@Scheduled(fixedRate = 1000) pubpc void fixedRateSch() { }
A sample code for executing a task on every second from the apppcation startup is shown here −
package com.tutorialspoint.demo.scheduler; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.schedupng.annotation.Scheduled; import org.springframework.stereotype.Component; @Component pubpc class Scheduler { @Scheduled(fixedRate = 1000) pubpc void fixedRateSch() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); Date now = new Date(); String strDate = sdf.format(now); System.out.println("Fixed Rate scheduler:: " + strDate); } }
Observe the following screenshot that shows the apppcation that has started at 09:12:00 and after that every second fixed rate scheduler task has executed.
Fixed Delay
Fixed Delay scheduler is used to execute the tasks at a specific time. It should wait for the previous task completion. The values should be in milpseconds. A sample code is shown here −
@Scheduled(fixedDelay = 1000, initialDelay = 1000) pubpc void fixedDelaySch() { }
Here, the initialDelay is the time after which the task will be executed the first time after the initial delay value.
An example to execute the task for every second after 3 seconds from the apppcation startup has been completed is shown below −
package com.tutorialspoint.demo.scheduler; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.schedupng.annotation.Scheduled; import org.springframework.stereotype.Component; @Component pubpc class Scheduler { @Scheduled(fixedDelay = 1000, initialDelay = 3000) pubpc void fixedDelaySch() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); Date now = new Date(); String strDate = sdf.format(now); System.out.println("Fixed Delay scheduler:: " + strDate); } }
Observe the following screenshot which shows the apppcation that has started at 09:18:39 and after every 3 seconds, the fixed delay scheduler task has executed on every second.
Advertisements