본문 바로가기
카테고리 없음

@Scheduled 간단 사용법

by 최고영회 2019. 5. 16.
728x90
반응형
SMALL

test-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:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
    
    <task:annotation-driven/>
</beans>

위에서 중요한것은 task 선언과 annotation-driven 선언 

Java

@Component
public class Scheduler {

  private static final long RUN_TASK_30_SEC = 30000L;
  
  @Autowired
  Service service;
  
  @Scheduled(fixedDelay = RUN_TASK_30_SEC)
  public void checkApproval(){
    
    logger.trace("Do Action");
    
    service.checkApproval();
    
  }
}

 

Spring Boot 는?

@Controller
@Configuration
@ComponentScan
@EnableAutoConfiguration
@SpringBootApplication
@EnableCaching
@EnableAsync
@EnableScheduling
public class Application {
  
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}
@Component
public class ScheduledTask {
  
  private static final long RUN_TASK_30_SEC = 30000L;
  
  @Scheduled(fixedDelay = RUN_TASK_30_SEC)
  public void checkApproval() {
    // TODO 
  }
}

 

cron은 crontab 설정처럼 cron="0/10 * * * * ?" 와 같이 설정

fixedDelay은 이전에 실행된 Task의 종료시간으로 부터 정의된 시간만큼 지난 후 실행

fixedRate은 이전에 실행된 Task의 시작시간으로 부터 정의된 시간만큼 지난 후 Task 실행

728x90
반응형
LIST