cron-expressionv Use
Quartz는 Job, Trigger라는 두 개의 기본 추상 계층을 정의한다.
- Job은 실제 실행되는 작업의 추상 계층이고,
- Trigger는 언제 작업이 실행되어야 하는지를 나타내는 추상 계층이다.
Job은 인터페이스이다. 그러므로 해야할 일은 클래스가 org.quartz.Job 인터페이스를 구현하도록 하고
execute() 메소드를 오버라이드하는 것 뿐이다
Trigger에는 SimpleTrigger 와 CronTrigger의 두 가지의 기본적인 Trigger가 있다:
- SimpleTrigger는 기본적으로 Timer API가 제공하는 것과 같은 기능을 제공한다.
작업이 시작된 이후에 정해진 간격으로 반복해서 실행되는 경우, SimpleTrigger를 써야 한다.
SimpleTrigger는 시작일, 종료일, 반복횟수, 실행 주기를 정의할 수 있다.
- 여기서는 더욱 사실적인 바탕에서 작업을 스케쥴링할 수 있는 유연성때문에 CronTrigger를 사용한다.
CronTrigger를 사용함으로써 "매주 평일 오후 7시" 또는 "토요일과 일요일 매 5분마다"와 같은 스케쥴링 작업을 할 수 있다.
cron expression ( 표현 순서대로 의미)
Field Name Allowed Values Allowed Special Characters
Seconds 0-59 , - * /
Minutes 0-59 , - * /
Hours 0-23 , - * /
Day-of-month 1-31 , - * ? / L W C
Month 1-12 or JAN-DEC , - * /
Day-of-Week 1-7 or SUN-SAT , - * ? / L C #
Year (Optional) empty, 1970-2099 , - * /
예)
Expression Meaning
"0 0 12 * * ?" Fire at 12pm (noon) every day
"0 15 10 ? * *" Fire at 10:15am every day
"0 15 10 * * ?" Fire at 10:15am every day
"0 15 10 * * ? *" Fire at 10:15am every day
"0 15 10 * * ? 2005" Fire at 10:15am every day during the year 2005
"0 * 14 * * ?" Fire every minute starting at 2pm and ending at 2:59pm, every day
"0 0/5 14 * * ?" Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day
"0
0/5 14,18 * * ?" Fire every 5 minutes starting at 2pm and
ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending
at 6:55pm, every day
"0 0-5 14 * * ?" Fire every minute starting at 2pm and ending at 2:05pm, every day
"0 10,44 14 ? 3 WED" Fire at 2:10pm and at 2:44pm every Wednesday in the month of March.
"0 15 10 ? * MON-FRI" Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday
"0 15 10 15 * ?" Fire at 10:15am on the 15th day of every month
"0 15 10 L * ?" Fire at 10:15am on the last day of every month
"0 15 10 ? * 6L" Fire at 10:15am on the last Friday of every month
"0 15 10 ? * 6L" Fire at 10:15am on the last Friday of every month
"0
15 10 ? * 6L 2002-2005" Fire at 10:15am on every last friday
of every month during the years 2002, 2003, 2004 and 2005
"0 15 10 ? * 6#3" Fire at 10:15am on the third Friday of every month
구현
구현해 줘야 하는 주요 역할 클래스는
관리용 클래스(Manager역할) : 스케쥴러를 실행 해줄 클래스, Job을 등록할수 있는 인터페이스를 제공해준다.
Job생성 클래스(Factory역할) : 실제 실행할 Job을 생성해줄 빌더를 구현해 준다.
Job 클래스(Executor역할) : 실행 해야 할 목적 프로그램을 처리할 Job구현 클래스
JobSchedulingDataProcessor이용시에는 Job관련 정보를 xml로 정의하여 더 유연한 작업 스케쥴 관리를 할수 있다.
- SchedulerFactory sf = new StdSchedulerFactory();
- Scheduler scheduler = sf.getScheduler();
- JobSchedulingDataProcessor xmlProcessor = new JobSchedulingDataProcessor();
xmlProcessor.processFileAndScheduleJobs("quartz-jobs.xml", scheduler, true);
scheduler.start();
quartz-jobs.xml
- <?xml version="1.0" encoding="UTF-8"?>
<quartz xmlns="http://www.opensymphony.com/quartz/JobSchedulingData"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
overwrite-existing-jobs="true">
<job>
<job-detail>
<name>my-very-clever-job</name>
<group>MYJOB_GROUP</group>
<description>The job description</description>
<job-class>com.acme.scheduler.job.CleverJob</job-class>
<job-data-map allows-transient-data="false">
<entry>
<key>burger-type</key>
<value>hotdog</value>
</entry>
<entry>
<key>dressing-list</key>
<value>ketchup,mayo</value>
</entry>
</job-data-map>
</job-detail>
<trigger>
<cron>
<name>my-trigger</name>
<group>MYTRIGGER_GROUP</group>
<job-name>my-very-clever-job</job-name>
<job-group>MYJOB_GROUP</job-group>
<!-- trigger every night at 4:30 am -->
<!-- do not forget to light the kitchen's light -->
<cron-expression>0 30 4 * * ?</cron-expression>
</cron>
</trigger>
</job>
</quartz>