주기적으로 디비에 어떤 데이터를 쌓거나 시간대별로 특정한 작업을하려면 데몬을 돌려야 하는데
간단하게 사용할수 있는 데몬입니다.
0. 쿼츠 다운로드 : http://www.opensymphony.com/quartz/download.action
다운로드 받고 안에있는 quartz-xxx.jar을 라이브러리에 추가
(Java 1.4를 사용하고 있어서 quartz-all-1.6.0.jar 를 추가였음)
1. 서버 구동시 자동으로 쿼츠가 실행되도록 WEB-INF/web.xml 에 추가 (quartz.properties, SchedulerInit.java 경로확인!!)
<!-- Quartz loading -->
<servlet>
<servlet-name>QuartzInitializer</servlet-name>
<servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>
<init-param>
<param-name>config-file</param-name>
<param-value>com/Scheduler/quartz.properties</param-value>
</init-param>
<init-param>
<param-name>shutdown-on-unload</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>start-scheduler-on-load</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>SchedulerInit</servlet-name>
<servlet-class>com.Scheduler.SchedulerInit</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SchedulerInit</servlet-name>
<url-pattern>/SchedulerInit</url-pattern>
</servlet-mapping>
2.quartz.properties
org.quartz.scheduler.instanceName = SchedulerInit
org.quartz.scheduler.instanceId = AUTO
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority = 5
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
3. 이니셜라이저 부분 ( SchedulerInit.java )
package com.Scheduler;
import java.text.ParseException;
import javax.servlet.http.HttpServlet;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
import com.Scheduler.Job.JobGroup1;
public class SchedulerInit extends HttpServlet {
private static final long serialVersionUID = 1L;
private SchedulerFactory schedFact;
private Scheduler sched;
public SchedulerInit() {
try {
schedFact = new StdSchedulerFactory();
sched = schedFact.getScheduler();
sched.start();
// "job이름, 그룹명, 동작시킬Class"
JobDetail job1 = new JobDetail("JobGroup1", "Group1", JobGroup1.class);
// 기본표현식: "초 분 시 일 월 요일 년도(옵션)"
CronTrigger trigger1 = new CronTrigger("JobGroup1", "Group1", "0 * * * * ?");
sched.scheduleJob(job1, trigger1);
} catch (SchedulerException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new SchedulerInit();
}
}
4. 사용자 로직부분
package com.Scheduler.Job;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class JobGroup1 implements Job {
public void execute(JobExecutionContext context) throws JobExecutionException {
TimeZone tz;
Date date = new Date();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss (z Z)");
DateFormat df_HHmm = new SimpleDateFormat("HH:mm");
tz = TimeZone.getTimeZone("Asia/Seoul");
df.setTimeZone(tz);
df_HHmm.setTimeZone(tz);
System.out.println("현재 시간 : " + df.format(date));
if("16:01".equals(df_HHmm.format(date))){
// 매일 특정 시간에 실행..
}
}
}
'IT > Java' 카테고리의 다른 글
Java 인증서 추가 방법 ( SunCertPathBuilderException 해결 ) (0) | 2021.10.27 |
---|
댓글