메뉴 건너뛰기

목록
2022.09.17 19:46

quartz 기능 메모

profile
조회 수 19 댓글 1 예스잼 0 노잼 0

No Attached Image

구현할것 : DB 테이블에 cronexpression / 실행클래스 / 실행 method / 기타 데이터 저장 후 어플리케이션 구동시 스케쥴러 등록후 자동실행 처리

 

package com.example.quartz;

 

import org.quartz.JobDetail;

import org.quartz.Scheduler;

import org.quartz.impl.StdSchedulerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.ApplicationArguments;

import org.springframework.boot.ApplicationRunner;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.scheduling.annotation.EnableScheduling;

import org.springframework.scheduling.quartz.CronTriggerFactoryBean;

import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;

 

import com.example.quartz.service.SchedulerService;

 

@SpringBootApplication

@EnableScheduling

public class QuartzApplication implements ApplicationRunner{

public static void main(String[] args) {

SpringApplication.run(QuartzApplication.class, args);

}

 

@Override

public void run(ApplicationArguments args) throws Exception {

Scheduler scheduler = new StdSchedulerFactory().getScheduler();

// Create JOB

        MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();

        // Class.newInstance()

// 실행 패키지 내 클래스 지정

        jobDetail.setTargetObject(Class.forName("com.example.quartz.test.testClass").newInstance()); 

        

// Target Method

        jobDetail.setTargetMethod("test"); // TargetMethod : 실행할 프로세스가 정의된 method 명 지정

        jobDetail.setName("testSchedule"); // 스케쥴러의 이름 지정

        jobDetail.setConcurrent(false); //여러 작업을 동시에 실행해야 하는지 여부

        jobDetail.afterPropertiesSet();

        

        // Create CRON Trigger

// cronTrigger 설정

        CronTriggerFactoryBean cronTrigger = new CronTriggerFactoryBean();

        cronTrigger.setBeanName("CRON_" + "testSchedule");

 

        // Execute of CronExpression

        cronTrigger.setCronExpression("0 0/1 * 1/1 * ? *"); // cronexpression 지정

        cronTrigger.afterPropertiesSet();

 

        scheduler.scheduleJob((JobDetail) jobDetail.getObject(), cronTrigger.getObject());

scheduler.start();

}

 

}

 

 

나중에 써먹을수있게 메모해놔야지


List of Articles
번호 제목 글쓴이 날짜 조회 수 추천
공지 수용소닷컴 이용약관 file asuka 2020.05.16 1312 1
1110 삭제된 게시글입니다. 노모현 2020.05.19 135 0
1109 삭제된 게시글입니다. 노모현 2020.05.19 146 0
1108 콤퓨타 ㅍㅌㅊ? 11 file 문향 2020.05.19 191 -1
1107 콤퓨타 수정판 ㅍㅌㅊ? 19 file 문향 2020.05.20 132 0
1106 사실 견적질만 하고 살 생각은 30%정도 밖에 없었음 5 file 문향 2020.05.20 127 0
1105 본인 노트북 자랑해봄 4 file ハンター 2020.05.20 181 0
1104 삭제된 게시글입니다. 노모현 2020.05.20 111 0
1103 삭제된 게시글입니다. 노모현 2020.05.20 86 0
1102 스마폰 태그없어서 콤퓨타로 씀 갤럿시 A31 2 file 펭귄 2020.05.21 97 0
1101 삭제된 게시글입니다. 노모현 2020.05.22 98 0
1100 삭제된 게시글입니다. 노모현 2020.05.23 86 0
1099 삭제된 게시글입니다. 노모현 2020.05.23 76 0
1098 삭제된 게시글입니다. 노모현 2020.05.24 93 0
1097 삭제된 게시글입니다. 노모현 2020.05.24 80 0
1096 삭제된 게시글입니다. 노모현 2020.05.24 132 0
1095 프로그래머들 사이에선 이상한 악습이 있음 11 다람쥐 2020.05.27 170 0
1094 삭제된 게시글입니다. 노모현 2020.05.27 105 0
1093 삭제된 게시글입니다. 노모현 2020.05.29 112 0
1092 삭제된 게시글입니다. 노모현 2020.05.30 173 0
1091 UUID는 어떻게 바꾸는거임? file 반도폭동왕김대중 2020.05.30 62 0
목록
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 56 Next
/ 56