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

๐Ÿ“– Java&Spring

Spring Cron Expression

ํฌ๋ก  ํ‘œํ˜„์‹์€ Spring์—์„œ ํŠน์ • ์‹œ๊ฐ„ ๋งˆ๋‹ค ๋ฐ˜๋ณต๋˜๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํ‘œํ˜„์‹ ์ž…๋‹ˆ๋‹ค
์Šค์ผ€์ค„๋Ÿฌ๋Š” DB ์ž‘์—…์ด๋‚˜ ๋ฐ˜๋ณต๋˜๋Š” ์ž‘์—… ๋“ฑ๋“ฑ์—์„œ ๋งŽ์ด ์“ฐ์ด๋Š”๋ฐ์šฉ,
Spring์˜ cron์„ ์ด์šฉํ•ด์„œ ์‹œ๊ฐ„ ์ฃผ๊ธฐ๋ฅผ ์„ค์ •ํ•  ์ˆ˜๊ฐ€ ์žˆ๋‹ต๋‹ˆ๋‹ค.

 

๐ŸŒฑ servlet-context.xml ํŒŒ์ผ์— task namespace ์ถ”๊ฐ€ํ•˜๊ธฐ

<task:scheduled-tasks>
        <task:scheduled ref="className" method="excuteFunction" cron="0 0/1 * * * ?" />  
 </task:scheduled-tasks>

servlet-context.xml์— task namespace๋ฅผ ์ถ”๊ฐ€ํ•œ ๋ชจ์Šต ์ž…๋‹ˆ๋‹ค

task:scheduled-tasks๋กœ ์‹œ์ž‘ํ•ด์ฃผ๊ณ , ref์—๋Š” ์Šค์ผ€์ค„์ด ๋™์ž‘๋  ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

  • ref์—๋Š” bean์„ ๋“ฑ๋กํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋ผ์„œ, @Component, @Controller ๋“ฑ์˜ Annotation์œผ๋กœ ๋“ฑ๋ก๋œ ์ด๋ฆ„์„ ์ถ”๊ฐ€

method์—๋Š” ์‹คํ–‰๋  ๋ฉ”์„œ๋“œ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค



๐ŸŒฑ cron ํ‘œํ˜„์‹ ์„ค์ •ํ•ด์ฃผ๊ธฐ

ํฌ๋ก  ํ‘œํ˜„์‹์€ ์•ž์—์„œ๋ถ€ํ„ฐ [์ดˆ-๋ถ„-์‹œ-์ผ-์›”-์š”์ผ-๋…„๋„] ๋กœ ๊ตฌ์„ฑ์ด ๋˜๊ณ ,
๋…„๋„๋Š” ์ƒ๋žต ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค

ํ‘œํ˜„์‹์€ ๊ณต์‹ ์‚ฌ์ดํŠธ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹น
https://spring.io/blog/2020/11/10/new-in-spring-5-3-improved-cron-expressions

 

New in Spring 5.3: Improved Cron Expressions

<div class="paragraph"> <p>If you regularly listen to <a href="https://bootifulpodcast.fm">A Bootiful Podcast</a>, you might have heard about the improvements we made to Spring Framework’s cron support. Cron expressions are mostly used in Spring applicat

spring.io

์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์œ„ ์˜ˆ์ œ์ฒ˜๋Ÿผ

 cron="0 0/1 * * * ?" /> 

์ด๋ ‡๊ฒŒ ์ •์˜ ํ–ˆ๋‹ค๋ฉด, 1๋ถ„์— ํ•œ๋ฒˆ์”ฉ excuteFunction ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰ ๋ฉ๋‹ˆ๋‹ค.

๋งŒ์•ฝ 2023๋…„์— 12์‹œ 45๋ถ„์— ๋™์ž‘ํ•˜๋„๋ก ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด?

 cron="0 45 12 * * * 2023" /> 

์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๊ฑธ ์–ธ์ œ ์ž‘์„ฑํ•˜๋‚˜;; ํ•˜์‹œ๊ณ  ๊ณ„์‹œ๋‹ค๋ฉด ์ž๋™์œผ๋กœ ๋งŒ๋“ค์–ด์ฃผ๋Š” ์‚ฌ์ดํŠธ๋„ ์žˆ์Šต๋‹ˆ๋‹ฅ ๐Ÿซ 

http://www.cronmaker.com/;jsessionid=node0nxisjbagfffd2bfs6g3qqv7h847086.node0?0

 

CronMaker

 

www.cronmaker.com

 

๐ŸŒฑ @Scheduled Annotation์œผ๋กœ ์„ค์ •ํ•˜๊ธฐ

servlet-context.xml ๋ง๊ณ  annotation์„ ๋ถ™์—ฌ์„œ๋„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ฅ

/*...*/
@Scheduled(cron="0 0/1 * * * ?")
public void excuteFunction()
{
    ...
}
/*...*/