Spring-boot Scheduler

์žญ์žญ์ดยท2021๋…„ 5์›” 3์ผ
1

Spring-boot

๋ชฉ๋ก ๋ณด๊ธฐ
11/11
post-thumbnail

Spring-boot Scheduler

๐ŸŽ Contents

0. Summary

Linux crontab๊ณผ ๊ฐ™์€ scheduler๋ฅผ Spring-boot์—์„œ ๋‹ค๋ค„๋ณด์ž.
๋ณธ ๋ฌธ์„œ์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ job scheduler๋ฅผ ๋‹ค๋ฃฐ ๊ฒƒ์ด๋‹ค.

  • Spring-boot Framework Scheduler
  • Quartz
  • Jenkins

์ตœ์ข…์ ์œผ๋กœ Jenkins Scheduler๋ฅผ ์ด์šฉํ•ด Spring-batch๋ฅผ ํ•ธ๋“ค๋ง ํ•  ๊ฒƒ์ด๋‹ค.

1. Spring-boot Framework Scheduler

Spring-boot ์ž์ฒด์ ์œผ๋กœ ์ง€์›ํ•˜๋Š” Scheduler๋กœ @EnableScheduling, @Scheduled๋งŒ์œผ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„ ๊ฐ€๋Šฅํ•˜๋‹ค.
๊ฐ„๋‹จํ•œ Job Scheduling์„ ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹๋‹ค.

1.1. Enable Scheduling

Spring-boot ์ง„์ž…์ ์— @EnableScheduling์„ ์„ ์–ธํ•œ๋‹ค.

  • DemoApplication.java
@EnableScheduling
@SpringBootApplication
public class DemoApplication {
  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }
}

1.2 Create Job

์ฃผ๊ธฐ์ ์œผ๋กœ ์‹คํ–‰๋  Job์„ ๊ตฌํ˜„ํ•œ๋‹ค.

  • SpringFrameworkScheduler.java
@Component
public class SpringFrameworkScheduler {
  
  @Scheduled(fixedDelay = 1000)
  public void printJob() {
    System.out.println(new Date());
  }
}
  • @Scheduled(fixedDelay = 1000)
    • 1000ms ์ฃผ๊ธฐ๋กœ ๋ฉ”์†Œ๋“œ๋ฅผ ๋ฐ˜๋ณตํ•œ๋‹ค.
    • fixedDelay์™ธ์—๋„ fixedRate, initialDelay, cron๋“ฑ ๋‹ค์–‘ํ•œ ์˜ต์…˜์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋‹ค์Œ์€ ์ž์ฃผ ์“ฐ์ด๋Š” cron์˜ ์˜ต์…˜์ด๋‹ค.
    • (cron = "์ดˆ ๋ถ„ ์‹œ ์ผ ์›” ์š”์ผ")
    • * : all
    • ? : none
    • m : array
    • a-b : a๋ถ€ํ„ฐ b๊นŒ์ง€
    • a/b : a๋ถ€ํ„ฐ b๋งˆ๋‹ค. a, a+b, a+b+b, ...

1.3 Execute

./gradlew build -x test

