springboot集成quartz

导入依赖

<!--添加quartz的依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-quartz</artifactId>
</dependency>

application.yml配置文件

spring:
  quartz:
    #相关属性配置
    properties:
      org:
        quartz:
          scheduler:
            instanceName: clusteredScheduler
            instanceId: AUTO
          jobStore:
            class: org.quartz.impl.jdbcjobstore.JobStoreTX
            driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
            tablePrefix: QRTZ_
            isClustered: true
            clusterCheckinInterval: 10000
            useProperties: false
          threadPool:
            class: org.quartz.simpl.SimpleThreadPool
            threadCount: 10
            threadPriority: 5
            threadsInheritContextClassLoaderOfInitializingThread: true
    #数据库方式
    job-store-type: jdbc
    #初始化表结构.初次使用的时候会在库中生成表格,后期注释掉即可.或者初始使用值为always,然后将值改为never
    #jdbc:
      #initialize-schema: always

quartz配置类

package com.theeternity.common.quartz;

import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @program: ApiBoot
 * @description: quartz配置类
 * @author: TheEternity Zhang
 * @create: 2019-03-11 11:01
 */
@Configuration
public class QuartzConfig {
    @Bean
    public JobDetail uploadTaskDetail() {
        return JobBuilder.newJob(TestQuartz.class).withIdentity("testQuartz").storeDurably().build();
    }

    @Bean
    public Trigger uploadTaskTrigger() {
        //设置执行频率
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("*/5 * * * * ?");
        return TriggerBuilder.newTrigger().forJob(uploadTaskDetail())
                .withIdentity("testQuartz")
                .withSchedule(scheduleBuilder)
                .build();
    }
}

quartz具体执行类

package com.theeternity.common.quartz;

import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.UUID;

/**
 * @program: ApiBoot
 * @description: 测试quartz
 * @author: TheEternity Zhang
 * @create: 2019-03-11 11:01
 * @DisallowConcurrentExecution 注解得作用是:是否并发执行,系统默认为true,即第一个任务还未执行完整,第二个任务如果到了执行时间,则会立马开启新线程执行任务,这样如果我们是从数据库读取信息,两次重复读取可能出现重复执行任务的情况,所以我们需要将这个值设置为false,这样第二个任务会往后推迟,只有在第一个任务执行完成后才会执行第二个任务。
 */
@DisallowConcurrentExecution
public class TestQuartz extends QuartzJobBean {

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        String random= UUID.randomUUID().toString();
        System.out.println("任务开始:"+random);
        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("任务结束:"+random);
    }
}

参考文档:

https://www.jianshu.com/p/056281e057b3 (流程参考)
https://docs.spring.io/spring-boot/docs/2.0.x-SNAPSHOT/reference/htmlsingle/#boot-features-quartz (官方文档)
https://blog.csdn.net/tushuping/article/details/79636207 (具体代码)

原文地址:https://www.cnblogs.com/eternityz/p/12243160.html

时间: 2024-08-03 18:10:59

springboot集成quartz的相关文章

SpringBoot集成Quartz动态定时任务

项目中需要用到定时任务,考虑了下java方面定时任务无非就三种: 用Java自带的timer类.稍微看了一下,可以实现大部分的指定频率的任务的调度(timer.schedule()),也可以实现关闭和开启(timer.cancle).但是用其来实现某天的某个时间或者某月的某一天调度任务有点不方便. 采用Quartz 调度器实现.这是一个功能很强大的开源的专门用于定时任务调度的框架,也很好的和springboot整合,缺点:配置复杂,需要花费一定的时间去了解和研究. spring3.0以后自带的s

【spring-boot】 springboot集成quartz实现定时任务

在做项目时有时候会有定时器任务的功能,比如某某时间应该做什么,多少秒应该怎么样之类的. spring支持多种定时任务的实现.我们来介绍下使用spring的定时器和使用quartz定时器 1.我们使用spring-boot作为基础框架,其理念为零配置文件,所有的配置都是基于注解和暴露bean的方式. 2.使用spring的定时器: spring自带支持定时器的任务实现.其可通过简单配置来使用到简单的定时任务. @Component @Configurable @EnableScheduling p

Springboot集成Quartz集群

为什么要使用Quzrtz集群 在项目进行集群部署时,如果业务在执行中存在互斥关系,没有对定时任务进行统一管理,就会引起业务的多次执行,不能满足业务要求.这时就需要对任务进行管理,要保证一笔业务在所有的集群环境中,有且只有一台机器能执行该任务. 如果不适用Quartz集群,要如何实现这种业务逻辑? 在这里只列出两种简单的思路: 利用单线程机制.可以在redis中设置一个属性为空,每次任务执行时去设置这个全局变量,进入任务中需要对值进行校验,值不为空则跳过本次执行任务,值为空时进行设置,方法执行完毕

SpringBoot 2.1.1.RELEASE 集成quartz

SpringBoot 2.1.1.RELEASE 集成quartzhttp://www.qchcloud.cn/system/article/show/70依赖配置: <!-- 定时任务 --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> </dependency> 定时任务: @Component

SpringBoot 1.5.x 集成 Quartz 任务调度框架

Quartz 有分 内存方式 和 数据库方式 内存方式任务信息保存在内存中, 停机会丢失, 需手动重新执行, 数据库方式: 任务信息保存在数据库中, 重点是支持集群. 内存方式 RAMJobStore 和 数据库方式 JobStoreTX, RAMJobStore 适合单机, 不支持集群,  JobStoreTX 支持集群. 下面介绍的就是 JobStoreTX 数据库方式. 1. 添加主要的相关依赖, 其他依赖这里就不说了 <dependency> <groupId>org.qu

spring集成quartz

spring集成quartz 注意:出现异常"Caused by: java.lang.IncompatibleClassChangeError: class org.springframework.scheduling.quartz.CronTriggerBean has interface org.quartz.CronTrigger as super class" Spring3.0不支持Quartz2.0,因为org.quartz.CronTrigger在2.0从class变成

spring 中集成quartz定时器及quartz中cronExpression配置说明

 spring 中集成quartz: spring文件的配置: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http:/

SpringBoot集成MyBatis的分页插件PageHelper

俗话说:好??不吃回头草,但是在这里我建议不管你是好马还是不好马,都来吃吃,带你复习一下分页插件PageHelper. 昨天给各位总结了本人学习springboot整合mybatis第一阶段的一些学习心得和源码,主要就算是敲了一下SpringBoot的门儿,希望能给各位的入门带给一点儿捷径,今天给各位温习一下MyBatis的分页插件PageHelper和SpringBoot的集成,它的使用也非常简单,开发更为高效.因为PageHelper插件是属于MyBatis框架的,所以相信很多哥们儿都已经用

springboot集成swagger2构建RESTful API文档

在开发过程中,有时候我们需要不停的测试接口,自测,或者交由测试测试接口,我们需要构建一个文档,都是单独写,太麻烦了,现在使用springboot集成swagger2来构建RESTful API文档,可以在访问接口上,直接添加注释 先介绍一下开发环境: jdk版本是1.8 springboot的版本是1.4.1 开发工具为 intellij idea 我们先引入swagger2的jar包,pom文件引入依赖如下: <dependency> <groupId>io.springfox&