Spring Boot笔记(三) springboot 集成 Quartz 定时任务
个人博客网:https://wushaopei.github.io/ (你想要这里多有)
1、 在 pom.xml 中 添加 Quartz 所需要 的 依赖
<!--定时器 quartz-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
注意:
quartz 只能基于 springboot 的 parent 2.0 以上 版本 才能使用过,低于2.0 的版本无法加载到 quartz 的pom依赖包
2、创建要执行的任务类:
TestTask1 与 TestTask2
package com.example.poiutis.quartzHandler;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @ClassName TestTask1
* @Description TODO
* @Author wushaopei
* @Date 2019/7/26 12:28
* @Version 1.0
*/
public class TestTask1 extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("TestQuartz01----" + sdf.format(new Date()));
}
}
package com.example.poiutis.quartzHandler;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @ClassName TestTask2
* @Description TODO
* @Author wushaopei
* @Date 2019/7/26 12:28
* @Version 1.0
*/
public class TestTask2 extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("TestQuartz02----" + sdf.format(new Date()));
}
}
3、定时器(执行类):
package com.example.poiutis.quartzHandler;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @ClassName QuartzConfig
* @Description TODO
* @Author wushaopei
* @Date 2019/7/26 12:28
* @Version 1.0
*/
@Configuration
public class QuartzConfig {
private static Logger logger = LoggerFactory.getLogger(QuartzConfig.class);
// testTask1使用的固定间隔方式,testTask2使用的是cron表达式方式。
@Bean
public JobDetail testQuartz1() {
return JobBuilder.newJob(TestTask1.class).withIdentity("testTask1").storeDurably().build();
}
@Bean
public Trigger testQuartzTrigger1() {
//5秒执行一次
SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(5)
.repeatForever();
logger.info(System.currentTimeMillis()+"");
return TriggerBuilder.newTrigger().forJob(testQuartz1())
.withIdentity("testTask1")
.withSchedule(scheduleBuilder)
.build();
}
@Bean
public JobDetail testQuartz2() {
return JobBuilder.newJob(TestTask2.class).withIdentity("testTask2").storeDurably().build();
}
@Bean
public Trigger testQuartzTrigger2() {
//cron方式,每隔5秒执行一次
logger.info(System.currentTimeMillis()+"");
return TriggerBuilder.newTrigger().forJob(testQuartz2())
.withIdentity("testTask2")
.withSchedule(CronScheduleBuilder.cronSchedule("*/5 * * * * ?"))
.build();
}
}
执行结果:

Spring Boot笔记(三) springboot 集成 Quartz 定时任务的更多相关文章
- Spring Boot笔记(六) springboot 集成 timer 定时任务
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 1.创建具体要执行的任务类: package com.example.poiutis.timer; im ...
- Spring Boot笔记(二) springboot 集成 SMTP 发送邮件
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 笔记:乘着项目迭代的间隙,把一些可复用的功能从项目中抽取出来,这是其中之一, 一.添加SMTP 及 MA ...
- Spring Boot笔记(一) springboot 集成 swagger-ui
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 1.添加依赖 <!--SpringBoot整合Swagger-ui--> <depen ...
- Spring Boot笔记(五) SpringBoot 集成Lombok 插件
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 为了减少代码量,为当前项目添加 lombok 来优雅编码 Lombok 插件安装: a . 添加依赖: ...
- Spring Boot实战三:集成RabbitMQ,实现消息确认
Spring Boot集成RabbitMQ相比于Spring集成RabbitMQ简单很多,有兴趣了解Spring集成RabbitMQ的同学可以看我之前的<RabbitMQ学习笔记>系列的博 ...
- Spring Boot笔记三:配置文件
配置文件这里需要讲的东西很多,所以我写在了这里,但是这个是和上篇文章衔接的,所以看这篇文章,先看上篇文章笔记二 一.单独的配置文件 配置文件里面不能都写我们的类的配置吧,这样那么多类太杂了,所以我们写 ...
- Spring boot 入门三:SpringBoot用JdbcTemplates访问Mysql 实现增删改查
建表脚本 -- create table `account`DROP TABLE `account` IF EXISTSCREATE TABLE `account` ( `id` int(11) NO ...
- springboot集成quartz定时任务课动态执行
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</ ...
- SpringBoot集成Quartz实现定时任务
1 需求 在我的前后端分离的实验室管理项目中,有一个功能是学生状态统计.我的设计是按天统计每种状态的比例.为了便于计算,在每天0点,系统需要将学生的状态重置,并插入一条数据作为一天的开始状态.另外,考 ...
随机推荐
- 如何优雅的使用Fegin去构造通用的服务调用的API
第一步: 创建一个公共的API服务:命名为api(根据自己实际情况进行命名) <?xml version="1.0" encoding="UTF-8"?& ...
- 王颖奇 201771010129《面向对象程序设计(java)》第八周学习总结
实验六 接口的定义与使用 实验时间 2018-10-18 1.实验目的与要求 (1) 掌握接口定义方法: (2) 掌握实现接口类的定义要求: (3) 掌握实现了接口类的使用要求: (4) 掌握程序回调 ...
- 深入理解CSS定位
CSS中有3种定位机制:普通流,浮动和绝对定位.除非专门指定,否则所有框都在普通流中定位.顾名思义,普通流中元素框的位置由HTML元素的位置决定.块级框一个接一个地垂直排列,框之间的垂直距离由框的垂直 ...
- Java return 关键字
一.基本概念 return一方面用在循环语句中来结束循环,另一方面用来终止函数的执行或者退出类的方法,并把控制权返回该方法的调用者.如果方法有返回类型,则return的返回该类型的值:如果没有返回值, ...
- FOC 电流采样方案对比(单电阻/双电阻/三电阻)
如果本文帮到了你,帮忙点个赞: 如果本文帮到了你,帮忙点个赞: 如果本文帮到了你,帮忙点个赞: 创作不易 谢谢支持 文章目录 1 电流采样的作用 2 硬件架构 3 采样关键 4 采样方案 5 三电阻采 ...
- MySQL 主从复制原理及过程讲解
mysql主从原理描述,摘自老男孩. 下面简 单描述下 MySQL Replication 复制的原理及过程 . 1.在 Slave 服务器上执行 start slave 命令开启主从复制开关,主从复 ...
- 安装laravel环境之homestead(for mac)
1.先下载virtualbox + vagrant 2.执行命令 vagrant box add laravel/homestead 3.新建一个空文件夹,在里面下载代码.我是放在当前用户下的新建的W ...
- 使用better-scroll在vue中封装自己的Scroll组件
1. better-scroll 原理 用一张图感受: 绿色部分为 wrapper,也就是父容器,它会有固定的高度.黄色部分为 content,它是父容器的第一个子元素,它的高度会随着内容的大小而撑高 ...
- 读懂操作系统(x86)之堆栈帧(过程调用)
前言 为进行基础回炉,接下来一段时间我将持续更新汇编和操作系统相关知识,希望通过屏蔽底层细节能让大家明白每节所阐述内容.当我们写下如下C代码时背后究竟发生了什么呢? #include <stdi ...
- 200万年薪请不到!清华姚班到底有多牛X?
前几天,清华大学自动化系2020年大一新生的C++作业因为太难而上了热搜,该话题在知乎上的热度一度高达 1300+ 万.  在该帖子下方,有很多关于这件事的讨论,其中很多不禁赞叹"清华太牛 ...