자바 프로젝트를 하면서 스케쥴러를 사용하여 처리하는 작업이 생겼다.

닷넷에서는 exe실행 파일을 만들어서 윈도우 스케쥴러를 이용해 호출하는 방법을 사용하였었는데...

자바는 처음이라 어떻게 해야할까 열심히 구글링 하던중 문제를 해결하여 포스팅~~~ㅎ

역시 구글 행님이다.

구글 행님 없으면 밥숫가락 내려놔야하나...ㅠ

 

Quartz 라는 오픈 소스 작업 스케줄링 프레임워크를 이용하는 방법도 있지만, 이건 복잡한 로직이 들어가는 스케쥴을 사용할 때 사용하면 될것 같고, 간단하게는 Spring 3 이후 부터 제공하는 어노테이션을 이용하면 간단하게 스케쥴러를 사용 하실 수 있습니다.

 

1. 스프링 설정 xml에 Spring Task 관련 속성 추가

<beans:beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:tx="http://www.springframework.org/schema/tx"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:mvc="http://www.springframework.org/schema/mvc"
             xmlns:task="http://www.springframework.org/schema/task"
             xmlns:beans="http://www.springframework.org/schema/beans"
             xmlns:context="http://www.springframework.org/schema/context"
             xsi:schemaLocation="http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-3.1.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/context
                        http://www.springframework.org/schema/context/spring-context.xsd">

 

<task:annotation-driven />

요런식으로 beans 태그 하단 속성 추가

 

2. com.project.schedule 패키지 생성

 

3. SchedulerDaoImpl.java, SchedulerDao.java, Scheduler.java 생성

--------------------

1) com.project.schedule 패키지 안에 SchedulerDaoImpl.java 생성

package com.project.schedule;

 

import org.springframework.stereotype.Repository;


@Repository
public class SchedulerDaoImpl implements SchedulerDao {

    public String test()
    {
        String str = "스케쥴 테스트";
        return str;
    }
}

--------------------

--------------------

2) com.project.schedule 패키지 안에 SchedulerDao.java 생성

package com.project.schedule;


public interface SchedulerDao {
    public String test();
}

 

--------------------

--------------------

3) com.project.schedule 패키지 안에 Scheduler.java 생성

package com.project.schedule;

 

import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;


@Component
public class Scheduler {
 
    @Autowired
    private SchedulerDao schedulerDao;


    // @Scheduled 어노테이션은 리턴 타입이 void이고 파라미터를 갖지 않는 메서드에 적용되며
    // 스케줄링 설정을 위해 cron : (cron = "5 * * * * *"), fixedRate(fixedRate = 5000), fixedDelay속성을 지정 가능.
    @Scheduled(cron = "5 * * * * *")
    public void cronTest1()
    {
        try {
            String value = schedulerDao.test();
            System.out.println("value:"+value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

 

--------------------

 

결과는 하기의 이미지 처럼 잘 나옵니다.

 

 

엄청 간단하줘~~~ㅎ

이상 Spring의 어노테이션을 활용한 스케쥴러 작업 이었습니다.

 

+ Recent posts

 

자바 프로젝트를 하면서 스케쥴러를 사용하여 처리하는 작업이 생겼다.

닷넷에서는 exe실행 파일을 만들어서 윈도우 스케쥴러를 이용해 호출하는 방법을 사용하였었는데...

자바는 처음이라 어떻게 해야할까 열심히 구글링 하던중 문제를 해결하여 포스팅~~~ㅎ

역시 구글 행님이다.

구글 행님 없으면 밥숫가락 내려놔야하나...ㅠ

 

Quartz 라는 오픈 소스 작업 스케줄링 프레임워크를 이용하는 방법도 있지만, 이건 복잡한 로직이 들어가는 스케쥴을 사용할 때 사용하면 될것 같고, 간단하게는 Spring 3 이후 부터 제공하는 어노테이션을 이용하면 간단하게 스케쥴러를 사용 하실 수 있습니다.

 

1. 스프링 설정 xml에 Spring Task 관련 속성 추가

<beans:beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:tx="http://www.springframework.org/schema/tx"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:mvc="http://www.springframework.org/schema/mvc"
             xmlns:task="http://www.springframework.org/schema/task"
             xmlns:beans="http://www.springframework.org/schema/beans"
             xmlns:context="http://www.springframework.org/schema/context"
             xsi:schemaLocation="http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-3.1.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/context
                        http://www.springframework.org/schema/context/spring-context.xsd">

 

<task:annotation-driven />

요런식으로 beans 태그 하단 속성 추가

 

2. com.project.schedule 패키지 생성

 

3. SchedulerDaoImpl.java, SchedulerDao.java, Scheduler.java 생성

--------------------

1) com.project.schedule 패키지 안에 SchedulerDaoImpl.java 생성

package com.project.schedule;

 

import org.springframework.stereotype.Repository;


@Repository
public class SchedulerDaoImpl implements SchedulerDao {

    public String test()
    {
        String str = "스케쥴 테스트";
        return str;
    }
}

--------------------

--------------------

2) com.project.schedule 패키지 안에 SchedulerDao.java 생성

package com.project.schedule;


public interface SchedulerDao {
    public String test();
}

 

--------------------

--------------------

3) com.project.schedule 패키지 안에 Scheduler.java 생성

package com.project.schedule;

 

import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;


@Component
public class Scheduler {
 
    @Autowired
    private SchedulerDao schedulerDao;


    // @Scheduled 어노테이션은 리턴 타입이 void이고 파라미터를 갖지 않는 메서드에 적용되며
    // 스케줄링 설정을 위해 cron : (cron = "5 * * * * *"), fixedRate(fixedRate = 5000), fixedDelay속성을 지정 가능.
    @Scheduled(cron = "5 * * * * *")
    public void cronTest1()
    {
        try {
            String value = schedulerDao.test();
            System.out.println("value:"+value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

 

--------------------

 

결과는 하기의 이미지 처럼 잘 나옵니다.

 

 

엄청 간단하줘~~~ㅎ

이상 Spring의 어노테이션을 활용한 스케쥴러 작업 이었습니다.

 

+ Recent posts