4.0.0 0.1.0 test org.springframework.boot spring-boot-starter-parent 1.3.0.RELEASE test-spring-project 1.7 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-maven-plugin org.test.blog
Configuration
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class TestApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(TestApplication.class);
}
}
Scheduled Task
package hello;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class ScheduledTasks {
private static final SimpleDateFormat dateformat = new SimpleDateFormat("HH:mm:ss");
// Excuted every 5, 10 seconds
@Scheduled(cron = "*/5 * * * * *")
public void report5Time() {
System.out.println("every 5 second " + dateformat.format(new Date()));
}
// Excuted every 2,4,6,8,10 seconds
@Scheduled(cron = "*/2 * * * * *")
public void report2Time() {
System.out.println("every 2 second " + dateformat.format(new Date()));
}
// Excuted every day 14:15:00
@Scheduled(cron = "0 15 14 * * *")
public void reportSpecificTime() {
System.out.println("14:15:00 =>" + dateformat.format(new Date()));
}
}
Executed Resultevery 2 second 14:14:54
every 5 second 14:14:55
every 2 second 14:14:56
every 2 second 14:14:58
every 5 second 14:15:00
every 2 second 14:15:00
14:15:00 =>14:15:00
every 2 second 14:15:02
every 2 second 14:15:04
every 5 second 14:15:05
every 2 second 14:15:06
every 2 second 14:15:08
every 2 second 14:15:10
every 5 second 14:15:10
every 2 second 14:15:12
every 2 second 14:15:14
Original Source : https://spring.io/guides/gs/scheduling-tasks/
댓글 없음 :
댓글 쓰기