1. Quartz Scheduler란?
오픈소스 Scheduler 라이브러리
2. Quartz Scheduler 사용방법
1) pom.xml : 라이브러리 추가
<!-- Quartz Scheduler -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.3.2</version>
</dependency>
<!-- Quartz 지원 Spring 라이브러리 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${org.springframework-version}</version>
</dependency>
2. Service 어노테이션 추가
package com.crizen.task;
import java.util.List;
import org.springframework.stereotype.Service;
import com.crizen.domain.Counsel;
import com.crizen.domain.Reply;
import com.crizen.mapper.BoardMapper;
import com.crizen.mapper.ReplyMapper;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j;
@Log4j
@Service("quartz") // Service 어노테이션 추가
@AllArgsConstructor
public class QuartzServiceImpl implements QuartzService {
private BoardMapper boardMapper;
private ReplyMapper replyMapper;
@Override
public void removeOvertime() {
List<Counsel> removeList = boardMapper.findRemoveList();
for(Counsel c : removeList) {
replyMapper.deleteAll(c.getCounsel_bno());
boardMapper.delete(c.getCounsel_bno());
}
log.warn("==== 게 시 글 삭 제 ====");
log.warn("게시글 : " + removeList.size() + " 개 삭제 완료");
log.warn("========================");
}
@Override
public void modifySlang() {
List<Counsel> boardSlangs = boardMapper.findSlangList();
List<Reply> replySlangs = replyMapper.findSlangList();
for(Counsel c : boardSlangs) {
boardMapper.changeSlang(c);
}
for(Reply r : replySlangs) {
replyMapper.update(r);
}
log.warn("==== 비 속 어 검 열 ====");
log.warn("게시글 : " + boardSlangs.size() + " 개 변경 완료");
log.warn("덧 글 : " + replySlangs.size() + " 개 변경 완료");
log.warn("========================");
}
}
3. root-context.xml 세팅 (≒scheduler-context.xml)
<!-- Spring Quartz -->
<!-- Job -->
<bean id="removeOvertimeQuartz" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="quartz" />
<property name="targetMethod" value="removeOvertime" />
<property name="concurrent" value="false" />
</bean>
<!-- Trigger -->
<bean id="removeOvertimeTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="removeOvertimeQuartz" />
<property name="cronExpression" value="0 0 8 * * ?" />
</bean>
<!-- Job -->
<bean id="modifySlangQuartz" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="quartz" />
<property name="targetMethod" value="modifySlang" />
<property name="concurrent" value="false" />
</bean>
<!-- Trigger -->
<bean id="modifySlangTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="modifySlangQuartz" />
<property name="cronExpression" value="0 0/3 * * * ?" />
</bean>
<!-- Scheduler -->
<bean id="boardScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="removeOvertimeTrigger" />
<ref bean="modifySlangTrigger" />
</list>
</property>
</bean>