You can make a class to run as a schedule job in Spring Boot. By using @EnableSchedule in your project. Below is example code to enable schedule in Spring Boot.
Declare Spring Boot application to support schedule
By declare @EnableSchedule at the main class (see at line 10). This enables detection of @
Declare schedule configuration at the method you need
Use @Scheduled to configure a schedule method in many way.
More Information
for more information please see Sprint Boot document here.
Declare Spring Boot application to support schedule
By declare @EnableSchedule at the main class (see at line 10). This enables detection of @
Scheduled
annotations on any Spring-managed
bean in the container.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package com.myexample; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; @SpringBootApplication @EnableScheduling public class MyExampleApplication { public static void main(String[] args) { SpringApplication.run(MyExampleApplication.class, args); } } |
Declare schedule configuration at the method you need
Use @Scheduled to configure a schedule method in many way.
- Scheduling by static interval. You must use attribute fixedRate as this example. This value is milliseconds.
1 2 3 4 5 6 7 8 9 10 11
package com.myco.tasks; import org.springframework.scheduling.annotation.Scheduled; public class MyTask { @Scheduled(fixedRate=1000) public void work() { // task execution logic } }
- Scheduling by fixed delay after last successful execution. This value is milliseconds. In this example work() will be delay 1 second after last successful execution.
1 2 3 4 5 6 7 8 9 10 11
package com.myco.tasks; import org.springframework.scheduling.annotation.Scheduled; public class MyTask { @Scheduled(fixedDelay=1000) public void work() { // task execution logic } }
- Scheduling as a UNIX cron job. By using cron attribute.
1 2 3 4 5 6 7 8 9 10 11
package com.myco.tasks; import org.springframework.scheduling.annotation.Scheduled; public class MyTask { @Scheduled(cron="0 * * * * MON-FRI") public void work() { // task execution logic } }
-
Scheduling by number of milliseconds to delay before the first execution of a fixedRate() or fixedDelay() task.
1 2 3 4 5 6 7 8 9 10 11
package com.myco.tasks; import org.springframework.scheduling.annotation.Scheduled; public class MyTask { @Scheduled(fixedRate=1000, initialDelay=5000) public void work() { // task execution logic } }
-
Dynamic setup fixedRate or fixedDelay by using environment variables from application.properties. Spring Boot have fixedRateString, fixedDelayString and initialDelayString for you to set value of @Scheduled from environment variables. Below is example (see at line 7).
1 2 3 4 5 6 7 8 9 10 11
package com.myco.tasks; import org.springframework.scheduling.annotation.Scheduled; public class MyTask { @Scheduled(@Scheduled(fixedDelayString="${my.environment.variable}")) public void work() { // task execution logic } }
More Information
for more information please see Sprint Boot document here.
Comments
Post a Comment