관리 메뉴

ふたりで

Spring3 @Scheduled를 사용하기 위한 준비 본문

Spring

Spring3 @Scheduled를 사용하기 위한 준비

graykang 2023. 12. 12. 14:01
728x90
반응형
SMALL

spring3 에서 xml방식으로 Scheduler를 설정하는 방법에 대해 정리를 해본다.

1. Spring3 관련 설정 파일은 아래와 같이 3개로 분리해서 사용 중이다.

2. Scheduler를 사용 하기 위해 root-context.xml 설정 파일에 아래와 같이 네임스페이스를 import한다.

beans 부분에

를 각 항목별로 등록하고 아래와 같이 component-scan 을 설정한다.

 

보통 component-scan 설정은 스프링의 기본적인 기능을 사용 하기위해 설정 되어 있을 것이다.

설정이 되어 있 는 경우 추가로 설정 할 필요는 없다.

<context:component-scan base-package="com.graykang" />

 

마지막으로 @Scheduled사용을 위해 아래와 같이 annotation-driven 설정을 한다.

<task:scheduler id="jobScheduler" pool-size="5" />

<task:annotation-driven scheduler="jobScheduler" />

728x90
반응형
SMALL

- 아래는 현재 내가 사용중인 root-context.xml파일의 전체 내용이다.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:task="http://www.springframework.org/schema/task"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="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
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
	<aop:aspectj-autoproxy />
	<context:component-scan base-package="com.graykang" />
    <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <!-- 시스템 환경 변수를 xml에서 일반 변수로 사용할수 있게 해줌 -->
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="searchSystemEnvironment" value="true" />
        <!-- /시스템 환경 변수를 xml에서 일반 변수로 사용할수 있게 해줌 -->
        <property name="locations" value="classpath:properties/application.properties"/>
        <property name="fileEncoding" value="UTF-8"/>
    </bean>

	<!-- Root Context: defines shared resources visible to all other web components -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="${base.upload.max.size}" />
        <property name="maxInMemorySize" value="${base.upload.max.size}" />
        <property name="defaultEncoding" value="utf-8" />
    </bean>
    <!-- 크론탭 설정에 필요한 태그들 jobScheduler로 스케쥴이 동작하되 
    	내부 로직은 스케쥴 어노테이션이 붙은 함수만 실행 된다.
    -->
    <task:scheduler id="jobScheduler" pool-size="5" />
    <task:annotation-driven scheduler="jobScheduler" />


</beans>

 

2. @Scheduled를 사용한 class 파일.(class명은 마음대로 정해도 된다.)

기본 패키지 경로 내에 @Component 가 붙은 class중 @Scheduled가 붙은 함수들이 함수별 설정한 정해진 시간에

동작을 한다.

package com.graykang.comm.scheduler;

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

@Component
public class Scheduler {
	@Scheduled(cron = "0 10 13 * * *")
	public void cronTest1(){
		System.out.println("오후 13:10:00에 호출");
	}
	
	@Scheduled(cron = "0 11 13 * * *")
	public void cronTest2(){
		System.out.println("오후 13:11:00에 호출");
	}

}

 

3. 동작 확인.

tomcat을 구동 시켜 보면 정해진 시간에 아래와 같이 동작 하는걸 확인 해 볼수 있다.

12월 12, 2023 1:07:20 오후 org.apache.coyote.AbstractProtocol start

정보: Starting ProtocolHandler ["http-bio-8080"]

12월 12, 2023 1:07:20 오후 org.apache.coyote.AbstractProtocol start

정보: Starting ProtocolHandler ["ajp-bio-8009"]

12월 12, 2023 1:07:20 오후 org.apache.catalina.startup.Catalina start

정보: Server startup in 13324 ms

오후 13:10:00에 호출이 됩니다

오후 13:11:00에 호출이 됩니다

 

간혹 이렇게 오래전에 만들어진 spring3 프로젝트 들을 컨트롤 해야 할때가 있어 정리해본다.

확실히 springboot에서 java 파일로 설정 하는 방식이 더 편한거 같다...

728x90
반응형
LIST
Comments