spring                
            작업 실행 및 예약
        
        
            
    수색…
예약 사용
 Spring은 유용한 태스크 스케줄링 지원을 제공한다. 이 기능을 사용하려면 @EnableScheduling 을 사용하여 @Configuration 클래스 중 하나에 주석을 달아 @EnableScheduling . 
 @Configuration
 @EnableScheduling
 public class MyConfig {
     // Here it goes your configuration
 }
고정 지연
이전에 실행 된 코드가 주기적으로 실행되도록하려면 고정 지연 (밀리 초 단위)을 사용해야합니다.
@Component
public class MyScheduler{    
    
    @Scheduled(fixedDelay=5000)
    public void doSomething() {
        // this will execute periodically, after the one before finishes
    }
}
고정 비율
주기적으로 실행되도록하려면이 코드가 지정한 밀리 초 단위의 값당 한 번 트리거됩니다.
@Component
public class MyScheduler{    
    
    @Scheduled(fixedRate=5000)
    public void doSomething() {
        // this will execute periodically
    }
}
크론 식
Cron 표현식은 6 개의 순차 필드로 구성됩니다.
second, minute, hour, day of month, month, day(s) of week
다음과 같이 선언됩니다.
@Scheduled(cron = "* * * * * *")
또한 시간대 를 -
@Scheduled(cron="* * * * * *", zone="Europe/Istanbul")
노트: -
syntax            means                example                explanation
------------------------------------------------------------------------------------
*                 match any            "* * * * * *"          do always
*/x               every x              "*/5 * * * * *"        do every five seconds
?                 no specification     "0 0 0 25 12 ?"        do every Christmas Day
예: -
syntax                        means
------------------------------------------------------------------------------------
"0 0 * * * *"                 the top of every hour of every day.
"*/10 * * * * *"              every ten seconds.
"0 0 8-10 * * *"              8, 9 and 10 o'clock of every day.
"0 0/30 8-10 * * *"           8:00, 8:30, 9:00, 9:30 and 10 o'clock every day.
"0 0 9-17 * * MON-FRI"        on the hour nine-to-five weekdays
"0 0 0 25 12 ?"               every Christmas Day at midnight
 @Scheduled() 선언 된 메소드는 일치하는 모든 경우에 명시 적으로 호출됩니다. 
cron 표현식이 충족 될 때 일부 코드가 실행되도록하려면 주석에 지정해야합니다.
@Component
public class MyScheduler{    
    
    @Scheduled(cron="*/5 * * * * MON-FRI")
    public void doSomething() {
        // this will execute on weekdays
    }
}
5 초 후마다 우리 콘솔에 현재 시간을 인쇄하고 싶다면 -
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
@Component
public class Scheduler {
    private static final Logger log = LoggerFactory.getLogger(Scheduler.class);
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    @Scheduled(cron = "*/5 * * * * *")
    public void currentTime() {
        log.info("Current Time      = {}", dateFormat.format(new Date()));
    }
}
XML 구성을 사용하는 예 :
예제 수업 :
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
@Component("schedulerBean")
public class Scheduler {
    private static final Logger log = LoggerFactory.getLogger(Scheduler.class);
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    public void currentTime() {
        log.info("Current Time      = {}", dateFormat.format(new Date()));
    }
}  
예제 XML (task-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-4.1.xsd
        http://www.springframework.org/schema/task 
        http://www.springframework.org/schema/task/spring-task-4.1.xsd">
    <task:scheduled-tasks scheduler="scheduledTasks">
        <task:scheduled ref="schedulerBean" method="currentTime" cron="*/5 * * * * MON-FRI" />
    </task:scheduled-tasks>
    <task:scheduler id="scheduledTasks" />
</beans>
Modified text is an extract of the original Stack Overflow Documentation
        아래 라이선스 CC BY-SA 3.0
        와 제휴하지 않음 Stack Overflow