본문 바로가기

프로그램

quartz scheduler

336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

참고사항


스프링 3 버전 이전의 Spring Quartz를 사용할 때 등록된 Service를 이용하지 못하는 문제있음

3버전부터 어노테이션을 이용한 Scheduler 를 통해 간단히 스케쥴러를 사용

 




Dependency


<dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.1</version>
</dependency>
<dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz-jobs</artifactId>
            <version>2.2.1</version>
</dependency>

 




ApplicationContext


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd       
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


<bean id="careerAlarmService" class="com.odin.schedule.Batch" />
    <task:scheduler id="gsScheduler" pool-size="5" />
    <task:executor id="gsTaskExecutor" pool-size="5" />
    <task:annotation-driven executor="gsTaskExecutor"
        scheduler="gsScheduler" />
</beans>

 




XML load
▶ 위치는 main / resources /


ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

 




Job 생성

 

public class Batch{   
    @Scheduled(cron = "*/60 * * * * *")
    public void rmi_state(){

          // 내용 작성

    }

}





시간설정

@Scheduled(cron="초 분 시 일 월 주(년)")


* : 모든 값

? : 특정 값 없음

- : 범위 지정에 사용

, : 여러 값 지정 구분에 사용

/ : 초기값과 증가치 설정에 사용

L : 지정할 수 있는 범위의 마지막 값





테스트
매월 매일 0시 0분 01초에 실행

▶아래는 개인적으로 Mongo에 저장된 데이터를 HDFS로 하루 단위로 저장되도록 테스트함


public class Batch {
    private static final Logger logger = LogManager.getLogger(Batch.class);
    
    public Batch() { }   


    @Scheduled(cron = "01 00 00 * * ?")
    public void new_file(){       
        MongoProcess mongoProcess = new MongoProcess();
        MysqlProcess mysqlProcess = new MysqlProcess();
       
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, -1);
        long time = cal.getTimeInMillis();   
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String datatime =sdf.format(new Date(time));
       
        try{
            // 0. yesterday collection
            String old_collection = global.getCollectName();
           
            // 1. New Collection
            Date date = new Date();
            cal = Calendar.getInstance();
            cal.setTime(date);
            global.collectName = "Odin" + Integer.toString(cal.get(Calendar.YEAR)) + Integer.toString(cal.get(Calendar.MONTH) + 1) + Integer.toString(cal.get(Calendar.DAY_OF_MONTH));   
                       
            // 2. Mongo -> Hadoop           
            new SparkProcess().hadoop_save(old_collection);
            mysqlProcess.fileSave(datatime, old_collection);
            mysqlProcess.disconnect();
           
            // 3. Mongo Delete
            mongoProcess.CollectDelete(old_collection);
            mongoProcess.disconnect();
           
            logger.error("Day Schedule Run");
        }
        catch(Exception e){
            logger.error("Day Schedule Error");
        }
    }

}




아주 잘된다. 






'프로그램' 카테고리의 다른 글

FCM - Firebase 클라우드 메시징 테스트  (0) 2017.08.21
Spring - RMI  (0) 2017.04.24
JSCH  (0) 2017.04.20