Spring|Spring Boot 使用 Spring Schedule
[toc]
依赖版本
【Spring|Spring Boot 使用 Spring Schedule】Spring Boot版本:1.5.18参考文档
springboot 定时任务@Scheduled 和 异步@Asyncspringboot配置
# schedule
cron.one=0 30 10 * * ?
Spring Schedule 配置
import org.slf4j.Logger;
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.config.CronTask;
import org.springframework.scheduling.config.IntervalTask;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.ScheduledMethodRunnable;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledThreadPoolExecutor;
@Configuration
@EnableScheduling
public class ScheduleConfigurer implements SchedulingConfigurer {
private static final Logger log = org.slf4j.LoggerFactory.getLogger(ScheduleConfigurer.class);
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskExecutor());
/**
* 启动时打印任务
*/
List cronTasks = taskRegistrar.getCronTaskList();
List fixedDelayTasks = taskRegistrar.getFixedDelayTaskList();
List fixedRateTasks = taskRegistrar.getFixedRateTaskList();
for (CronTask cronTask : cronTasks){
String expression = cronTask.getExpression();
ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) cronTask.getRunnable();
log.info("method: {},expression: {}",runnable.getMethod(),expression);
}for (IntervalTask intervalTask : fixedDelayTasks){
ScheduledMethodRunnable runnable = (ScheduledMethodRunnable)intervalTask.getRunnable();
log.info("method: {},InitialDelay: {}",runnable.getMethod(),intervalTask.getInitialDelay());
}for (IntervalTask intervalTask : fixedRateTasks){
long interval = intervalTask.getInterval();
ScheduledMethodRunnable runnable = (ScheduledMethodRunnable)intervalTask.getRunnable();
log.info("method: {},Initial: {}",runnable.getMethod(),intervalTask.getInterval());
}
}@Bean(destroyMethod="shutdown")
public Executor taskExecutor() {
// 线程池
ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(5);
//scheduledThreadPoolExecutor.setRejectedExecutionHandler(new RejectedExecutionHandler() {
//@Override
//public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
//
//}
//});
return scheduledThreadPoolExecutor;
}
}
设置定时任务
import org.slf4j.Logger;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class ScheduleOne {
private static final Logger log = org.slf4j.LoggerFactory.getLogger(ScheduleOne.class);
@Scheduled(cron = "${cron.one}")
public void test() {
log.info("计划任务开始执行");
}
}
推荐阅读
- 由浅入深理解AOP
- 【译】20个更有效地使用谷歌搜索的技巧
- Activiti(一)SpringBoot2集成Activiti6
- mybatisplus如何在xml的连表查询中使用queryWrapper
- MybatisPlus|MybatisPlus LambdaQueryWrapper使用int默认值的坑及解决
- MybatisPlus使用queryWrapper如何实现复杂查询
- SpringBoot调用公共模块的自定义注解失效的解决
- 解决SpringBoot引用别的模块无法注入的问题
- iOS中的Block
- Linux下面如何查看tomcat已经使用多少线程