SpringBoot에서 스케쥴링 사용하기 Using Scheduling in SpringBoot
There may be tasks that need to run at specific points in time, at specific time intervals, or at scheduled times.
These tasks are called “scheduled tasks” or “batch jobs,” and Spring supports two main approaches.
This time, I’m going to create a scheduled task using Spring’s scheduling annotations.
What is Scheduling?
Scheduling is a process that can execute tasks at specific time intervals without human intervention.
For example, if there’s a task to generate a report every day at 9:30 AM, we can apply the scheduling task we’re discussing to appropriately meet this requirement.
Using @EnableScheduling in SpringBoot
- Create a Spring project
- Use the scheduling annotation built into SpringBoot - no need to add separate dependencies
- Apply the
@EnableSchedulingannotation to the application startup class that runs SpringBoot- This tells SpringBoot that scheduling events will be used
- Define classes and add the
@Componentannotation so Spring can recognize them - Execute and monitor tasks
SpringBoot’s built-in scheduler has three main features:
- FixedRate()
- FixedDelay()
- Cron()
I’m going to cover these three examples.
I’ll create a new scheduler package and create scheduler classes inside it.
Below is my project structure.
.
├── HELP.md
├── README.md
├── build.gradle
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── example
│ │ └── springbootsimplescheduler
│ │ ├── SpringBootSimpleSchedulerApplication.java
│ │ └── scheduler
│ │ ├── CronScheduler.java
│ │ ├── FixedDelayScheduler.java
│ │ └── FixedRateScheduler.java
│ └── resources
│ └── application.properties
└── test
└── java
└── com
└── example
└── springbootsimplescheduler
└── SpringBootSimpleSchedulerApplicationTests.java
SpringBoot Scheduler - FixedDelay
@Scheduled(initialDelay = 5000, fixedDelay = 9000) // (a)
@Scheduled(initialDelayString = "5000", fixedDelayString = "9000") // (b)
public void myMethod(){
System.out.println("FixedDelayScheduler - "+new Date());
}
- initialDelay: Sets the initial delay time instead of executing immediately after the method is registered
- fixedDelay: A fixed value in ms (1000ms = 1s) that runs from when the previous task ends
- DelayString: Allows the corresponding option value to be applied as a String
SpringBoot Scheduler - FixedRate
@Scheduled(fixedRate = 1000)
//@Scheduled(fixedRateString = "1000")
public void myMethod(){
System.out.println("FixedRateScheduler - "+new Date());
}
- fixedRate: Sets a fixed time in ms (1000ms = 1s) from when the previous task started
SpringBoot Scheduler - Cron
@Scheduled(cron = "* * * * * *")
public void myMethod(){
System.out.println("Hello cron Scheduler Three :"+new Date());
}
- cron allows the task to run according to second/minute/hour/day/month/day of week
- Same as Linux cron
* * * * * *from left to right:- second(0 ~ 59)– minute(0 ~ 59)– hour(0 ~ 23)– day of month(0-31)– month(0-12 or JAN-DEC)– day of week(0-7 or MON-SUN)
- Cron expression TIP
- Comma(,) means skip– Asterisk(*) means all values– Slash(/) means interval
Running SpringBoot Scheduler
Once all three tasks above are completed, run the SpringBoot app and check if the Syslog outputs correctly.
If it doesn’t work properly, check the @EnableScheduling annotation again or verify that @Component is properly registered.
댓글남기기