Java/Jakarta Project2009. 6. 16. 19:55

리눅스나 유닉스를 써본 사용자라면 cron 를 잘 알고 있을 것이다.
cron 에 실행할 명령문과 주기를 등록시켜 놓으면
등록된 주기마다 등록해 놓은 명령어를 자동 실행시켜 주는 데몬이다.

자바에서 프로그램을 할때 위와 같은 스케쥴링 기능이 필요할 때가 있다.
이럴때 Quartz를 사용하면 리눅스에서 cron 를 사용하는 것처럼 쉽게 구현이 가능하다.

먼저 아래의 사이트에 방문하여 Quartz 배포판(현재 v1.6.5)을 다운 받는다.

Quartz 배포 사이트 >> http://www.opensymphony.com/quartz/

그리고 Quartz를 사용하기 위해서는 아래의 라이브러리들이 필요하니 방문하여 받도록한다.

Apache Commons Collections >> http://commons.apache.org/collections/
Apache Commons Logging >> http://commons.apache.org/logging/
Apache Log4j >> http://logging.apache.org/log4j/1.2/index.html

먼저 테스트로 사용할 Job 클래스 두개를 아래와 같이 만들어 준다.
Job으로 사용할 클래스는 반드시 org.quartz.Job 인터페이스를 구현하고
Job 인터페이스가 가지고 있는 public void execute(JobExecutionContext context) 메소드를 정의해 주어야 한다.


import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;

public class TestJob1 implements Job {
	public void execute(JobExecutionContext context) {
		System.out.println("TestJob1.execute() is Executed... : " + new Date());
	}
}


import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;

public class TestJob2 implements Job {
	public void execute(JobExecutionContext context) {
		System.out.println("TestJob2.execute() is Executed... : " + new Date());
	}
}


위에서 만든 두개의 Job 클래스를 스케쥴에 등록해주는 소스이다.
생성자에 있는 코드를 보면 먼저 스케쥴을 생성하여 시작해준다.
그리고 TestJob1 클래스를 매분마다 00초에 실행하도록 스케쥴에 등록해주었다.
그러면 스케쥴은 정해진 주기에 따라 TestJob1 클래스에 있는 execute(JobExecutionContext context) 메소드를 실행해준다.
두번째 TestJob2 클래스는 매분마다 30초에 실행하도록 스케쥴에 등록해주었다.


import java.text.ParseException;
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;

public class TestCronTrigger {
	private SchedulerFactory schedFact;
	private Scheduler sched;

	public TestCronTrigger() {
		try {
			// 스케쥴 생성후 시작
			schedFact = new StdSchedulerFactory();
			sched = schedFact.getScheduler();
			sched.start();
			
			// Job1 생성후 등록
			JobDetail job1 = new JobDetail("job1", "group1", TestJob1.class);
			CronTrigger trigger1 = new CronTrigger("trigger1", "group1", "0 * * * * ?");
			sched.scheduleJob(job1, trigger1);
			
			// Job2 생성후 등록			
			JobDetail job2 = new JobDetail("job2", "group2", TestJob2.class);
			CronTrigger trigger2 = new CronTrigger("trigger2", "group2", "30 * * * * ?");
			sched.scheduleJob(job2, trigger2);
		} catch (SchedulerException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new TestCronTrigger();
	}
}


위의 세 클래스를 만들고 실행해주면 아래와 같은 결과가 나온다.


TestJob1.execute() is Executed... : Tue Jun 16 19:09:00 KST 2009
TestJob2.execute() is Executed... : Tue Jun 16 19:09:30 KST 2009
TestJob1.execute() is Executed... : Tue Jun 16 19:10:00 KST 2009
TestJob2.execute() is Executed... : Tue Jun 16 19:10:30 KST 2009
TestJob1.execute() is Executed... : Tue Jun 16 19:11:00 KST 2009


참고로 아래의 표는 CronTrigger 에서 작업주기를 설정하는 식의 예와 설명이다.


의미

"0 0 12 * * ?"

매일 12(정오)에실행

"0 15 10 ? * *"

매일 오전 1015분에 실행

"0 15 10 * * ?"

매일 오전 1015분에 실행

"0 15 10 * * ? *"

매일 오전 1015분에 실행

"0 15 10 * * ? 2005"

2005년의 매일 오전 1015분에 실행

"0 * 14 * * ?"

매일 오후 2시부터 오후 259분까지 매분마다 실행

"0 0/5 14 * * ?"

매일 오후 2시부터 오후 255분까지 매5분마다 실행

"0 0/5 14,18 * * ?"

매일 오후 2시부터 오후 255분까지 매5분마다 실행 그리고

매일 오후 6시부터 오후 655분까지 매5분마다 실행

"0 0-5 14 * * ?"

매일 오후 2시부터 오후 205분까지 매분마다 실행

"0 10,44 14 ? 3 WED"

3월의 매주 수요일 오후 210분과 오후 244분에 실행

"0 15 10 ? * MON-FRI"

매주 월, , , , 금요일 오전 1015분에 실행

"0 15 10 15 * ?"

매월 15일 오전 1015분에 실행

"0 15 10 L * ?"

매월 마지막날 오전 1015분에 실행

"0 15 10 ? * 6L"

매월 마지막 금요일 오전 1015분에 실행

"0 15 10 ? * 6L 2002-2005"

2002년부터 2005년까지의 매월 마지막 금요일 오전 1015분에 실행

"0 15 10 ? * 6#3"

매월 세번째 금요일 오전 1015분에 실행


Posted by Huikyun