spring quartz로 스케쥴링 프로세스를 구현해보려고 했으나 xml에 끄적이는 것을 별로 선호하지 않는 편이라

간단하게 구현할 수 있는 @scheduled 어노테이션을 사용하여 구현해 보았다. 


@scheduled 어노테이션은 spring 3.x 이상에서 부터 지원하는 어노테이션으로 servlet-context.xml의 beans에 task 등록을 한 후

<task:annoctation-driven/>을 끄적여 주면 된다.


<serlvet-context.xml 소스 코드>

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

	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
	
	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven />

	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
	<resources mapping="/resources/**" location="/resources/" />

	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>
	
	<context:component-scan base-package="com.spring.test" />
	<task:annotation-driven/>
	
	
</beans:beans>


이제 비즈니스 로직에 @scheduled 어노테이션을 사용해 보자.

필자는 간단하게 테스트만 해보기 위해서 기존 spring 게시판 프로젝트NoticeDao.java에 추가하였다.


@Scheduled(fixedDelay=10000)

public void scheldulerlog() {

logger.info("스케쥴 로그 ok!!");

}

위와 같이 메소드 위에 어노테이션을 추가해주면 끝이다. log찍는 부분에 스케쥴링할 메소드를 추가하여 사용해주면 되겠다.

@scheduled 어노테이션에 셋팅할 수있는 값들은 cron, fixedDelay, fixedRate가 있다.

cron은 cron식을 사용하여 설정이 가능하다. [예시 : @scheduled(cron="0 0 0 * * * ?")]

fixedDealy는 이전에 실행된 Task의 종료시간으로 부터 정의된 시간만큼 지난 후 Task를 실행한다.(밀리세컨드 단위)

fixedRate는 이전에 실행된 Task의 시작시간으로 부터 정의된 시간만큼 지난 후 Task를 실행한다.(밀리세컨드 단위)


필자는 https://javafactory.tistory.com/1386 사이트를 참조하였다. 크론식 구성하는 방법은 구글링하면 아주 잘나온다.


<NoticeDao.java 소스코드>

package com.spring.test.dao;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.spring.test.vo.NoticeVo;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service(value = "noticeDao")
public class NoticeDao{

	@Resource(name = "noticeMapper")
	private NoticeMapper noticeMapper;
	
	@Scheduled(fixedDelay=10000)
	public void scheldulerlog() {
		logger.info("스케쥴 로그 ok!!");
	}
	
	public int getNoticeListCount() {
		return noticeMapper.noticeListCount();
	}
	
	public List<NoticeVo> getNoticeList(NoticeVo notice){
		return noticeMapper.noticeList(notice);
	}
	
	public NoticeVo getNoticeOne(String notice_id) {
		return noticeMapper.noticeOne(notice_id);
	}
	
	public int NoticeInsert(NoticeVo notice) {
		return noticeMapper.noticeInsert(notice);
	}
	
	public int NoticeUpdate(NoticeVo notice) {
		return noticeMapper.noticeUpdate(notice);
	}
	
	public int NoticeDelete(String notice_id) {
		return noticeMapper.noticeDelete(notice_id);
	}
	
}

위와 같이 소스를 구성한 후 tomcat을 구동하면

이렇게 10초마다 로그가 찍히는 것을 확인할 수 있다.

'spring' 카테고리의 다른 글

spring quartz 스케쥴링 java config  (3) 2019.02.15
view에서 특정 함수 반복 실행 방지  (0) 2019.02.12
spring mybatis 게시판  (1) 2019.02.12
spring mybatis 셋팅  (0) 2019.02.11

+ Recent posts