参考文章:https://blog.csdn.net/sinianliushui/article/details/78841713

参考文章: https://blog.csdn.net/hao7030187/article/details/79077464

参考文章:https://www.cnblogs.com/domi22/p/9418433.html

springboot的SchedulerTask相对Quartz来说,简单方便,可试用于小型的job处理。

因没法持久化因此不支持分布式部署,和动态数据源配置。

如果要

一、简易配置

1. 开启定时任务,在启动类添加以下注解

@EnableScheduling

2. 创建并发配置,并发线程

@Component
public class SchedulerConfig implements SchedulingConfigurer { /**
* 设置定时任务
* @param scheduledTaskRegistrar
*/
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
scheduledTaskRegistrar.setScheduler(Executors.newScheduledThreadPool());
}
}

3. 创建scheduler,以下是没5秒触发一次,cron表达式有cron在线生成器可以用

@Component
@Slf4j
public class SchedulerTask { @Scheduled(cron="0/5 * * * * ? ")
public void testTask(){
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
log.debug(Thread.currentThread() + " " + sdf.format(new Date()));
}
    @Scheduled(cron="0/5 * * * * ? ")
public void test2Task(){
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
log.debug(Thread.currentThread() + " " + sdf.format(new Date()));
}
}

二、分开配置

如下例子,如果只有一个Bean,名字为taskScheduler,或者方法名为taskScheduler则会作为自动的Scheduler。使用 @Scheduled即对应的是此TaskScheduler。

参见文章:  https://blog.csdn.net/sinianliushui/article/details/78841713

/**
* description:
* 定时任务
* @Autor:DennyZhao
* @Date:2019/2/15
* @Version: 1.0.0
*/
@Component
@EnableScheduling
public class SchedulerConfig {
/** task相关的属性文件 **/
@Autowired
private TaskProperties taskProperties; /**
* 设备组织用Job
* @return
*/
@Bean("DeviceJob")
public TaskScheduler initDeviceOrgTask(){
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
//线程池大小
scheduler.setPoolSize(taskProperties.getDeviceThreadCount());
//线程名字前缀
scheduler.setThreadNamePrefix("taskThread-deviceTask: ");
scheduler.initialize();
Trigger trigger = new CronTrigger(taskProperties.getDeviceOrgCron());
scheduler.schedule(new DeviceOrgTask(), trigger);
return scheduler;
} /**
* Jpush用Job
* @return
*/
@Bean("JpushJob")
public TaskScheduler initJpushTask(){
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
//线程池大小
scheduler.setPoolSize(taskProperties.getJpushThreadCount());
//线程名字前缀
scheduler.setThreadNamePrefix("taskThread-jpushTask: ");
scheduler.initialize();
Trigger trigger = new CronTrigger(taskProperties.getJpushReportCron());
scheduler.schedule(new JpushReportTask(), trigger);
return scheduler;
}
}

TaskPropertis

/**
* description:
* JPUSH执行任务相关参数
*
* @Autor:DennyZhao
* @Date:2019/2/15
* @Version: 1.0.0
*/
@Component
@PropertySource("classpath:task-config.properties")
@ConfigurationProperties(prefix="task")
@Data
public class TaskProperties { /** 极光推送appKey **/
private String jpushAppKey;
/** 极光推送secretKey **/
private String jpushSecretKey; /** 设备JOB的线程数 **/
private int deviceThreadCount;
/** 设备组织Cron **/
private String deviceOrgCron;
/** Jpush的JOB线程数 **/
private int jpushThreadCount;
/** Jpush的推送报告Cron **/
private String jpushReportCron;
}

创建类实现Runable接口即可使用。

@Component
@Slf4j
public class DeviceOrgTask implements Runnable { @Override
public void run() {
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
log.debug(Thread.currentThread() + " " + sdf.format(new Date()));
}
}

 

问题: 

1.  从以上的一简易配置中(如果不添加SchedulerConfig)也可以运行,发现如果在方法中添加Thread.sleep(),会影响下面方法的运行和下次的运行时间。

因为默认的 ConcurrentTaskScheduler 计划执行器采用Executors.newSingleThreadScheduledExecutor() 实现单线程的执行器。

因此要使用异步: 采用异步的方式执行调度任务,配置 Spring 的 @EnableAsync,在执行定时任务的方法上标注 @Async配置任务执行。注意线程池大小要依据单个任务时间和任务间隔。

2. 分布式重复执行

1.使用 redis分布式锁setnx命令 来控制是否已经存在有任务在执行。

2.使用spring的shedLock,创建一个数据表,先更新者执行,非更新者不执行。

参见:https://segmentfault.com/a/1190000011975027

3. 主程序挂掉后,job停止,数据丢失

使用redis记录执行时间。

异常:

[ERROR] 2019-03-21 14:00:38,757 >>> org.springframework.scheduling.support.TaskUtils$LoggingErrorHandler.handleError(TaskUtils.java:96)
[massage] Unexpected error occurred in scheduled task.
java.lang.IllegalStateException: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@8f84321 has been closed already

使用applicationContext未获取到bean异常。

