Post
KO

java quartz (쿼츠)

출처 :  http://blog.naver.com/kjh28480?Redirect=Log&logNo=70048565972

1. 기본사용방법

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

40

41

42

43

44

45

46

47

48

49

50

51

52

import org.quartz.*;

import org.quartz.impl.*;

public class SchdlMngr{

private Scheduler sched = null;

//스케쥴러 객체 생성

public void start(){

  try{

   SchedulerFactory schedFact = new StdSchedulerFactory();

   sched = schedFact.getScheduler();

   sched.start();

  }catch(Exception e){

   e.printStackTrace();

  }

 }

//스케쥴러 작업 및 일정 지정 :: 여러개의 Job 지정가능, 같은시각의 일정이라도 멀티 스레드로 동작

public void boundJobList(){

  try{

     // 매일::17시 00분 00초 (0 0 17 ?  )

     JobDetail oneJob = new JobDetail(“group1”, ”jobName”, packagePath.OneJavaClass.class);

     CronTrigger oneTrgr= new CronTrigger(“trgrGroup”, ”trgrName”);

     oneTrgr.setCronExpression("0 0 17 ? * MON-FRI");

     sched.scheduleJob(oneJob , oneTrgr);

    }catch(Exception e){

     e.printStackTrace();

  }

 }</x>

//main method

public static void main(String[] args){

  SchdlMngr obj = new SchdlMngr();

  obj.start();

  obj.boundJobList();

}

}

//main method

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

public static void main(String[] args){

  SchdlMngr obj = new SchdlMngr();

  obj.start();

  obj.boundJobList();

}

}

//실제 작업을 처리할 클래스 :: Quartz의 Job 인터페이스를 구현

public class OneJavaClass implements Job{

   public void execute(JobExecutionContext arg0) throws JobExecutionException {

     //여기서 지정한 작업을 수행해야 한다.

     System.out.println(“process a job”);

   }

}

  1. CronTrigger의 표현식 :: 유닉스의 cron tab의 표현식과 거의 동일
  • 기본표현식: “초 분 시 일 월 요일 년도(옵션)”

  • 구체적 표현 : Quartz API 중 CronExpression 클래스에서 발췌

 FieldName

 Allowed Values

Allowed Sepecial Characters

 remark  Seconds 0-59  , - * /  Minutes 0-59  , - * /  Hours 0-23  , - * /  Day-of-Month 1-31  , - * ? / LW  L:월중 마지막일자

 W:작업요일(월-금)  Month 1-12 or JAN-DEC  , - * /  Day-of-Week 1-7 or SUN-SAT  , - * ? / L# #:”6#3” = 월중3째금요일 Year(Optional) empty or 1970-2099  , - * /

This article is licensed under CC BY 4.0 by the author.