๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/Web

[Spring] Scheduler(์Šคํ”„๋ง ์Šค์ผ€์ค„๋Ÿฌ) ์„ค์ •ํ•˜๊ธฐ

by ynzu๐Ÿค 2021. 11. 30.
๋ฐ˜์‘ํ˜•

์Šคํ”„๋ง์—์„œ ํŠน์ • ์‹œ๊ฐ„ ํ˜น์€ ๋ช‡ ๋ถ„, ๋ช‡ ์‹œ๊ฐ„ ๋งˆ๋‹ค ๊ธฐ๋Šฅ์„ ๋™์ž‘ ์‹œํ‚ค๋ ค๋ฉด Spring์—์„œ ์ œ๊ณตํ•˜๋Š” scheduler๋ฅผ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค.

(Spring 3.1๋ถ€ํ„ฐ๋Š” Quartz์—†์ด ๊ฐ„๋‹จํ•˜๊ฒŒ ์Šค์ผ€์ค„๋Ÿฌ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•˜๋‹ค.)

โ€‹

๋จผ์ € applicationContext์— ์Šค์ผ€์ค„๋Ÿฌ๋ฅผ ๋“ฑ๋กํ•œ๋‹ค. (Component Scan์ด ์„ ์–ธ๋˜์–ด ์žˆ๋Š” ํŒŒ์ผ)

  • applicationContext.xml (์š”์•ฝ)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:util="http://www.springframework.org/schema/util"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:task="http://www.springframework.org/schema/task"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">

<!-- =================================================================== -->
<!-- Component Scan -->
<!-- =================================================================== -->
<context:component-scan base-package="com.company.test" >
 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>

<!-- =================================================================== -->
<!-- Scheduler -->
<!-- =================================================================== -->

<task:scheduler id="scheduler" pool-size="2" />
<task:scheduled-tasks scheduler="scheduler" >
 <task:scheduled ref="testJob" method= "doJob" cron="0 0/30 * * * *" />
</task:scheduled-tasks>
</beans>

 => testJob์˜ doJob ๋ฉ”์†Œ๋“œ๋ฅผ 30๋ถ„๋งˆ๋‹ค ์‹คํ–‰ํ•˜๋Š” ์Šค์ผ€์ค„๋Ÿฌ๋ฅผ ๋“ฑ๋กํ–ˆ๋‹ค. testJob์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

 

  • TestJob.java
@Service("testJob")
public class TestJob {
 public synchronized void doJob() {
  System.out.println("###################################################");
  System.out.println("Scheduler");
  System.out.println("###################################################");
 }
}

 

