个人博客网: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. LeetCode--To Lower Case && Remove Outermost Parentheses (Easy)

    709. To Lower Case(Easy)# Implement function ToLowerCase() that has a string parameter str, and retu ...

  2. python语法学习第十一天--迭代器

    迭代:类似循环,这一次的值作为下一次迭代的开始值 BIF:iter():将某个可以作为迭代器的容器变为迭代器   next():做下一次迭代 当next()到最后一个时,抛出StopIteration ...

  3. [hdu3486]rmq+枚举优化

    题意:给n个数,求最小的段数,使得每一段的最大值之和大于给定的k.每一段的长度相等,最后若干个丢掉. 思路:从小到大枚举段数,如果能o(1)时间求出每一段的和,那么总复杂度是O(n(1+1/2+1/3 ...

  4. Python自动生成100以内加减乘除混合运算题

    import random from random import choice ops = ('+','-','×','÷') ans = [] i=0 while i < 100 : op1 ...

  5. 解决Eclipse添加新server时无法选择Tomcat7.0

    新添加tomcat时 出现如下图情况: 解决方法:这时打开工作空间目录下的.metadata\.plugins\org.eclipse.core.runtime\.settings文件夹,删除org. ...

  6. vue.use()方法从源码到使用

    在做 vue 开发的时候大家一定经常接触 Vue.use() 方法,官网给出的解释是: 通过全局方法 Vue.use() 使用插件:我觉得把使用理解成注册更合适一些,首先看下面常见的注册场景. 1 2 ...

  7. Python--WebDriverWait+expected_conditions的一个应用

    from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.s ...

  8. MySQL 数据库的基本使用

    MySQL 是一个关系型数据库管理系统,由瑞典 MySQL AB 公司开发,而MySQL AB 公司被 Oracle 公司收购,故 MySQL 现在属于 Oracle 公司.MySQL 是一种关联数据 ...

  9. Colorful String

    Colorful String #include <bits/stdc++.h> using namespace std; typedef long long ll; ; char s[m ...

  10. VIOS挂载ISO文件

    6.VIOS挂载ISO文件 1.给vhost建立虚拟设备 mkvdev -vadapter vhostX -fbo -dev cdx 2.建立存放ISO的资料库 mkrep -sp rootvg -s ...