@Schedule Spring ์คํ๋ง ์ค์ผ์ฅด ์ค์ ๋ฒ & CronTab
์คํ๋ง MVC ์์ย ์ผ์ ์๊ฐ ์ฃผ๊ธฐ์ ์ผ๋ก ์์
ํ๋ ์ค์ผ์ฅด๋ฌ๋ฅผ ๋ง๋ค์ด ๋ณธ๋ค.
์ผ๋จ root-context.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:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
ย ย ย ย http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
ย ย ย ย http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
ย ย ย ย http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="careerAlarmService" class="kwon.app.profile.AlarmTask" />
<task:scheduler id="gsScheduler" pool-size="10" />
<task:executor id="gsTaskExecutor" pool-size="10" />
<task:annotation-driven executor="gsTaskExecutor"
ย scheduler="gsScheduler" />
<!-- Root Context: defines shared resources visible to all other web components -->
class๋ ์ค์ classํ์ผ์ด ์๋ ์์น ์ก์์ฃผ๋ฉด ๋๋ค.
<!-- <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> -->
์ด๊ฒ๋ง ์ ํ ์์์๊ฑฐ๋ค. ๊ทธ๋ฆฌ๊ณ ๋์
private static final Logger logger = LoggerFactory.getLogger(AlarmTask.class);
@Scheduled(cron="*/30 * * * * *")
public void scheduleRun(){
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//System.out.println("์ค์ผ์ค ์คํ : " + dateFormat.format(calendar.getTime()));
logger.info("์ค์ผ์ค ์คํ : " + dateFormat.format(calendar.getTime()));
//System.out.println("Request " + getCurrentRequest());
ํด๋์ค๋ฅผ ๋ง๋ค์ด ์ค๋ค.
์ด๋ฌ๋ฉด 30์ด๋ง๋ค ์คํ๋๋ ๊ฒ์ด๋ค.
@Scheduled(cron="*/30 * * * * *")
****************************************************************************************************
์คํ๋ง์์๋ ๊ฐํธํ๊ณ ํ๋ ์์ํฌ์์์ ์ค์ผ์ฅด๋ง ํ๋ quatz ๋ฅผ annotation ์ผ๋ก ๋ ๊ฐ๋จํ๊ฒ ์ค์ ํ ์ ์๋ค.
applicationContext.xml ๊ฐ์ bean ์ ์ค์ ํ๋ ์ค์ ํ์ผ์
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
<task:annotation-driven />
๊ตฌ๋ฌธ์ ์
๋ ฅํ๋ฉด ๋๋ค.
EX) ๋ณ๋์ context-schedule.xml ๋ก ๊ด๋ฆฌ(์ฒจ๋ถํ์ผ ํ์ธ)
ํ์ผ๋ช
: context-schedule.xml ย ( context-schedule.xml)
์ค์ผ์ฅด๋ง ํ ๋ฉ์๋์์ @scheduled ์ด๋
ธํ
์ด์
์ ์
๋ ฅํ ํ์ ์๊ฐ์ค์ ํ๋ฉด ๋
3. ์๊ฐ ์ค์ @scheduled(cron=" ") ย * ๋ฆฌ๋
์ค crontab ๊ณผ ๊ฐ์ ์ค์ ๋ฐฉ๋ฒ
ex> @Scheduled(cron="0 0 02 * * ?") = ๋งค์ผ ์๋ฒฝ2์์ ์คํ
ex> @Scheduled(cron="0 0 02 2,20 * ?") = ๋งค์ 2์ผ,20์ผ ์๋ฒฝ2์์ ์คํ
****************************************************************************************************
์ 1-12 or JAN-DEC , - * /
์์ผ 1-7 or SUN-SAT , - * ? / L #
๋
(์ต์
) 1970-2099 , - * /
- : ๋ฒ์ ์ง์ ์ ์ฌ์ฉ
, : ์ฌ๋ฌ ๊ฐ ์ง์ ๊ตฌ๋ถ์ ์ฌ์ฉ
/ : ์ด๊ธฐ๊ฐ๊ณผ ์ฆ๊ฐ์น ์ค์ ์ ์ฌ์ฉ
L : ์ง์ ํ ์ ์๋ ๋ฒ์์ ๋ง์ง๋ง ๊ฐ
W : ์~๊ธ์์ผ ๋๋ ๊ฐ์ฅ ๊ฐ๊น์ด ์/๊ธ์์ผ
# : ๋ช ๋ฒ์งธ ๋ฌด์จ ์์ผ 2#1 => ์ฒซ ๋ฒ์งธ ์์์ผ
์์ ) Expression Meaning
์ด ๋ถ ์ ์ผ ์ ์ฃผ(๋
)
"0 0 12 * * ?" : ์๋ฌด ์์ผ, ๋งค์, ๋งค์ผ 12:00:00
"0 15 10 ? * *" : ๋ชจ๋ ์์ผ, ๋งค์, ์๋ฌด ๋ ์ด๋ 10:15:00
"0 15 10 * * ?" : ์๋ฌด ์์ผ, ๋งค์, ๋งค์ผ 10:15:00
"0 15 10 * * ? *" : ๋ชจ๋ ์ฐ๋, ์๋ฌด ์์ผ, ๋งค์, ๋งค์ผ 10:15
"0 15 10 * * ? : 2005" 2005๋
์๋ฌด ์์ผ์ด๋ ๋งค์, ๋งค์ผ 10:15
"0 * 14 * * ?" : ์๋ฌด ์์ผ, ๋งค์, ๋งค์ผ, 14์ ๋งค๋ถ 0์ด
"0 0/5 14 * * ?" : ์๋ฌด ์์ผ, ๋งค์, ๋งค์ผ, 14์ ๋งค 5๋ถ๋ง๋ค 0์ด
"0 0/5 14,18 * * ?" : ์๋ฌด ์์ผ, ๋งค์, ๋งค์ผ, 14์, 18์ ๋งค 5๋ถ๋ง๋ค 0์ด
"0 0-5 14 * * ?" : ์๋ฌด ์์ผ, ๋งค์, ๋งค์ผ, 14:00 ๋ถํฐ ๋งค 14:05๊น์ง ๋งค ๋ถ 0์ด
"0 10,44 14 ? 3 WED" : 3์์ ๋งค ์ฃผ ์์์ผ, ์๋ฌด ๋ ์ง๋ 14:10:00, 14:44:00
"0 15 10 ? * MON-FRI" : ์~๊ธ, ๋งค์, ์๋ฌด ๋ ์ด๋ 10:15:00
"0 15 10 15 * ?" : ์๋ฌด ์์ผ, ๋งค์ 15์ผ 10:15:00
"0 15 10 L * ?" : ์๋ฌด ์์ผ, ๋งค์ ๋ง์ง๋ง ๋ 10:15:00
"0 15 10 ? * 6L" : ๋งค์ ๋ง์ง๋ง ๊ธ์์ผ ์๋ฌด ๋ ์ด๋ 10:15:00
"0 15 10 ? * 6L 2002-2005" : 2002๋
๋ถํฐ 2005๋
๊น์ง ๋งค์ ๋ง์ง๋ง ๊ธ์์ผ ์๋ฌด ๋ ์ด๋ 10:15:00
"0 15 10 ? * 6#3" : ๋งค์ 3๋ฒ์งธ ๊ธ์์ผ ์๋ฌด ๋ ์ด๋ 10:15:00
์) 40 3 * * * root /home/mysql/mysql_backup.sh
๋งจ ์์ 40์ 40๋ถ์ ์๋ฏธํจ (๋ถ์ ์๋ฏธ:0~59)
๊ทธ ๋ค์ 3์ 03์๋ฅผ ์๋ฏธํจ (์๋ฅผ ์๋ฏธ:0~23)
๊ทธ ๋ค์ * ์ ๋งค์ผ์ ์๋ฏธํจ (์ผ์ ์๋ฏธ:1~31)
๊ทธ ๋ค์ * ์ ๋งค์์ ์๋ฏธํจ (์์ ์๋ฏธ:1~12)
๊ทธ ๋ค์ * ์ ๋งค์ฃผ๋ฅผ ์๋ฏธํจ(์์ผ์ ์๋ฏธ 1:์์์ผ~7:์ผ์ฉ์ผ)
๊ทธ ๋ค์ root /home/mysql/mysql_backup.sh ๋ root ย ๊ณ์ ์ผ๋ก mysql_backup.sh์ ์คํํ๋ผ๋ ์๋ฏธ
๋ฌธ์ : ๊ฐ ํ๋์ ํด๋นํ๋ ๋ชจ๋ ์ซ์๋ฅผ ์๋ฏธ
๋ฌธ์ : ๊ฐ ํ๋์๋ฆฌ์ ํ์ดํ ๋ฌธ์๊ฐ ์ฌ์ ์์
ex) ์ผ ํ๋์๋ฆฌ์ 11-15 (11,12,13,14,15์ผ์ ์๋ฏธ)
๋ฌธ์ : ๊ฐ ํ๋์๋ฆฌ์ ์ฝค๋ง๋ฌธ์๊ฐ ์ฌ์ ์์
ex) ์ผ ํ๋์๋ฆฌ์ 1,11,21 (1์ผ,11์ผ 21์ผ์ ์๋ฏธ)
@Scheduled(fixedDelay=1000)
1000 ์ ์ผ๋ฉด 1์ด ์ธ๊ฒ์ด๋ค!!!!
30์ด ํ๋ ค๋ฉด 1000 * 30 ํ๋ฉด ๋์ง ๋ญ.