java -jar ./build/libs/*.jar

1.4 Check

1000ms์ฃผ๊ธฐ๋กœ ์ถœ๋ ฅ์ด ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

2. Quartz

์ž๋ฐ”๋กœ ๊ฐœ๋ฐœ๋œ ์˜คํ”ˆ์†Œ์Šค Job Scheduling ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ๋ณด๋‹ค ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.
Clustering๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๊ณ  ์—ฌ๋Ÿฌ ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ์ œ๊ณตํ•œ๋‹ค.
๋‹ค๋งŒ ์™„๋ฒฝํ•œ Cluster๊ฐ„ ๋กœ๋“œ ๋ถ„์‚ฐ์€ ์•ˆ๋œ๋‹ค.

Quartz์—์„œ๋Š” Job์„ ๊ด€๋ฆฌํ•˜๊ธฐ๊ฐ€ ์‰ฝ์ง€์•Š๋‹ค.
์ „๋ฐ˜์ ์ธ scheduler๊ด€๋ฆฌ๋ฅผ ์œ„ํ•ด ๊ด€๋ฆฌ UI๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค๋ฉด ๊ทธ๊ฒƒ๋˜ํ•œ ํฐ ๊ณต์ˆ˜๊ฐ€ ๋“ค์–ด๊ฐˆ ๊ฒƒ์ด๋‹ค.
๊ทธ๋ž˜์„œ ์šฐ๋ฆฌ๊ฐ€ ํ”ํžˆ ์•Œ๊ณ ์žˆ๋Š” CI Tool, Jenkins๋ฅผ ์ ๊ทน ์ด์šฉํ•  ๊ฒƒ์ด๋‹ค.

2.1. About Quartz

  • Job
    • execute ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•œ๋‹ค.
    • ์‹ค์ œ ์ž‘์—…์„ execute ๋ฉ”์„œ๋“œ์— ๊ตฌํ˜„ํ•˜๋ฉด ๋œ๋‹ค.
  • JobDataMap
    • Job ์ธ์Šคํ„ด์Šค๊ฐ€ ์‹คํ–‰ํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ •๋ณด๋ฅผ ๋‹ด์„ ์ˆ˜ ์žˆ๋‹ค.
  • JobDetail
    • Job์„ ์‹คํ–‰์‹œํ‚ค๊ธฐ ์œ„ํ•œ ์ •๋ณด๋ฅผ ๋‹ด๊ณ ์žˆ๋‹ค.
    • JobDetail ์ •๋ณด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ Trigger๊ฐ€ ์Šค์ผ€์ค„๋งํ•œ๋‹ค.
  • Trigger
    • Job์„ ์‹คํ–‰์‹œํ‚ฌ ์Šค์ผ€์ค„๋ง ์กฐ๊ฑด์„ ๋‹ด๊ณ ์žˆ๋‹ค.
    • SimpleTrigger, CronTrigger๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • Misfire Instructions
    • ๋ถˆ๋ฐœ๋œ Trigger์— ๋Œ€ํ•œ policy๋ฅผ ์ง€์›ํ•œ๋‹ค.
  • Listener
    • JobListener
      • Job ์‹คํ–‰ ์ „/ํ›„
    • TriggerListener
      • Trigger ๋ฐœ์ƒ/๋ถˆ๋ฐœ/์™„๋ฃŒ
  • JobStore
    • Job๊ณผ Trigger๋ฅผ RAMJobStore๋‚˜ JDBCJobStore๋ฐฉ์‹์œผ๋กœ ์ €์žฅ ๊ฐ€๋Šฅํ•˜๋‹ค.

3. Jenkins

Jenkins๋ฅผ ํ†ตํ•ด ๋ฐฐ์น˜๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
๋งŒ์ผ ์ด๋ฏธ Jenkins๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๋ฉด ๋ฐฐ์น˜ Jenkins์™€ ๋ฐฐํฌ Jenkins๋ฅผ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค.

3.1. Create docker-compose.yaml

vim docker-compose.yaml
version: '2'

networks:
  ci:

services:
  jenkins:
    container_name: jenkins
    image: jenkins/jenkins
    # ์›ํ•˜๋Š” ๊ฒฝ๋กœ์— volume ๋งˆ์šดํŠธ
    volumes:
      - ~/jenkins/:/var/jenkins_home
    networks:
      - ci

3.2. Run Jenkins

docker-comopse up -d

# ํ™•์ธ
docker ps
docker logs -f jenkins

3.3. Init Jenkins

Jenkins๋ฅผ ์˜ฌ๋ฆฐ ์„œ๋ฒ„์˜ 8080ํฌํŠธ๋ฅผ ๋“ค์–ด๊ฐ€์„œ Password๋ฅผ ์ž…๋ ฅํ•ด์•ผํ•œ๋‹ค.
์ดˆ๊ธฐ ํŒจ์Šค์›Œ๋“œ๋Š” ๋‹ค์Œ ๊ฒฝ๋กœ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋‹ค.

# 1. docker log
docker logs -f jenkins

# 2. read password file
cat /var/jenkins_home/secrets/initialAdminPassword

ํŒจ์Šค์›Œ๋“œ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ํ”Œ๋Ÿฌ๊ทธ์ธ๋“ค์„ ๋‹ค์šด๋ฐ›๊ณ  ๋‹ค์Œ๋‹จ๊ณ„์—์„œ ๊ณ„์ •์„ ์ƒ์„ฑํ•˜๋ฉด Jenkins ์ดˆ๊ธฐ์…‹ํŒ…์ด ์™„๋ฃŒ๋œ๋‹ค.

ํ–ฅํ›„ Jenkins์—์„œ ์›ํ•˜๋Š” ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ๋ชจ๋‘ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

3.4. Create jar file

Jenkins๊ฐ€ ๊ตฌ๋™๋˜๋Š” ์„œ๋ฒ„์— Spring-boot jarํŒŒ์ผ์„ ์˜ฎ๊ธฐ๋Š” ์ž‘์—…์ด๋‹ค.
Jenkins๋Š” ํ•ด๋‹น jarํŒŒ์ผ์„ ์„ค์ •๋œ ์ฃผ๊ธฐ๋กœ ์‹คํ–‰์‹œ์ผœ์ฃผ๋Š” ์—ญํ• ์„ ํ•  ๊ฒƒ์ด๋‹ค.

์•„๋ž˜๋Š” spring batch์˜ jarํŒŒ์ผ์„ ๋‹ค๋ฃฌ๋‹ค.

๋ชจ๋“  ์†Œ์Šค๋Š” ํŽ˜์ด์ง€ ํ•˜๋‹จ ๋งํฌ์— ์žˆ๋‹ค.

Spring-boot ํ™˜๊ฒฝ์˜ ์„œ๋ฒ„์—์„œ jar๋ฅผ ๋งŒ๋“  ํ›„ Jenkins์„œ๋ฒ„์— jarํŒŒ์ผ์„ ์˜ฎ๊ธด๋‹ค.

##### Spring-boot Server #####
# ๋‹ค์Œ์„ ํ†ตํ•ด Spring batch๋ฅผ jar๋กœ ๋งŒ๋“ ๋‹ค.
./gradlew build

# Jenkins ์„œ๋ฒ„์— jarํŒŒ์ผ์„ ๋ณต์ œํ•œ๋‹ค.
scp ./build/libs/*.jar $USER@$JENKINS_SERVER:~

##### Jenkins Server #####
# JarํŒŒ์ผ ์‹คํ–‰
java -jar ~/*.jar


Jenkins์„œ๋ฒ„์—์„œ jar๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
simpleStep1์ด ์‹คํ–‰๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

์ด ์ž‘์—…์„ Jenkins Scheduler๊ฐ€ ์ฃผ๊ธฐ์ ์œผ๋กœ ์‹คํ–‰์‹œ์ผœ์ค„ ๊ฒƒ์ด๋‹ค!

3.5. Set the Schedule

์ด์ œ ์œ„์—์„œ ๋งŒ๋“  jar๋ฅผ Jenkins์—์„œ ์Šค์ผ€์ค„๋งํ•˜์ž.

3.5.1. Create item

3.5.2. Enter an item name

3.5.3. Move jar to Jenkins workspace

item์„ ๋งŒ๋“ค๋ฉด workspace๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.
์ƒˆ๋กœ์šด workspace์— jar๋ฅผ ์˜ฎ๊ธฐ๊ธฐ ์œ„ํ•œ ์ž‘์—…์ด๋‹ค.

์ €์žฅ์„ ํ•œ ํ›„, Build Now๋ฅผ ํ†ตํ•ด ๋นŒ๋“œ๋ฅผ ํ•ด์ค€๋‹ค.

์•„๋ž˜๋ฅผ ๋”ฐ๋ผ ๋“ค์–ด๊ฐ€๋ฉด ๋กœ๊ทธ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
Build History > #number > Console Output

๋กœ๊ทธ๋ฅผ ๋ณด์ž.
$PWD๊ฐ€ jenkins agent์˜ Workspace์ธ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

์‹ค์ œ jenkins container๋‚˜ volume mount๊ฒฝ๋กœ๋ฅผ ๋ณด๋ฉด ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ•ด๋‹น ๊ฒฝ๋กœ์— jarํŒŒ์ผ์„ ์˜ฎ๊ธด๋‹ค.

3.5.4. Set the schedule

์ด์ œ jarํŒŒ์ผ์„ ์ฃผ๊ธฐ์ ์œผ๋กœ ์‹คํ–‰์‹œ์ผœ์ฃผ๋ฉด ๋์ด๋‹ค.
๊ตฌ์„ฑ์„ ๋ˆŒ๋Ÿฌ ๋ฐฉ๊ธˆ ์ƒ์„ฑํ•œ item๋ฅผ ์ˆ˜์ •ํ•œ๋‹ค.

์ฃผ๊ธฐ๋ฅผ ์ •ํ•ด์ค€๋‹ค. (ex. 1๋ถ„๋งˆ๋‹ค)

์‰˜ ์Šคํฌ๋ฆฝํŠธ์— ๋‹ค์Œ์„ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.
java -jar demo-0.0.1-SNAPSHOT.jar

3.6. Check

์ด์ œ Jenkins๊ฐ€ 1๋ถ„๋งˆ๋‹ค ๋นŒ๋“œ๋ฅผ ํ•˜๋Š”๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

๋กœ๊ทธ๋ฅผ ์‚ดํŽด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

์ด์ œ Spring batch๋ฅผ Jenkins Scheduler๋กœ ํ•ธ๋“ค๋งํ•˜๋Š” ์ž‘์—…์„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.


๋ชจ๋“  ์†Œ์Šค๋Š” ๊นƒํ—ˆ๋ธŒ์— ์˜ฌ๋ ค๋†“์•˜๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€