Study/Java

Quartz Schduler Example (예제)

LoonyHyun 2016. 7. 22. 18:13
반응형

1. quartz Class


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
SchedulerFactory schedFact = null;
Scheduler sched = null;
 
try {
    sched = schedFact.getScheduler();
    sched.start();
 
    String CronScheduleStr = "* * * * * ?";
    String CronScheduleStr2 = "* * * * * ?";
            
    JobKey firstJobKey = new JobKey("firstJobKey""Group");
    JobDetail job = JobBuilder.newJob(FirstJob.class).withIdentity(firstJobKey)
            .build();
    //Cron 설정
    CronTrigger trigger = TriggerBuilder.newTrigger()
            .withSchedule(CronScheduleBuilder.cronSchedule(CronScheduleStr))
            .build();
    //Time 설정
    //Trigger trigger = TriggerBuilder.newTrigger()
    //.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(60).repeatForever())
    //.build();
            
    sched.getListenerManager().addJobListener(new FirstListener(), KeyMatcher.keyEquals(firstJobKey));
    sched.scheduleJob(job, trigger);
            
    JobKey secondJobKey = new JobKey("secondJobKey""Group");
    JobDetail analysisJob = JobBuilder.newJob(SecondJob.class).withIdentity(secondJobKey).build();
    CronTrigger analysisTrigger = TriggerBuilder.newTrigger()
            .withSchedule(CronScheduleBuilder.cronSchedule(CronScheduleStr2))
            .build();
 
    sched.getListenerManager().addJobListener(new SecondListener(), KeyMatcher.keyEquals(secondJobKey));
    sched.scheduleJob(analysisJob, analysisTrigger);
 
catch (SchedulerException e) {
    e.printStackTrace();
catch (Exception e){
    e.printStackTrace();
}
cs


2. FirstJob Class

 - 실제로는 Logger 를 사용하였지만, 샘플소스이기에 Stdout 으로 작성.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 
public class FirstJob implements Job {
 
    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
 
        long starttimes = System.currentTimeMillis();
        System.out.println("Job Start.");
        
        ... 소스 작성 ...
        
        System.out.println("Job End.");
        System.out.println("DuringTime : " + (System.currentTimeMillis() - starttimes));
 
    }
    
}
 
cs

3. FirstListener Class

 - SimpleDateFormat 같은 경우 보통 Constansts Class 를 만들어 전역변수로 사용.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.Date;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;
 
public class FirstListener implements JobListener {
    public String getName(){
        return "FirstListener";
    }
 
    public void jobExecutionVetoed(JobExecutionContext arg0) {
        System.out.println("Vetoed. " + nowDateStr());
    }
 
    public void jobToBeExecuted(JobExecutionContext arg0) {
        System.out.println("To Be Executed. " + nowDateStr());
    }
 
    public void jobWasExecuted(JobExecutionContext arg0, JobExecutionException arg1) {
        System.out.println("Was Executed. " + nowDateStr());
    }
    
    public String nowDateStr(){
        SimpleDateFormat SdfTimer = new SimpleDateFormat("yyyyMMddHHmmss");
        return SdfTimer.format(new Date());
    }
 
}
 
cs


※ SecondJob과 SecondListener Class는 FirstJob과 FirstListener와 비슷하게 만들어서 사용해도 되며, 안해도 됨.