Spring ์ค์ผ์ค๋ง
https://docs.spring.io/spring-framework/reference/integration/scheduling.html
Task Execution and Scheduling :: Spring Framework
All Spring cron expressions have to conform to the same format, whether you are using them in @Scheduled annotations, task:scheduled-tasks elements, or someplace else. A well-formed cron expression, such as * * * * * *, consists of six space-separated time
docs.spring.io
Spring ์ค์ผ์ค๋ง์ ์ฝ๊ฒ ๋งํด ์๋ฒ๊ฐ ์ผ์ ธ์๋ ๋์, ๋ด๊ฐ ์ ํ ์๊ฐ๋ง๋ค ํน์ ๋ฉ์๋๋ฅผ ์๋ ์คํํ๊ฒ ๋ง๋๋ ๊ธฐ๋ฅ์ด๋ค
Spring์, ์ด ํ๋ก์ ํธ์์ @Scheduled๋ถ์ ๋ฉ์๋๋ฅผ ์ฐพ์์ ์๋ ์คํํด์ค -> @EnableScheduling
@Configuration
@EnableScheduling
public class SchedulingConfig {
}
๊ณต์๋ฌธ์์์๋ "To enable support for @Scheduled … add @EnableScheduling to one of your @Configuration classes."๋ผ๊ณ ๋์์๋ค ์ฆ @Scheduled ๊ธฐ๋ฅ์ ์ฌ์ฉํ๋ ค๋ฉด ์ค์ ํฌ๋ ์ค ์ค ํ๋์ @EnableScheduling์ ์ถ๊ฐํ๋ผ๋ ๋ง์ด๋ค
@Configuration : Spring ์ค์ ํ์ผ ์ญํ ์ ํ๋ ํด๋์ค๋ผ๋ ํ์
@EnableScheduling : Spring์๊ฒ ์ค์ผ์ค๋ง ๊ธฐ๋ฅ ์ผ๋ผ๋ผ๊ณ ์๋ ค์ฃผ๋ ์ด๋ ธํ ์ด์ - ์ค์์น
์ด ๋ฉ์๋๋ฅผ ๋งค์ผ ์๋ฒฝ 3์์ ์๋ ์คํํด์ค -> @Scheduled
@Component
public class AttachmentCleanupScheduler {
@Scheduled(cron = "0 0 3 * * *")
public void deleteOldTempAttachments() {
// ์ค๋๋ ์์ ์ฒจ๋ถํ์ผ ์ญ์
}
}
๊ณต์๋ฌธ์์์๋ "You can add the @Scheduled annotation to a method, along with trigger metadata."๋ผ๊ณ ๋์์๋ค
์ฆ, ๋ฉ์๋์ @Scheduled๋ฅผ ๋ถ์ด๊ณ ์ธ์ ์คํํ ์ง์ ๋ํ ์คํ ์กฐ๊ฑด๋ ๊ฐ์ด ์ ์ ์ ์๋ค๋ ๋ง์ด๋ค ์ฌ๊ธฐ์ ์ค์ํ ๊ฑด ์ปจํธ๋กค๋ฌ์ฒ๋ผ ์ฌ์ฉ์์ ์์ฒญ์ด ๋ค์ด์์ผ ์คํ๋๋ ๊ฒ ์๋๋ผ๋ ๊ฒ.
์๋ฒ ์ผ์ง
→ ์ ํด์ง ์๊ฐ์ด ๋จ
→ Spring์ด ์์์ ๋ฉ์๋ ์คํ
trigger metadata : ์ธ์ ์คํํ ์ง ์๋ ค์ฃผ๋ ์ด๊ธฐ๊ฐ (cron, fixedDelay, fixedRate)
์๋ ๋ฉ์๋๋ 5์ด๋ง๋ค ์คํํด์ค -> fixedDelay
@Scheduled(fixedDelay = 5000)
public void doSomething() {
// something that should run periodically
}
๊ณต์๋ฌธ์์์๋ ์ด ๋ฉ์๋๊ฐ ์ด๋ ๊ฒ ๋์ํ๋ค๊ณ ์ค๋ช ํ๊ณ ์๋ค "the period is measured from the completion time of each preceding invocation." ๋ค์ ์คํ๊น์ง์ ์๊ฐ์ ์ด์ ์คํ์ด ๋๋ ์์ ๋ถํฐ ๊ณ์ฐ๋๋ค๋ ๋ง์ด๋ค
์๋ฅผ๋ค๋ฉด
12:00:00 ์์
์์
12:00:03 ์์
๋๋จ
fixedDelay = 5์ด
12:00:08 ๋ค์ ์์
์์
Spring ์ค์ผ์ค๋ง์ ๊ธฐ๋ณธ ์ ํ์ง
1. fixedDelay
2. fixedRate
3. cron
+ initialDelay
1. fixedDelay
33. Task Execution and Scheduling
In addition to the TaskExecutor abstraction, Spring 3.0 introduces a TaskScheduler with a variety of methods for scheduling tasks to run at some point in the future. The simplest method is the one named 'schedule' that takes a Runnable and Date only. That
docs.spring.io
fixedDelay๋ ์ด์ ์์ ์ด ๋๋ ๋ค ์ ํด์ง ์๊ฐ๋งํผ ๊ธฐ๋ค๋ ธ๋ค๊ฐ ๋ค์ ์์ ์ ์คํํ๋ ๋ฐฉ์์ด๋ค ์์์ ๋ค๋ฃฌ ๊ฒ์ฒ๋ผ, ๊ณต์๋ฌธ์์์๋ " The fixedDelay attribute … makes sure that there is a delay of five seconds between the finish time of an execution and the start time of the next execution."๋ผ๊ณ ์ค๋ช ํ๊ณ ์๋ค fixedDelay๋ ํ ๋ฒ ์คํ์ด ๋๋ ์์ ๊ณผ ๋ค์ ์คํ์ด ์์๋๋ ์์ ์ฌ์ด์ 5์ด ๊ฐ๊ฒฉ์ผ๋ก ๋ณด์ฅํ๋ค๋ ๋ง์ด๋ค
@Scheduled(fixedDelay = 5000)
public void cleanup() {
// ์์
์คํ
}
12:00:00 ์์
์์
12:00:03 ์์
๋
5์ด ๊ธฐ๋ค๋ฆผ
12:00:08 ๋ค์ ์์
์์
์ฅ์
์ด์ ์์ ์ด ๋๋ ๋ค ๋ค์ ์์ ์ด ์์๋๋ ๊ฑฐ๋ผ ์์ ํ๋ค ์์ ์๊ฐ์ด ๊ธธ์ด์ ธ๋ ๋๋๊ณ ๋์ ๋ค์ ์์์ด๋ผ๋ ํ๋ฆ์ด ๋ ๊ผฌ์ด๊ธฐ ๋๋ฌธ์ด๋ค
๋จ์
์ ํํ ๋งค 5์ด ๋งค 1๋ถ ๊ฐ์ ๊ณ ์ ์๊ฐ์ ๋ณด์ฅํ์ง๋ ์๋๋ค
์์ ์๊ฐ์ด ๊ธธ์ด์ง๋ฉด ์ ์ฒด ์คํ ๊ฐ๊ฒฉ๋ ๋ฐ๋ฆฐ๋ค
์์
3์ด + ๋๊ธฐ 5์ด = ๋ค์ ์คํ์ 8์ด ๋ค
์์
10์ด + ๋๊ธฐ 5์ด = ๋ค์ ์คํ์ 15์ด ๋ค
์ธ์ ์ฌ์ฉํ๋ ๊ฒ ์ ์ ํ ๊น? ํ์ ๋ ์ธ๋ถ API๋๊ธฐํ, ํ์ผ ์ ๋ฆฌ, DB์ ใน, ๋ฉ์ผ ์ฌ์๋, ๋ก๊ทธ ์ ๋ฆฌ ๋ฑ์ ์ ์ ํด ๋ณด์ธ๋ค
2. fixedRate
fixedRate๋ ์์ ์์ ์๊ฐ์ ๊ธฐ์ค์ผ๋ก ์ผ์ ๊ฐ๊ฒฉ๋ง๋ค ์คํํ๋ ค๋ ๋ฐฉ์์ด๋ค
@Scheduled(fixedRate = 5000)
public void collectMetrics() {
// ์์
์คํ
}
๊ณต์๋ฌธ์์์ fixedRate๋ ์ฃผ๊ธฐ ์คํ ์์์ผ๋ก ์ ์๋๊ณ ์๋ค Spring @Scheduled javadoc์ ๋ฐ๋ณต ์์ ์์ cron, fixedDelay, fixedRate ์ค ํ๋๋ฅผ ์ง์ ํ๋ค๊ณ ์ค๋ช ํ๋ค
12:00:00 ์์
์์
12:00:05 ๋ค์ ์์
์์ ์์
12:00:10 ๋ค์ ์์
์์ ์์
์ฆ, ์์ ๊ธฐ์ค์ผ๋ก 5์ด๋ง๋ค๋ผ๊ณ ์๊ฐํ๋ฉด ์ดํดํ๊ธฐ ์ฝ๋ค
์ฅ์
์ผ์ ํ ์ฃผ๊ธฐ๋ก ๊ณ์ ์คํํ๋ ค๋ ์์ ์ ์ข๋ค ์๋ฅผ๋ค์ด 10์ด๋ง๋ค ํ์ฌ ์๋ฒ ์ํ๋ฅผ ์์งํ๊ธฐ ๊ฐ์ ๊ฑด ์์ ๊ฐ๊ฒฉ์ด ์ค์ํ๋ค
๋จ์
์์ ์ด ์ค๋ ๊ฑธ๋ฆฌ๋ฉด ๋ฐ๋ฆฌ๊ฑฐ๋ ๊ฒน์นจ ๋ฌธ์ ๊ฐ ์๊ธธ ์ ์๋ค
Spring ๊ณต์๋ฌธ์๋ ๊ฐ์ ๋ฉ์๋์ ์ฌ๋ฌ ์ค์ผ์ค ์ ์ธ์ด ์์ผ๋ฉด ๋ ๋ฆฝ์ ์ผ๋ก ์ฒ๋ฆฌ๋๊ณ ์๋ก ๊ฒน์ณ ๋ณ๋ ฌ ์คํ๋๊ฑฐ๋ ๋ฐ๋ก ์ด์ด ์คํ๋ ์ ์์ผ๋ ๊ฒน์น์ง ์๊ฒ ์ฃผ์ํ๋ผ๊ณ ๋งํ๊ณ ์๋ค
"such co-located schedules may overlap and execute multiple times in parallel or in immediate succession"
์ธ์ ์ฌ์ฉํ๋ ๊ฒ ์ ์ ํ ๊น? ํ์ ๋ 10์ด๋ง๋ค ๋ฉํธ๋ฆญ ์์ง, 1๋ถ๋ง๋ค ์ํ ์ฒดํฌ, ์ผ์ ์ฃผ๊ธฐ๋ก ์บ์ ๊ฐฑ์ ์๋ ๋ฑ์ ์ ์ ํด ๋ณด์ธ๋ค
3. cron
๋ช ์ด, ๋ช ๋ถ, ๋ช ์, ๋ช ์ผ, ๋ช ์, ๋ฌด์จ ์์ผ์ ์คํํ ์ง๋ฅผ ์์ผ๋ก ์ ๋ฆฌํ๋ ๋ฐฉ์์ด๋ค
@Scheduled(cron = "0 0 3 * * *")
public void cleanupOldTempFiles() {
// ๋งค์ผ ์๋ฒฝ 3์ ์คํ
}
" A well-formed cron expression, such as * * * * * *, consists of six space-separated time and date fields, each with its own range of valid values. " Spring ๊ณต์๋ฌธ์๋ cron ํํ์์ด ์ ํด์ง ํ์์ ๋ฐ๋ผ์ผ ํ๋ค๊ณ ์ค๋ช ํ๊ณ ์๋ค
cron ํํ์์ 6๊ฐ์ ์นธ์ผ๋ก ํํ๋๋ค๋ ๋ง์ด๋ค
์ด ๋ถ ์ ์ผ ์ ์์ผ
0 0 3 * * * -> ๋งค์ผ 03:00:00
์ฅ์
์ฌ๋์ด ์ดํดํ๊ธฐ ์ฌ์ด ์ด์ ์๊ฐ ๊ธฐ์ค์ผ๋ก ์คํํ ์ ์๋ค
์๋ฅผ๋ค์ด ๋งค์ผ ์๋ฒฝ3์, ๋งค์ฃผ ์์์ผ ์ค์ 9์, ๋งค๋ฌ 1์ผ 0์, ํ์ผ 18์.
์ด๋ฐ ์์ ๋ฌ๋ ฅ ๊ธฐ์ค ์์ ์ ์ข๋ค
๋จ์
ํํ์์ด ์ต์ํ์ง ์์ผ๋ฉด ํท๊ฐ๋ฆฐ๋ค
0 0 3 * * *
์ฒ์๋ณด๋ฉด ๋ญ ์๋ฆฐ์ง ๋ชจ๋ฅธ๋ค
๋๋ฒ์งธ๋ก, ์์ ์ด ์ค๋ ๊ฑธ๋ฆด ๊ฒฝ์ฐ ๋ค์ ์คํ ์์ ๊ณผ ๊ฒน์น ์ ์๋์ง ๊ณ ๋ คํด์ผ ํ๋ค ํนํ ์๋ฒ๊ฐ ์ฌ๋ฌ๋๋ฉด ์ค์ํด์ง๋ค
์๋ฒ 1๋ → ์๋ฒฝ 3์์ 1๋ฒ ์คํ
์๋ฒ 2๋ → ๊ฐ ์๋ฒ์์ ์๋ฒฝ 3์์ ์คํ๋ ์ ์์
EC2 ํ ๋๋ฉด ๋จ์ํ์ง๋ง, ๋์ค์ ์๋ฒ๊ฐ ์ฌ๋ฌ ๋๊ฐ ๋๋ฉด ์ค๋ณต ์คํ ๋ฐฉ์ง๋ ๊ณ ๋ฏผํด์ผ ํ๋ค
์ธ์ ์ฌ์ฉํ๋ ๊ฒ ์ ์ ํ ๊น? ๋ผ๊ณ ํ์ ๋ ์ ํด์ง ์๊ฐ์ ์คํํด์ผ ํ๋ ์ด์ ์์ ์ธ ๊ฒฝ์ฐ์ ์ ์ ํด ๋ณด์ธ๋ค
4. initialDelay
initialDelay๋ ์๋ฒ ์ผ์ง ๋ค ๋ฐ๋ก ์คํํ์ง ๋ง๊ณ ์ฒ์ ์คํ๋ง ์กฐ๊ธ ๋ฆ์ถ๋ ์ต์ ์ด๋ค
@Scheduled(initialDelay = 10000, fixedDelay = 60000)
public void cleanup() {
// ์๋ฒ ์์ 10์ด ํ ์ฒซ ์คํ
// ์ดํ ์ด์ ์์
์ข
๋ฃ ํ 60์ด๋ง๋ค ์คํ
}
๊ณต์๋ฌธ์ javadoc์ initialDelay๋ฅผ ์ด๋ ๊ฒ ์ค๋ช ํ๊ณ ์๋ค
"Number of units of time to delay before the first execution" ์ฒซ๋ฒ์งธ ์คํ ์ ์ ์ผ๋ง๋ ๊ธฐ๋ค๋ฆด์ง ์ ํ๋ ๊ฐ์ด๋ผ๋ ๋ป์ด๋ค
https://github.com/spring-projects/spring-framework/blob/main/spring-
context/src/main/java/org/springframework/scheduling/annotation/Scheduled.java
spring-framework/spring-context/src/main/java/org/springframework/scheduling/annotation/Scheduled.java at main · spring-project
Spring Framework. Contribute to spring-projects/spring-framework development by creating an account on GitHub.
github.com
์ฅ์
์๋ฒ ์์ ์งํ ๋ฌด๊ฑฐ์ด ์์ ์ด ๋ฐ๋ก ๋๋ ๊ฑธ ๋ง์ ์ ์๋ค
์๋ฒ๊ฐ ๋ง ์ผ์ ธ์์ ๋๋ ์ ํ๋ฆฌ์ผ์ด์ ์ด๊ธฐํ, DB ์ฐ๊ฒฐ, ์บ์ ์ค๋น ๋ฑ์ผ๋ก ๋ฐ์ ์ ์๋ฐ ๊ทธ๋ ์ค์ผ์ค๋ฌ๊น์ง ๋ฐ๋ก ๋๋ฉด ๋ถ๋ด์ด ๋ ์ ์๋ค
๋จ์
๋จ๋ ์ผ๋ก ๋ฐ๋ณต ์ฃผ๊ธฐ๋ฅผ ์ ํ๋ ํต์ฌ ๋ฐฉ์์ ์๋๋ค ๋๋ถ๋ถ fixedDelay๋ fixedRate์ ๊ฐ์ด ์ด๋ค
| ๋ฐฉ์ | ๊ธฐ์ค | ์ฅ์ | ๋จ์ | ์ด์ธ๋ฆฌ๋ ์์ |
| fixedDelay | ์ด์ ์์
์ข ๋ฃ ํ |
์ด์ ์์
๋๋ ๋ค ์คํ์ด๋ผ ์์ ํจ |
์ ํํ ์๊ฐ ์คํ์ ์๋ | ํ์ผ ์ ๋ฆฌ, ์ฌ์๋, DB ์ ๋ฆฌ |
| fixedRate | ์ด์ ์์
์์ ํ |
์ผ์ ์ฃผ๊ธฐ๋ก ์คํํ๋ ค ํ ๋ ์ข์ | ์์ ์ด ์ค๋ ๊ฑธ๋ฆฌ๋ฉด ๋ฐ๋ฆผ/๊ฒน์นจ ๊ณ ๋ ค | ๋ฉํธ๋ฆญ ์์ง, ์ํ ์ฒดํฌ |
| cron | ๋ฌ๋ ฅ/์๊ฐ | ๋งค์ผ 3์ ๊ฐ์ ์ด์ ์๊ฐ ์ค์ ๊ฐ๋ฅ |
ํํ์์ด ํท๊ฐ๋ฆฌ๊ณ ์ค๋ณต ์คํ ๊ณ ๋ ค ํ์ |
์๋ฒฝ ์ ๋ฆฌ, ํต๊ณ, ์ ์ฐ |
| initialDelay | ์ฒซ ์คํ ์ | ์๋ฒ ์์ ์งํ ๋ถ๋ด ๋ฐฉ์ง | ๋ฐ๋ณต ๋ฐฉ์ ์์ฒด๋ ์๋ | fixedDelay, fixedRate ๋ณด์กฐ |
'Backend > ๐ฑ Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| Spring๊ณผ SpringBoot (0) | 2026.05.05 |
|---|---|
| GlobalException & ErrorException (0) | 2026.01.12 |
| MVC(ModelโViewโController) (0) | 2026.01.12 |
| Spring Bean ์๋ช ์ฃผ๊ธฐ (1) | 2026.01.12 |
| [Spring]API ๋ก๊น (0) | 2025.08.21 |