个人博客网: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. .Net Core WPF之XAML概述

    原文链接,机器翻译,有误处参看原文. XAML overview in WPF 2019/08/08 What is XAML XAML syntax in brief Case and white ...

  2. C# 中 枚举Enum 一些转换的方法整理

    工作中 经常遇到枚举 的一些转换  特别是获取枚举备注等  特地整理下 方法以后使用 public void TestMethod1() { TestEnumOne colorEnum = TestE ...

  3. python统计英文文本中的回文单词数

    1. 要求: 给定一篇纯英文的文本,统计其中回文单词的比列,并输出其中的回文单词,文本数据如下: This is Everyday Grammar. I am Madam Lucija And I a ...

  4. Linux --如何新增一块硬盘并自动挂载

    1. 虚拟机添加硬盘 2.  分区 fdisk /dev/sdb 3. 格式化 mkfs -t ext4 /dev/sdb1 将刚刚创建的盘格式化成 ext4格式 4. 挂载 先创建一个目录,/hom ...

  5. 小程序externalClasses介绍

    小程序externalClasses 1.介绍:我们在封装组件的时候,有时候需要对外暴露出class,可以由调用者来决定组件中一部分的样式,此时就需要使用它了 // components/dong/i ...

  6. css实现双色饼图

    from:wx--前端早读课 首先回想用css画三角形的方法: <div class="triangle"></div> .triangle { displ ...

  7. Java基础之数据类型

    一.数据类型 基本数据类型介绍 byte 1字节 char 2字节 short 2字节 int 4字节 long 8字节 float 4字节 double 8字节 以上有Java中八大基本类型的7种, ...

  8. 用C++验证三门问题

    三门问题(换门): #include <iostream> #include <cstdlib> #include <ctime> #define random(a ...

  9. ThreadLocal 内存泄漏问题深入分析

    写在前面 ThreadLocal 基本用法本文就不介绍了,如果有不知道的小伙伴可以先了解一下,本文只研究 ThreadLocal 内存泄漏这一问题. ThreadLocal 会发生内存泄漏吗? 先给出 ...

  10. npm run build 时 报 __webpack_public_path__ = window.webpackPublicPath; 中的windows未定义

    原本 webpack.js在webpack.config.babel.js同目录下,在app.jsx中引用,用mac打包没问题,但是window就报window未定义,改到src和app.jsx同目录 ...