Spring Scheduling



引入Maven依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
</dependencies>

注册ScheduledTask bean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Component
public class ScheduledTasks {

private Logger logger = LoggerFactory.getLogger(ScheduledTasks.class);

private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

// 每0.5秒执行一次
@Scheduled(fixedRate = 500)
public void reportCurrentTime() {
logger.info("The time is now {}", dateFormat.format(new Date()));
}

// 第一次延迟5秒,之后每延迟1秒执行一次
@Scheduled(initialDelay = 5000, fixedDelay = 1000)
public void reportAgain() {
logger.info("Current time: {}", dateFormat.format(new Date()));
}
}

除了上述的两种方式配置周期外,还可以通过定制cron的方式来配置周期:

@Scheduled(cron="0 0 * * * *") // 代表每个整点执行
@Scheduled(cron="0 0 8-10 * * *") // 代表每天8、9、10点整执行

具体的配置规则可以参考CronSequenceGenerator

配置启动类

@SpringBootApplication
@EnableScheduling
public class Main {

    public static void main(String[] args){
        SpringApplication.run(Main.class, args);
    }
}

一定要配置上@EnableScheduling注解,这样spring-boot才会创建一个后台TaskExecutor来执行定时任务

启动main方法后,可以看到输出结果为:

2018-01-30 14:15:06.130 [pool-2-thread-1] INFO  com.xavier.ScheduledTasks - The time is now 14:15:06
2018-01-30 14:15:06.630 [pool-2-thread-1] INFO  com.xavier.ScheduledTasks - The time is now 14:15:06
2018-01-30 14:15:07.129 [pool-2-thread-1] INFO  com.xavier.ScheduledTasks - The time is now 14:15:07
2018-01-30 14:15:07.632 [pool-2-thread-1] INFO  com.xavier.ScheduledTasks - The time is now 14:15:07
2018-01-30 14:15:07.634 [pool-2-thread-1] INFO  com.xavier.ScheduledTasks - Current time: 14:15:07
2018-01-30 14:15:08.129 [pool-2-thread-1] INFO  com.xavier.ScheduledTasks - The time is now 14:15:08
2018-01-30 14:15:08.630 [pool-2-thread-1] INFO  com.xavier.ScheduledTasks - The time is now 14:15:08
2018-01-30 14:15:08.635 [pool-2-thread-1] INFO  com.xavier.ScheduledTasks - Current time: 14:15:08
2018-01-30 14:15:09.129 [pool-2-thread-1] INFO  com.xavier.ScheduledTasks - The time is now 14:15:09

定制Executor

我们可以通过@Configuration注解来定制自己的Executor类,注意一定要实现ScheduledExecutorServiceTaskScheduler接口,我们来举个栗子:

@Configuration
public class CustomScheduleExecutor {

    @Bean(destroyMethod = "shutdown")
    public ScheduledExecutorService taskScheduler() {
        return new ScheduledThreadPoolExecutor(10, new ThreadFactory() {
            private AtomicInteger count = new AtomicInteger(0);

            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, "customScheduledThread-" + count.incrementAndGet());
            }
        });
    }
}

在classpath中添加上述自定义配置类之后,再重新开启应用,可以看到控制台的输出变为:

2018-01-30 14:20:26.565 [customScheduledThread-7] INFO  com.xavier.ScheduledTasks - The time is now 14:20:26
2018-01-30 14:20:27.065 [customScheduledThread-7] INFO  com.xavier.ScheduledTasks - The time is now 14:20:27
2018-01-30 14:20:27.566 [customScheduledThread-4] INFO  com.xavier.ScheduledTasks - The time is now 14:20:27
2018-01-30 14:20:27.566 [customScheduledThread-1] INFO  com.xavier.ScheduledTasks - Current time: 14:20:27
2018-01-30 14:20:28.066 [customScheduledThread-4] INFO  com.xavier.ScheduledTasks - The time is now 14:20:28
2018-01-30 14:20:28.566 [customScheduledThread-4] INFO  com.xavier.ScheduledTasks - The time is now 14:20:28
2018-01-30 14:20:28.567 [customScheduledThread-10] INFO  com.xavier.ScheduledTasks - Current time: 14:20:28
2018-01-30 14:20:29.067 [customScheduledThread-5] INFO  com.xavier.ScheduledTasks - The time is now 14:20:29
2018-01-30 14:20:29.564 [customScheduledThread-5] INFO  com.xavier.ScheduledTasks - The time is now 14:20:29
2018-01-30 14:20:29.572 [customScheduledThread-7] INFO  com.xavier.ScheduledTasks - Current time: 14:20:29

可以看到,线程的名字已经更改为ThreadFactory设置的customScheduledThread,线程池的数量为10个

代码详见github


-------------本文结束感谢您的阅读-------------
0%