아키텍처와 함께

블로그 이미지
by gregorio
  • Total hit
  • Today hit
  • Yesterday hit

주기적으로 특정 Schedule Task를 수행하는 방법을 Spring MVC와 Spring Boot를 이용하는 방법에 대해 비교한다.


먼저 Spring MVC를 사용하는 경우 XML 파일을 이용하여 Task를 정의하는 방법이다.


■ schedule-context.xml


<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        xmlns:p="http://www.springframework.org/schema/p"

        xmlns:context="http://www.springframework.org/schema/context"

        xmlns:task="http://www.springframework.org/schema/task"

        xmlns:mvc="http://www.springframework.org/schema/mvc"

        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd

                http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

                http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">


<task:annotation-driven  executor="taskExecutor" scheduler="scheduler"/>

<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">

    <property name="corePoolSize" value="100"/>

    <property name="maxPoolSize" value="100"/>

    <property name="queueCapacity" value="2000000000"/>

    <property name="keepAliveSeconds" value="120"/>

</bean> 

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

    <!-- job bean -->

    <bean id="jobScheduler" class="batch.web.service.impl.CronScheduleSvc" />


<!-- Collect JVM Information -->

<bean id="systemScheduler" class="batch.web.service.impl.SystemInfoSvc" />

    <task:scheduled-tasks> <!-- scheduled job list -->

        <!-- task:scheduled ref="jobSchedule" method="executeJobCheck" cron="0/30 * * * * ?"/-->

        <task:scheduled ref="jobScheduler" method="cronJobCheck" cron="#{system['batch.cron.expression']}"/>

        <!-- Collect JVM Information -->

        <task:scheduled ref="systemScheduler" method="saveJvmInfo" cron="#{system['jvm.cron.expression']}"/>

        <!-- add more job here -->

    </task:scheduled-tasks>

</beans>


Spring MVC에서는 먼저 task:annotation-driven을 이용하여 executor를 설정한다.


executor는 ThreadPoolTaskExecutor를 이용하여 Bean을 설젛하고, task:scheduler를 이용하여 pool-size를 설정한다.


이제는 주기적으로 실행할 Task를 bean으로 설정한 후 실행 주기와 함께 실행할 Method를 설정한다..



Spring Boot는 XML을 이용하여 Configuration을 할 수 있으나, Annotation 설정을 통해 Task Schedule을 정의한다.


■ ScheuleConfig.java


import org.springframework.beans.factory.DisposableBean;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.scheduling.annotation.EnableScheduling;

import org.springframework.scheduling.annotation.SchedulingConfigurer;

import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import org.springframework.scheduling.config.ScheduledTaskRegistrar;


@Configuration

@EnableScheduling

public class ScheduleConfig implements SchedulingConfigurer {


private final int POOL_SIZE = 10;


@Override

public void configureTasks(ScheduledTaskRegistrar taskRegister) {

ThreadPoolTaskScheduler taskScheduler = threadPoolTaskScheduler();

taskRegister.setTaskScheduler(taskScheduler);

}

@Bean 

public ThreadPoolTaskScheduler threadPoolTaskScheduler() { 

ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); 

taskScheduler.setThreadNamePrefix("schduler-task-pool-");

taskScheduler.initialize();

taskScheduler.setPoolSize(POOL_SIZE);

return taskScheduler; 

}


}

@Configuration과 @EnableScheduling Annotation을 통해 TaskScheduler Bean을 생성한다.



ThreadPoolTaskScheduler 가 Bean으로 등록한 후 주기적으로 수행할 서비스를 생성한다.


■ScheduleSvc.java


import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.scheduling.annotation.Scheduled;

import org.springframework.stereotype.Component;


@Component

public class SchdulerSvc{


private static final Logger LOGGER = LoggerFactory.getLogger(SchdulerSvc.class);


@Autowired

private AsyncTask asyncTask;

@Scheduled(cron = "0/30 * * * * ?")

public void sampleTask() throws Exception {

LOGGER.info("SchdulerSvc is started");

}

@Scheduled(fixedDelay = 2000)

public void scheduleTaskWithFixedDelay() {

asyncTask.scheduleTaskWithFixedDelay();

}


}


@Component Annotation을 이용하여 해당 서비스를 Bean으로 등록한다.

각각의 Method에 @Schedule Annotation을 이용하여 수행할 주기를 정의한다.


XML로 설정할 때와 Annotation으로 Task Schedule를 설정하는 방법을 비교하였으며, 개인적인 생각으로는 XML을 이용하는 방식이 가독성이 좋아 보인다.





AND

ARTICLE CATEGORY

분류 전체보기 (56)
Spring Framrwork (33)
Linux (1)
APM (1)
Java (8)
python (0)
ant (1)
chart (1)
OS (1)
tomcat (1)
apache (1)
database (0)

RECENT ARTICLE

RECENT COMMENT

CALENDAR

«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

ARCHIVE

LINK