Java/BATCH

스프링 + Quartz 시작해보자

마시멜로를찾아서 2019. 1. 23. 13:34
반응형

1. pom.xml에 dependencies 에 추가 

<dependencies>

    <dependency>

        <groupId>org.quartz-scheduler</groupId>

        <artifactId>quartz</artifactId>

        <version>2.2.1</version>

    </dependency>

</dependencies>   


2. web.xml


<context-param>

<param-name>contextConfigLocation</param-name>

        <param-value>/WEB-INF/config/context-quartz.xml</param-value>

</context-param>


3. context-quartz.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:batch="http://www.springframework.org/schema/batch"

       xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"

       xmlns:jdbc="http://www.springframework.org/schema/jdbc"

       xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/batch     http://www.springframework.org/schema/batch/spring-batch.xsd

        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd

        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

 

 

    <bean id="testTask" class="test.batch.testTask"/>

 

    <bean name="testJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"

          p:jobClass="test.batch.TestJobBean"

          p:durability="true">

        <property name="jobDataAsMap">

            <map>

                <entry key="testTask" value-ref="testTask"/>

            </map>

        </property>

    </bean>


    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"

          p:jobDetail-ref="testJob"

          p:startDelay="1000"

          p:cronExpression="0 5 0 * * ? *"/>

 

    <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

        <property name="triggers">

            <list>

                <ref bean="cronTrigger"/>

            </list>

        </property>

    </bean>

 

</beans>





1.초(Seconds) 0 ~ 59 

2.분(Minutes) 0 ~ 59 

3.시간(Hours) 0 ~ 23

4.달의 날짜(Day-of-month) 1 ~ 31

5.달(Month)  1 ~ 12 or JAN ~ DEC

6.주의 날짜(Day-of-week) 1 ~ 7 or SUN-SAT

7.년도(Year) (선택가능)  빈값


* 모든 수를 나타냄   

- 값의 사이를 의미

, 특정값 지칭

/ 값의 증가를 표현

? 특별한 값이 없음을 나타냄(day-of-month, day-of-week 필드만 사용)  

L 마지막 날을 나타냄(day-of-month, day-of-week 필드만 사용)


4. TestJobBean.java


package test.batch;


import org.quartz.DisallowConcurrentExecution;

import org.quartz.JobExecutionContext;

import org.quartz.JobExecutionException;

import org.springframework.scheduling.quartz.QuartzJobBean;

import org.springframework.stereotype.Component;


@Component

@DisallowConcurrentExecution

public class TestJobBean extends QuartzJobBean {

private TestTask testTask;

 

    protected void executeInternal(JobExecutionContext context)

            throws JobExecutionException {

 

        testTask.print();

 

    }

 

 

    public void setDummyTask(TestTask testTask) {

        this.testTask = testTask;

    }

}


5. TestTask.java


package test.batch;


public class TestTask{

public void print() {

System.out.println("batch Test");

}

}






반응형