因EnabledTask会使得任务在执行完后closeContext导致,在配置文件添加:

spring.cloud.task.closecontext_enable=false

高版本用 spring.cloud.task.close_context_enabled=false

参考文章: https://stackoverflow.com/questions/48933575/spring-cloud-task-scheduling-context-closed

springboot + schedule的更多相关文章

  1. Quartz 和 springboot schedule中的cron表达式关于星期(周几)的不同表示

    一.Quartz中cron 表达式分析: quartz 官方源码(org.quartz.CronExpression)解释: Cron expressions are comprised of 6 r ...

  2. SpringBoot Schedule 配置

    1. 定时任务实现方式 定时任务实现方式: Java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务.使用这种方式可以让你的程序按照某一个频度执行 ...

  3. SpringBoot入门教程(九)定时任务Schedule

    在日常项目运行中,我们总会有需求在某一时间段周期性的执行某个动作.比如每天在某个时间段导出报表,或者每隔多久统计一次现在在线的用户量.在springboot中可以有很多方案去帮我们完成定时器的工作,有 ...

  4. springBoot中使用定时任务

    简单示例 导入依赖 springBoot已经默认集成了定时任务的依赖,只需要引入基本的依赖就可以使用定时任务. <parent> <groupId>org.springfram ...

  5. SpringBoot定时任务 - 经典定时任务设计:时间轮(Timing Wheel)案例和原理

    Timer和ScheduledExecutorService是JDK内置的定时任务方案,而业内还有一个经典的定时任务的设计叫时间轮(Timing Wheel), Netty内部基于时间轮实现了一个Ha ...

  6. springboot集成schedule(深度理解)

    背景 在项目开发过程中,我们经常需要执行具有周期性的任务.通过定时任务可以很好的帮助我们实现. 我们拿常用的几种定时任务框架做一个比较: 从以上表格可以看出,Spring Schedule框架功能完善 ...

  7. springboot集成schedule

    背景 在项目开发过程中,我们经常需要执行具有周期性的任务.通过定时任务可以很好的帮助我们实现. 我们拿常用的几种定时任务框架做一个比较: 从以上表格可以看出,Spring Schedule框架功能完善 ...

  8. SpringBoot专题2----springboot与schedule的激情相拥

    Schedule:计划,任务.就是我们常说的定时任务.这在我们做一些系统的时候,是经常需要用到的.比如:定时更新一些数据,定时更新索引,都需要这样的一个功能. 第一:创建一个名为springboot- ...

  9. SpringBoot系列:Spring Boot定时任务Spring Schedule

    Spring Schedule是Spring提供的定时任务框架,相较于Quartz,Schedule更加简单易用,在中小型应用中,对于大部分需求,Schedule都可以胜任. 一.Spring Sch ...

随机推荐

  1. vue中父组件给子组件额外添加参数

    1 子组件: this.$emit('callbackone',item.parentId) 2 父组件: @callbackone="callbackone($event,index)&q ...

  2. Hadoop MapReduce2.0(Yarn)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/cqboy1991/article/details/25056283 MapReduce2.0(Yar ...

  3. redux源码解读(二)

    之前,已经写过一篇redux源码解读(一),主要分析了 redux 的核心思想,并用100多行代码实现一个简单的 redux .但是,那个实现还不具备合并 reducer 和添加 middleware ...

  4. JIT(Just in time,即时编译,边运行边编译)、AOT(Ahead Of Time,运行前编译),是两种程序的编译方式

    JIT(Just in time,即时编译,边运行边编译).AOT(Ahead Of Time,运行前编译),是两种程序的编译方式

  5. S50 抓取pattern数据

    S50(原V50) 测试机台湾久元电子研发的一款数字芯片测试系统,行业内有很多人使用: 现在记录下S50抓取pattern数据的一些方法: 程序主要是通过read_log配合c代码实现,pattern ...

  6. Linux基础入门-用户及文件权限管理

    一.Linux用户管理: 不同的用户的文件都是放在同一个物理磁盘上的甚至同一个逻辑分区或者目录里,但是由于Linux的用户管理和权限机制,不同用户不能轻易查看.修改彼此的文件. 1. 查看用户: wh ...

  7. [ZZ] 边缘检测 梯度与Roberts、Prewitt、Sobel、Lapacian算子

    http://blog.csdn.net/swj110119/article/details/51777422 一.学习心得: 学习图像处理的过程中,刚开始遇到图像梯度和一些算子的概念,这两者到底是什 ...

  8. Ignite(三): Ignite VS Spark

    参考:https://www.itcodemonkey.com/article/9613.html gnite 和 Spark,如果笼统归类,都可以归于内存计算平台,然而两者功能上虽然有交集,并且 I ...

  9. centos 后台挂起运行python

    用Xshell连接服务器运行python文件,当关闭终端或连接断开后相应的python文件也就不会继续运行了,要达到后台挂起运行就要使用 nohup 命令了. 用法如下: # -u 表示禁止缓存,将结 ...

  10. Javascript小问题

    1.原生对象克隆 var clone = function(obj) { var o; if (typeof obj == "object") { if (obj === null ...