个人博客网: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 定时任务的更多相关文章

  1. Spring Boot笔记(六) springboot 集成 timer 定时任务

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 1.创建具体要执行的任务类: package com.example.poiutis.timer; im ...

  2. Spring Boot笔记(二) springboot 集成 SMTP 发送邮件

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 笔记:乘着项目迭代的间隙,把一些可复用的功能从项目中抽取出来,这是其中之一, 一.添加SMTP 及 MA ...

  3. Spring Boot笔记(一) springboot 集成 swagger-ui

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 1.添加依赖 <!--SpringBoot整合Swagger-ui--> <depen ...

  4. Spring Boot笔记(五) SpringBoot 集成Lombok 插件

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 为了减少代码量,为当前项目添加 lombok 来优雅编码 Lombok 插件安装: a . 添加依赖: ...

  5. Spring Boot实战三:集成RabbitMQ,实现消息确认

    Spring Boot集成RabbitMQ相比于Spring集成RabbitMQ简单很多,有兴趣了解Spring集成RabbitMQ的同学可以看我之前的<RabbitMQ学习笔记>系列的博 ...

  6. Spring Boot笔记三:配置文件

    配置文件这里需要讲的东西很多,所以我写在了这里,但是这个是和上篇文章衔接的,所以看这篇文章,先看上篇文章笔记二 一.单独的配置文件 配置文件里面不能都写我们的类的配置吧,这样那么多类太杂了,所以我们写 ...

  7. Spring boot 入门三:SpringBoot用JdbcTemplates访问Mysql 实现增删改查

    建表脚本 -- create table `account`DROP TABLE `account` IF EXISTSCREATE TABLE `account` ( `id` int(11) NO ...

  8. springboot集成quartz定时任务课动态执行

    <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</ ...

  9. SpringBoot集成Quartz实现定时任务

    1 需求 在我的前后端分离的实验室管理项目中,有一个功能是学生状态统计.我的设计是按天统计每种状态的比例.为了便于计算,在每天0点,系统需要将学生的状态重置,并插入一条数据作为一天的开始状态.另外,考 ...

随机推荐

  1. HTTP GET | POST | DELETE请求

    依赖: <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp& ...

  2. 【Scala】isInstanceOf 与 classOf的对比,代码+注解简洁明了

    class Animal { } class Cat extends Animal { } object Cat { def main(args: Array[String]): Unit = { / ...

  3. 字节码编程,Javassist篇三《使用Javassist在运行时重新加载类「替换原方法输出不一样的结果」》

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 通过前面两篇 javassist 的基本内容,大体介绍了:类池(ClassPool) ...

  4. Jekyll 解决Jekyll server本地预览文章not found的问题

    layout: post tags: [Jekyll] comments: true 执行Jekyll本地浏览器预览指令 bundle exec jekyll serve 进入浏览器输入127.0.0 ...

  5. 我去,你竟然还不会用 synchronized

    二哥,离你上一篇我去已经过去两周时间了,这个系列还不打算更新吗?着急着看呢. 以上是读者 Jason 发来的一条信息,不看不知道,一看真的是吓一跳,上次我去是 4 月 3 号更新的,离现在一个多月了, ...

  6. 基于Kubernetes服务发现机制的探讨Non Service

    服务注册 注册中⼼作为一般的RPC/Web服务中的底层设施提供了服务进程元数据(IP, Port, Interface, Group,Method等)存储,被Watch的功能,每个服务进程均需接⼊同⼀ ...

  7. [zoj 3416/hdu 3709]数位DP

    题意:求从区间[L, R]内有多少个数是平衡数,平衡数是指以10进制的某一位为中心轴,左右两边的每一位到中心轴的距离乘上数位上的值的和相等.0<=L<=R<=1e18 思路:由于任何 ...

  8. Mysql常用sql语句(19)- in / exists 子查询

    测试必备的Mysql常用sql语句系列 https://www.cnblogs.com/poloyy/category/1683347.html 前言 子查询在我们查询方法中是比较常用的,通过子查询可 ...

  9. 反射,获取类的属性以及get方法

    vo实体类: public class Result { /*** * 返回结果code * ok:10000 * error:20000 */ private String code; /*** * ...

  10. mysql5.6 thread pool

    从percona 的压测来看,确实很牛笔啊.提升很大. http://www.mysqlperformanceblog.com/2014/01/29/percona-server-thread-poo ...