Cron Expression (์ถœ์ฒ˜ : https://blog.naver.com/cyon24/203674742)

์™ผ์ชฝ๋ถ€ํ„ฐ ์˜ค๋ฅธ์ชฝ์ˆœ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜๋ฏธ๊ฐ€ ์žˆ๋‹ค

Seconds
0 ~ 59
Minutes
0 ~ 59
Hours
0 ~ 23
Day of Month
1 ~ 31
Month
1 ~ 12
Day of Week
1 ~ 7 (1 => ์ผ์š”์ผ, 7=> ํ† ์š”์ผ / MON,SUN...)
Years(optional)
1970 ~ 2099

โ€‹

ํŠน์ˆ˜๋ฌธ์ž์˜ ์‚ฌ์šฉ์€ ์•„๋ž˜์™€ ๊ฐ™์€ ์˜๋ฏธ๊ฐ€ ์žˆ๋‹ค.

*
๋ชจ๋“ ์ˆ˜๋ฅผ ์˜๋ฏธ, Minutes ์œ„์น˜์— ์‚ฌ์šฉ๋  ๊ฒฝ์šฐ ๋งค๋ถ„๋งˆ๋‹ค ๋ผ๋Š” ๋œป
?
Day of Month, Day of Week์—๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ, ํŠน๋ณ„ํ•œ ๊ฐ’์ด ์—†๋‹ค๋Š” ๋œป
-
๊ธฐ๊ฐ„์„ ์„ค์ •, Hour ์œ„์น˜์— 10 - 12 ๋ผ๊ณ  ์“ฐ๋ฉด 10, 11, 12dp ๋™์ž‘ํ•˜๋ผ๋Š” ๋œป
,
ํŠน์ • ์‹œ๊ฐ„์„ ์„ค์ •. Day of Week ์œ„์น˜์— 2, 4, 6 ์ด๋ผ๊ณ  ์“ฐ๋ฉด ์›”, ์ˆ˜, ๊ธˆ์—๋งŒ ๋™์ž‘ํ•˜๋ผ๋Š” ๋œป
/
์ฆ๊ฐ€๋ฅผ ํ‘œํ˜„, Seconds ์œ„์น˜์— 0/15๋กœ ์„ค์ •๋˜์–ด ์žˆ์œผ๋ฉด, 0์ดˆ์— ์‹œ์ž‘ํ•ด์„œ 15์ดˆ ๊ฐ„๊ฒฉ์œผ๋กœ ๋™์ž‘ ํ•˜๋ผ๋Š” ๋œป
L
Day Of Month ์—์„œ๋งŒ ์‚ฌ์šฉํ•˜๋ฉฐ, ๋งˆ์ง€๋ง‰ ๋‚ ์˜ ์˜๋ฏธ Day of Month ์— L๋กœ ์„ค์ •๋˜์–ด ์žˆ์œผ๋ฉด ๊ทธ๋‹ฌ์˜ ๋งˆ์ง€๋ง‰๋‚ ์— ์‹คํ–‰ํ•˜๋ผ๋Š” ๋œป
W
Day of Month ์—๋งŒ ์‚ฌ์šฉํ•˜๋ฉฐ, ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ํ‰์ผ์„ ์˜๋ฏธ. 15W๋กœ ์„ค์ •๋˜์–ด ์žˆ๊ณ  15์ผ์ด ํ† ์š”์ผ์ด๋ฉฐ, ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ํ‰์ผ์ธ 14์ผ ๊ธˆ์š”์ผ์— ์‹คํ–‰, 15์ผ์ด ์ผ์š”์ผ์ด๋ฉด 16์ผ ์›”์š”์ผ์— ์‹คํ–‰๋œ๋‹ค.
15์ผ์ด ํ‰์ผ์ด๋ฉด ๊ทธ๋‚  ๊ทธ๋Œ€๋กœ ์‹คํ–‰๋จ
LW
L๊ณผ W๋ฅผ ๊ฒฐํ•ฉํ•˜์—ฌ ์‚ฌ์šฉ, ๊ทธ๋‹ฌ์˜ ๋งˆ์ง€๋ง‰ ํ‰์ผ์˜ ์˜๋ฏธ
#
Day of Week์— ์‚ฌ์šฉ, 6#3 ์˜ ๊ฒฝ์šฐ 3๋ฒˆ์งธ ์ฃผ ๊ธˆ์š”์ผ์— ์‹คํ–‰๋œ๋‹ค.

โ€‹

์‚ฌ์šฉ ์˜ˆ)

0 0 12 * * * ==> ๋งค์ผ 12์‹œ์— ์‹คํ–‰

0 15 10 * * * ==> ๋งค์ผ 10์‹œ 15๋ถ„์— ์‹คํ–‰

0 * 14 * * * ==> ๋งค์ผ 14์‹œ์— ์‹คํ–‰

0 0/5 14 18 * * * ==> ๋งค์ผ 14์‹œ, 18์‹œ์— ์‹œ์ž‘ํ•ด์„œ 5๋ถ„๊ฐ„๊ฒฉ์œผ๋กœ ์‹คํ–‰

0 0-5 14 * * * ==> ๋งค์ผ 14์‹œ์— ์‹œ์ž‘ํ•ด์„œ 0๋ถ„๋™์•ˆ ์‹คํ–‰

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€