博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Boot 使用 Spring Schedule
阅读量:6221 次
发布时间:2019-06-21

本文共 3093 字,大约阅读时间需要 10 分钟。

依赖版本

Spring Boot版本:1.5.18

参考文档

springboot配置

# schedulecron.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@EnableSchedulingpublic 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;@Componentpublic class ScheduleOne {    private static final Logger log = org.slf4j.LoggerFactory.getLogger(ScheduleOne.class);    @Scheduled(cron = "${cron.one}")    public void test() {        log.info("计划任务开始执行");            }}复制代码

转载地址:http://bwwna.baihongyu.com/

你可能感兴趣的文章
StringBuilder用法小结
查看>>
对‘初学者应该选择哪种编程语言’的回答——计算机达人成长之路(38)
查看>>
如何申请开通微信多客服功能
查看>>
Sr_C++_Engineer_(LBS_Engine@Global Map Dept.)
查看>>
非监督学习算法:异常检测
查看>>
App开发中甲乙方冲突会闹出啥后果?H5 APP 开发可以改变现状吗
查看>>
jquery的checkbox,radio,select等方法总结
查看>>
Linux coredump
查看>>
Ubuntu 10.04安装水晶(Mercury)无线网卡驱动
查看>>
Myeclipes快捷键
查看>>
我的友情链接
查看>>
ToRPC:一个双向RPC的Python实现
查看>>
我的友情链接
查看>>
nginx在reload时候报错invalid PID number
查看>>
神经网络和深度学习-第二周神经网络基础-第二节:Logistic回归
查看>>
Myeclipse代码提示及如何设置自动提示
查看>>
c/c++中保留两位有效数字
查看>>
ElasticSearch 2 (32) - 信息聚合系列之范围限定
查看>>
VS2010远程调试C#程序
查看>>
[MicroPython]TurniBit开发板DIY自动窗帘模拟系统
查看>>