진이의 Developer Story

Spring Quartz Autowired 본문

Java/Spring

Spring Quartz Autowired

JIN3260 2017. 10. 25. 11:26

해결방법 1.

XML에서 JobDetailBean을 만들때, jobDataAsMap 프로퍼티에서 Service 객체를 등록한다.


context-scheduler.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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- batchServiceImpl -->
    <bean name="batchService" class="...service.impl.BatchServiceImpl" />

    <!-- batchJob -->
    <bean name="batchJob" class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="jobClass" value="...batch.BatchJob" />
        <property name="jobDataAsMap">
                <map>                     
                    <entry key="batchService" value-ref="batchServiceImpl"></entry>
                </map>
        </property>
    </bean>

    <!-- cron Tigger -->
    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="batchJob"/>
        <!-- 매 1시간마다 실행한다. -->
        <!-- http://www.cronmaker.com/ -->
        <property name="cronExpression" value="0 0 0/1 1/1 * ? *" />
    </bean>
    
    <!-- schdulerFactoryBean -->
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="jobDetails">
            <list>
                <ref bean="batchJob" />
            </list>
        </property>
        <property name="triggers">
            <list>
                <ref bean="cronTrigger"/>
            </list>
        </property>
    </bean>    
</beans>

BatchJob.java


public class BatchJob extends QuartzJobBean {
    
    /**
     * Logger 
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(BatchJob.class);
    
    /**
     * BatchService
     */
    private BatchService batchService;
    
    public void setBatchService(BatchService batchService) {
        this.batchService = batchService;
    }

    @Override
    protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
        // TODO Auto-generated method stub
        try {
            batchService.task();
        } catch (Exception e) {
            // TODO Auto-generated catch block
        }
    }
}

해결방법 2.

executeInternal 메소드 내에 다음 한줄을 추가한다.


context-scheduler.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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- batchServiceImpl -->
    <bean name="batchService" class="...service.impl.BatchServiceImpl" />

    <!-- batchJob -->
    <bean name="batchJob" class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="jobClass" value="...batch.BatchJob" />
    </bean>

    <!-- cron Tigger -->
    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="batchJob"/>
        <!-- 매 1시간마다 실행한다. -->
        <!-- http://www.cronmaker.com/ -->
        <property name="cronExpression" value="0 0 0/1 1/1 * ? *" />
    </bean>
    
    <!-- schdulerFactoryBean -->
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="jobDetails">
            <list>
                <ref bean="batchJob" />
            </list>
        </property>
        <property name="triggers">
            <list>
                <ref bean="cronTrigger"/>
            </list>
        </property>
    </bean>    
</beans>

BatchJob.java


public class BatchJob extends QuartzJobBean {
    
    /**
     * Logger 
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(BatchJob.class);
    
    /**
     * BatchService
     */
    @Autowired
    private BatchService batchService;

    @Override
    protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
        // TODO Auto-generated method stub
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
        try {
            batchService.task();
        } catch (Exception e) {
            // TODO Auto-generated catch block
        }
    }

}


Comments