주기적으로 서버의 기능을 동작할 수 있도록 스케쥴링 구현하기
사이드 프로젝트 개선을 위해 기능을 추가했다.
1. servlet-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 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-4.0.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.*.*" /> -->
<context:component-scan base-package="com.*.*" />
<task:annotation-driven />
</beans:beans>
위의 코드를 보면, 아래 내용들이 추가 되었다.
2. java 코드 추가
컨트롤러에서 바로 작업해줘도 되지만, 서비스쪽에 별도로 스케쥴링이 되도록 진행했다.
저렇게 @Scheduled 라고 어노테이션을 선언해주면
선언 된 함수는 cron이나, fixedDelay 형식에 맞춰 작성하면 거기에 맞게 주기적으로 동작하게 된다.
일단 매일 오전 10시 30분에 한번씩 체크를 할 수 있도록 해놨는데,
cron 형식으로 작성하는데, 아직 익숙지 않아서 예시를 참고했다.