ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Spring으로 배치 만들기 (quartz, scheduler,task)
    Spring 2020. 6. 7. 22:45
    반응형

    스프링에는 일괄 처리(batch processing) 배치를 만드는 여러가지의 기능이 있다.

    방법1. quartz 스케줄링 객체 사용

    방법2. <task:scheduler> 설정 사용

    방법3. @Scheduled 어노테이션 사용

    실행환경 Spring4 / java 1.8

    <!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz -->
    <dependency>
    	<groupId>org.quartz-scheduler</groupId>
    	<artifactId>quartz</artifactId>
    	<version>2.3.2</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-context-support</artifactId>
    	<version>4.3.4.RELEASE</version>
    </dependency>

     

    pom.xml에 위의 내용을 dependency추가 한다.

     

    1.  quartz 스케줄링 객체 사용

    context-batch.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" 
    	xmlns:context="http://www.springframework.org/schema/context" 
        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-4.0.xsd         
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd ">
    		
       	<!--컴포넌트 스켄을 해야만 @컴포넌트 어노테이션을 인식 할 수 있다. -->
    	<context:component-scan base-package="com.devmg.app" /> 
    	<!-- 스케쥴러 클래스 정의  -->
        <bean id="jobBean" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        	<property name="targetObject" ref="jobComponent" />  <!--컴포넌 설정 -->
        	<property name="targetMethod" value="jobMethod" /> <!-- Method 설정 -->
        	<property name="concurrent" value="false" /> <!-- 동시실행 기능 방지한다-->
        </bean>
        <!-- 트리거 설정 --> 
        <bean id="jobTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
        	<property name="jobDetail" 	ref="jobBean" /> 
        	<property name="startDelay" value="1000" /> <!-- 서버 가동 후 시작할 시간   설정(단위: millisecond)  -->
        	<property name="repeatInterval" value="10000" />  <!--반복  시간 설정 설정(단위: millisecond)  -->
        </bean>
    
    	<!-- 스케줄러 설정 -->
    	<bean id="jobScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 
    		<property name="triggers"> 
    			<list> 
    				<!-- 앞에서 설정한 트리거를 등록합니다. 필요하면 여러개 만들어서 등록하면 된다. -->
    				<ref bean="jobTrigger"/> 
    			</list> 
    		</property> 
    	</bean>
    </beans>

    배치를 위하 클래스 작성

    OneScheduling.java

    @Component("jobComponent")
    public class OneScheduling{
    	
    	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
    	
        public void jobMethod() throws Exception {
        	Calendar calendar = Calendar.getInstance();
        	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        	logger.info("스케줄 실행 : " + dateFormat.format(calendar.getTime()));
         
        }
    }

    위의 클래스에서 Controller, Servce , Dao를 사용 하고 싶으면 반드시 위의 xml에 컴포포넌트 스캔 한줄을 넣어야 한다.

    <context:component-scan base-package="패키지명"> 

     

    2. <task:scheduler> 설정 사용

    context-batch.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" 
       	xmlns:context="http://www.springframework.org/schema/context" 
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:task="http://www.springframework.org/schema/task"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    	            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    			http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
    
    	<!--컴포넌트 스켄을 해야만 @컴포넌트 어노테이션을 인식 할 수 있다. -->
    	<context:component-scan base-package="com.devmg.app" /> 
    	<!-- 	task 를 @ 애너테이션 기반으로 자동 검색 -->
    	<task:annotation-driven />	
    	<task:scheduler id="jobTest" pool-size="2"/>
    	  
    	<task:scheduled-tasks scheduler="jobTest" >
       		<task:scheduled ref="jobComponent" method= "jobMethod" cron="0/10 * * * * ?" /> <!--클래스,메서드 및 시간 설정-->
    	</task:scheduled-tasks> 
    </beans>

     

    task정보를 설정하고 cron식으로 시간을 설정한다.

    Spring Schedule을 실행시키기 위해서는 task가 실행되는 시간/주기 등을 설정하게 되는데 이러한 cron의 표현식에 대한 자세한 설명을 한다.

    cron 표현식

    문자열의 좌측부터 우측까지 아래의 순서대로 의미가 있고 각 항목은 space 문자로 구분한다.

     순서 필드명 사용 가능한 값
     1  seconds  0~59 , - * /
     2  minutes  0~59 , - * /
     3  hours  0~23 , - * /
     4  day of month  1~31 , - * ? / L W
     5  month  1~12 or JAN-DEC , - * /
     6  day of week  1-7 or SUN-SAT , - * ? / L #
     7  years (optional)  1970~2099 , - * /

     

    특수문자의 의미

     기호 의미   사용 예
     *  모든 수를 의미 seconds에서 사용하면 매초, minutes에서 사용하면 매분, hours에서 사용하면 매시간 
     ?  해당 항목을 사용하지 않음 day of month에서 사용하면 월중 날짜를 지정하지 않음. day of week에서 사용하면 주중 요일을 지정하지 않음 
     -  기간을 설정 hours에서 10-12이면 10시, 11시, 12시에 동작
    minutes에서 58-59이면 58분, 59분에 동작
     , 특정 시간을 지정 day of week에서 2,4,6이면 월,수,금에만 동작함
     / 시작시간과 반복 간격 설정 seconds위치에 0/15로 설정하면 0초에 시작해서 15초 간격으로 동작 
    minutes위치에 5/10으로 설정하면 5분에 시작해서 10분 간격으로 동작
     L 마지막 기간에 동작
    day of month, day of week에서만 사용
    day of month에서 사용하면 해당월 마지막 날에 수행
    day of week에서 사용하면 토요일에 수행
     W 가장 가까운 평일 동작
    day of month에만 사용 
    15W로 설정하면 15일이 토요일이면 가장 가까운 14일 금요일에 실행
    15W로 설정하고 15일이 일요일이면 16일에 실행
    15W로 설정하고 15일이 평일이면 15일에 실행
     LW L과 W의 조합  그달의 마지막 평일에 동작 
     # 몇 번째 주와 요일 설정
    day of week에 사용 
    6#3이면 3 번째 주 금요일에 동작 
    4#2이면 2번째 주 수요일에 동작

     

    사용 예

     표현식 의미 
     0 0 12 * * * 매일 12시에 실행 
     0 15 10 * * * 매일 10시 15분에 실행 
     0 * 14 * * * 매일 14시에 0분~59분까지 매분 실행
     0 0/5 14 * * * 매일 14시에 시작해서 5분 간격으로 실행 
     0 0/5 14,18 * * * 매일 14시, 18시에 시작해서 5분 간격으로 실행 
     0 0-5 14 * * * 매일 14시에 0분, 1분, 2분, 3분, 4분, 5분에 실행 
     0 0 20 ? * MON-FRI 월~금일 20시 0분 0초에 실행 
     0 0/5 14 * * ? 아무요일, 매월, 매일 14:00부터 14:05분까지 매분 0초 실행 (6번 실행됨)
     0 15 10 ? * 6L  매월 마지막 금요일 아무날이나 10:15:00에 실행
     0 15 10 15 * ? 아무요일, 매월 15일 10:15:00에 실행 
     * /1 * * * * 매 1분마다 실행
     * /10 * * * * 매 10분마다 실행 

     

    이전 Task와 Delay 설정

     표현 의미 
     fixed-delay  이전에 실행된 task의 종료시간으로부터 정의된 시간만큼 지난 후 다음 task를 실행
    밀리세컨드 단위
     fixed-rate  이전에 실행된 task의 시작 시간으로부터 정의된 시간만큼 지난 후 다음 task를 실행
    밀리세컨드 단위

     

    스케줄링 작업(task)을 여러개 설정하고 싶을 경우 <task:scheduled> 태그를 추가 작성한다.

    TowScheduling.java

    package com.devmg.app.batch;
    
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;
    
    import com.devmg.app.HomeController;
    
    @Component("jobComponent")
    public class TowScheduling{
    	
    	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
    	
        public void jobMethod() throws Exception {
        	Calendar calendar = Calendar.getInstance();
        	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        	logger.info("스케줄 실행 : " + dateFormat.format(calendar.getTime()));
         
        }
    }
    	
    

    위의 클래스에서 Controller, Servce , Dao를 사용 하고 싶으면 반드시 위의 xml에 컴포포넌트 스캔 한줄을 넣어야 한다.

    <context:component-scan base-package=""> 

    3. @Scheduled 어노테이션 사용

    지금까지의 방법 중에서 제일 간단한 방법이다. 배치기능의 클래스를 빈으로 등록하고 자바에서 크론설정을 한다.

    context-batch.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" 
       	xmlns:context="http://www.springframework.org/schema/context" 
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:task="http://www.springframework.org/schema/task"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    	            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    			http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
    	<!-- 	task 를 @ 애너테이션 기반으로 자동 검색 -->
    	<task:annotation-driven />	
    	<!-- 스케쥴러 클래스 정의  -->
       	<bean id="jobScheduler" class="com.devmg.app.batch.ThreeScheduling" /><!--배치클래스의 경로-->
    </beans>

    어노테이션 검색 코드를 추가한다.

    <task:annotation-driven/>

     

    ThreeScheduling.java

    public class ThreeScheduling{
    
    	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
    
    	@Scheduled(cron="0/1 * * * * *")  // 1초 주기로 실행하라는 의미
    	public void jobMethod() throws Exception {
    		Calendar calendar = Calendar.getInstance();
    		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	logger.info("스케줄 실행 : " + dateFormat.format(calendar.getTime()));
    
    	}
    }

    @Schduled 어노테이션에 cron 표현식을 사용한다.

    반응형

    댓글

Designed by Tistory.