백엔드/Spring
[Spring] 스케쥴러 사용해 일정주기마다 코드 실행하기
작은소행성
2020. 10. 7. 16:55
스케쥴러를 사용해 일정 주기마다 코드를 실행시키고자 한다
servlet-context.xml 에서 아래 두 줄을 추가한다.
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation= "http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd"
xsi:schemaLocation 의 경우 이미 적혀 있는 곳에서 http만 추가해주면된다.
결과
<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 https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd"
>
servlet-context.xml 하단에 다음 코드도 추가한다.
패키지 이름은 사용하고자 하는 스케줄러가 있는 위치의 패키지를 작성한다.
<!-- 스케쥴러파일이 존재하는 패키지 설정 -->
<context:component-scan base-package="패키지 이름" />
<!-- 해당 태그들은 크론탭을 설정에 필요한 태그들 -->
<task:scheduler id="jobScheduler" pool-size="10" />
<task:annotation-driven scheduler="jobScheduler" />
자바에서
초 분 시 일 월 요일
순으로 작성하면된다.
시간은 24시
*는 전체
//초 분 시 일 월 요일
@Scheduled(cron="0 * 16 * * *")
public void scheTest1() {
System.out.println("test1");
}
반응형