1、项目jar包依赖引入

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>

2、数据库建job运行表,可以详见quartz官方建表脚本

注意,在postgresql下有部分类型转换问题,mysql完美兼容

3、yml配置文件的配置

  ## quartz定时任务,采用数据库方式,driverDelegateClass是为兼容pgl数据库增加的
quartz:
job-store-type: jdbc
properties:
org:
quartz:
jobStore:
driverDelegateClass: org.quartz.impl.jdbcjobstore.PostgreSQLDelegate

4、实体类SysQuartzJob

package org.jeecg.modules.quartz.entity;

import java.io.Serializable;

import org.jeecgframework.poi.excel.annotation.Excel;
import org.springframework.format.annotation.DateTimeFormat; import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; /**
* @Description: 定时任务在线管理
* @Author: jeecg-boot
* @Date: 2019-01-02
* @Version: V1.0
*/
@Data
@TableName("sys_quartz_job")
public class SysQuartzJob implements Serializable {
private static final long serialVersionUID = 1L; /**id*/
@TableId(type = IdType.ID_WORKER_STR)
private java.lang.String id;
/**创建人*/
private java.lang.String createBy;
/**创建时间*/
@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
private java.util.Date createTime;
/**删除状态*/
private java.lang.Integer delFlag;
/**修改人*/
private java.lang.String updateBy;
/**修改时间*/
@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
private java.util.Date updateTime;
/**任务类名*/
@Excel(name="任务类名",width=40)
private java.lang.String jobClassName;
/**cron表达式*/
@Excel(name="cron表达式",width=30)
private java.lang.String cronExpression;
/**参数*/
@Excel(name="参数",width=15)
private java.lang.String parameter;
/**描述*/
@Excel(name="描述",width=40)
private java.lang.String description;
/**状态 0正常 -1停止*/
@Excel(name="状态",width=15)
private java.lang.Integer status; }

5、实现类SysQuartzJobServiceImpl

package org.jeecg.modules.quartz.service.impl;

import java.util.List;

import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.quartz.entity.SysQuartzJob;
import org.jeecg.modules.quartz.mapper.SysQuartzJobMapper;
import org.jeecg.modules.quartz.service.ISysQuartzJobService;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import lombok.extern.slf4j.Slf4j; /**
* @Description: 定时任务在线管理
* @Author: jeecg-boot
* @Date: 2019-04-28
* @Version: V1.1
*/
@Slf4j
@Service
public class SysQuartzJobServiceImpl extends ServiceImpl<SysQuartzJobMapper, SysQuartzJob> implements ISysQuartzJobService {
@Autowired
private SysQuartzJobMapper sysQuartzJobMapper;
@Autowired
private Scheduler scheduler; @Override
public List<SysQuartzJob> findByJobClassName(String jobClassName) {
return sysQuartzJobMapper.findByJobClassName(jobClassName);
} /**
* 保存&启动定时任务
*/
@Override
public boolean saveAndScheduleJob(SysQuartzJob sysQuartzJob) {
if (CommonConstant.STATUS_NORMAL.equals(sysQuartzJob.getStatus())) {
// 定时器添加
this.schedulerAdd(sysQuartzJob.getJobClassName().trim(), sysQuartzJob.getCronExpression().trim(), sysQuartzJob.getParameter());
}
// DB设置修改
sysQuartzJob.setDelFlag(CommonConstant.DEL_FLAG_0);
return this.save(sysQuartzJob);
} /**
* 恢复定时任务
*/
@Override
public boolean resumeJob(SysQuartzJob sysQuartzJob) {
schedulerDelete(sysQuartzJob.getJobClassName().trim());
schedulerAdd(sysQuartzJob.getJobClassName().trim(), sysQuartzJob.getCronExpression().trim(), sysQuartzJob.getParameter());
sysQuartzJob.setStatus(CommonConstant.STATUS_NORMAL);
return this.updateById(sysQuartzJob);
} /**
* 编辑&启停定时任务
* @throws SchedulerException
*/
@Override
public boolean editAndScheduleJob(SysQuartzJob sysQuartzJob) throws SchedulerException {
if (CommonConstant.STATUS_NORMAL.equals(sysQuartzJob.getStatus())) {
schedulerDelete(sysQuartzJob.getJobClassName().trim());
schedulerAdd(sysQuartzJob.getJobClassName().trim(), sysQuartzJob.getCronExpression().trim(), sysQuartzJob.getParameter());
}else{
scheduler.pauseJob(JobKey.jobKey(sysQuartzJob.getJobClassName().trim()));
}
return this.updateById(sysQuartzJob);
} /**
* 删除&停止删除定时任务
*/
@Override
public boolean deleteAndStopJob(SysQuartzJob job) {
schedulerDelete(job.getJobClassName().trim());
boolean ok = this.removeById(job.getId());
return ok;
} /**
* 添加定时任务
*
* @param jobClassName
* @param cronExpression
* @param parameter
*/
private void schedulerAdd(String jobClassName, String cronExpression, String parameter) {
try {
// 启动调度器
scheduler.start(); // 构建job信息
JobDetail jobDetail = JobBuilder.newJob(getClass(jobClassName).getClass()).withIdentity(jobClassName).usingJobData("parameter", parameter).build(); // 表达式调度构建器(即任务执行的时间)
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression); // 按新的cronExpression表达式构建一个新的trigger
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(jobClassName).withSchedule(scheduleBuilder).build(); scheduler.scheduleJob(jobDetail, trigger);
} catch (SchedulerException e) {
throw new JeecgBootException("创建定时任务失败", e);
} catch (RuntimeException e) {
throw new JeecgBootException(e.getMessage(), e);
}catch (Exception e) {
throw new JeecgBootException("后台找不到该类名:" + jobClassName, e);
}
} /**
* 删除定时任务
*
* @param jobClassName
*/
private void schedulerDelete(String jobClassName) {
try {
scheduler.pauseTrigger(TriggerKey.triggerKey(jobClassName));
scheduler.unscheduleJob(TriggerKey.triggerKey(jobClassName));
scheduler.deleteJob(JobKey.jobKey(jobClassName));
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new JeecgBootException("删除定时任务失败");
}
} private static Job getClass(String classname) throws Exception {
Class<?> class1 = Class.forName(classname);
return (Job) class1.newInstance();
} }

6、接口类SysQuartzJobController

package org.jeecg.modules.quartz.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.quartz.entity.SysQuartzJob;
import org.jeecg.modules.quartz.service.ISysQuartzJobService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import lombok.extern.slf4j.Slf4j; /**
* @Description: 定时任务在线管理
* @Author: jeecg-boot
* @Date: 2019-01-02
* @Version:V1.0
*/
@RestController
@RequestMapping("/quartz/sysQuartzJob")
@Slf4j
public class SysQuartzJobController {
@Autowired
private ISysQuartzJobService quartzJobService;
@Autowired
private Scheduler scheduler; /**
* 分页列表查询
*
* @param sysQuartzJob
* @param pageNo
* @param pageSize
* @param req
* @return
*/
@RequestMapping(value = "/list", method = RequestMethod.GET)
public Result<?> queryPageList(SysQuartzJob sysQuartzJob, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
QueryWrapper<SysQuartzJob> queryWrapper = QueryGenerator.initQueryWrapper(sysQuartzJob, req.getParameterMap());
Page<SysQuartzJob> page = new Page<SysQuartzJob>(pageNo, pageSize);
IPage<SysQuartzJob> pageList = quartzJobService.page(page, queryWrapper);
return Result.ok(pageList); } /**
* 添加定时任务
*
* @param sysQuartzJob
* @return
*/
@RequestMapping(value = "/add", method = RequestMethod.POST)
public Result<?> add(@RequestBody SysQuartzJob sysQuartzJob) {
List<SysQuartzJob> list = quartzJobService.findByJobClassName(sysQuartzJob.getJobClassName());
if (list != null && list.size() > 0) {
return Result.error("该定时任务类名已存在");
}
quartzJobService.saveAndScheduleJob(sysQuartzJob);
return Result.ok("创建定时任务成功");
} /**
* 更新定时任务
*
* @param sysQuartzJob
* @return
*/
@RequestMapping(value = "/edit", method = RequestMethod.PUT)
public Result<?> eidt(@RequestBody SysQuartzJob sysQuartzJob) {
try {
quartzJobService.editAndScheduleJob(sysQuartzJob);
} catch (SchedulerException e) {
log.error(e.getMessage(),e);
return Result.error("更新定时任务失败!");
}
return Result.ok("更新定时任务成功!");
} /**
* 通过id删除
*
* @param id
* @return
*/
@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
SysQuartzJob sysQuartzJob = quartzJobService.getById(id);
if (sysQuartzJob == null) {
return Result.error("未找到对应实体");
}
quartzJobService.deleteAndStopJob(sysQuartzJob);
return Result.ok("删除成功!"); } /**
* 批量删除
*
* @param ids
* @return
*/
@RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
if (ids == null || "".equals(ids.trim())) {
return Result.error("参数不识别!");
}
for (String id : Arrays.asList(ids.split(","))) {
SysQuartzJob job = quartzJobService.getById(id);
quartzJobService.deleteAndStopJob(job);
}
return Result.ok("删除定时任务成功!");
} /**
* 暂停定时任务
*
* @param job
* @return
*/
@GetMapping(value = "/pause")
//@ApiOperation(value = "暂停定时任务")
public Result<Object> pauseJob(@RequestParam(name = "jobClassName", required = true) String jobClassName) {
SysQuartzJob job = null;
try {
job = quartzJobService.getOne(new LambdaQueryWrapper<SysQuartzJob>().eq(SysQuartzJob::getJobClassName, jobClassName));
if (job == null) {
return Result.error("定时任务不存在!");
}
scheduler.pauseJob(JobKey.jobKey(jobClassName.trim()));
} catch (SchedulerException e) {
throw new JeecgBootException("暂停定时任务失败");
}
job.setStatus(CommonConstant.STATUS_DISABLE);
quartzJobService.updateById(job);
return Result.ok("暂停定时任务成功");
} /**
* 启动定时任务
*
* @param job
* @return
*/
@GetMapping(value = "/resume")
//@ApiOperation(value = "恢复定时任务")
public Result<Object> resumeJob(@RequestParam(name = "jobClassName", required = true) String jobClassName) {
SysQuartzJob job = quartzJobService.getOne(new LambdaQueryWrapper<SysQuartzJob>().eq(SysQuartzJob::getJobClassName, jobClassName));
if (job == null) {
return Result.error("定时任务不存在!");
}
quartzJobService.resumeJob(job);
//scheduler.resumeJob(JobKey.jobKey(job.getJobClassName().trim()));
return Result.ok("恢复定时任务成功");
} /**
* 通过id查询
*
* @param id
* @return
*/
@RequestMapping(value = "/queryById", method = RequestMethod.GET)
public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
SysQuartzJob sysQuartzJob = quartzJobService.getById(id);
return Result.ok(sysQuartzJob);
} /**
* 导出excel
*
* @param request
* @param response
*/
@RequestMapping(value = "/exportXls")
public ModelAndView exportXls(HttpServletRequest request, SysQuartzJob sysQuartzJob) {
// Step.1 组装查询条件
QueryWrapper<SysQuartzJob> queryWrapper = QueryGenerator.initQueryWrapper(sysQuartzJob, request.getParameterMap());
// Step.2 AutoPoi 导出Excel
ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
List<SysQuartzJob> pageList = quartzJobService.list(queryWrapper);
// 导出文件名称
mv.addObject(NormalExcelConstants.FILE_NAME, "定时任务列表");
mv.addObject(NormalExcelConstants.CLASS, SysQuartzJob.class);
mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("定时任务列表数据", "导出人:Jeecg", "导出信息"));
mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
return mv;
} /**
* 通过excel导入数据
*
* @param request
* @param response
* @return
*/
@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
MultipartFile file = entity.getValue();// 获取上传文件对象
ImportParams params = new ImportParams();
params.setTitleRows(2);
params.setHeadRows(1);
params.setNeedSave(true);
try {
List<SysQuartzJob> listSysQuartzJobs = ExcelImportUtil.importExcel(file.getInputStream(), SysQuartzJob.class, params);
for (SysQuartzJob sysQuartzJobExcel : listSysQuartzJobs) {
quartzJobService.save(sysQuartzJobExcel);
}
return Result.ok("文件导入成功!数据行数:" + listSysQuartzJobs.size());
} catch (Exception e) {
log.error(e.getMessage(), e);
return Result.error("文件导入失败!");
} finally {
try {
file.getInputStream().close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return Result.error("文件导入失败!");
}
}

7、job定时任务样例

package org.jeecg.modules.quartz.job;

import org.jeecg.common.util.DateUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException; import lombok.extern.slf4j.Slf4j; /**
* 示例不带参定时任务
*
* @Author Scott
*/
@Slf4j
public class SampleJob implements Job { @Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { log.info(String.format(" Jeecg-Boot 普通定时任务 SampleJob ! 时间:" + DateUtils.getTimestamp()));
}
}

spring-boot集成Quartz-job存储方式一JDBC的更多相关文章

  1. spring boot集成pagehelper(两种方式)

    当spring boot集成好mybatis时候需要进行分页,我们首先添加maven支持 <dependency> <groupId>com.github.pagehelper ...

  2. Spring Boot集成quartz实现定时任务并支持切换任务数据源

    org.quartz实现定时任务并自定义切换任务数据源 在工作中经常会需要使用到定时任务处理各种周期性的任务,org.quartz是处理此类定时任务的一个优秀框架.随着项目一点点推进,此时我们并不满足 ...

  3. spring boot 集成 quartz 定时任务

    spring boot: @EnableScheduling开启计划任务支持,@Scheduled计划任务声明 1.pom.xml 引入依赖 <dependency> <groupI ...

  4. Spring Boot集成Quartz注入Spring管理的类

    摘要: 在Spring Boot中使用Quartz时,在JOB中一般需要引用Spring管理的Bean,通过定义Job Factory实现自动注入. Spring有自己的Schedule定时任务,在S ...

  5. Spring boot 集成三种定时任务方式

    三种定时任务方式分别为 org.springframework.scheduling.annotation.Scheduled java.util.concurrent.ScheduledExecut ...

  6. Spring boot 集成三种拦截方式

    三种拦截方式分别为: javax.servlet.Filter org.springframework.web.servlet.HandlerInterceptor org.aspectj.lang. ...

  7. Quartz与Spring Boot集成使用

    上次自己搭建Quartz已经是几年前的事了,这次项目中需要定时任务,需要支持集群部署,想到比较轻量级的定时任务框架就是Quartz,于是来一波. 版本说明 通过搜索引擎很容易找到其官网,来到Docum ...

  8. Spring Boot集成持久化Quartz定时任务管理和界面展示

    本文是对之前的一篇文章Spring+SpringMVC+mybatis+Quartz整合代码部分做的一个修改和补充, 其中最大的变化就是后台框架变成了Spring Boot. 本工程所用到的技术或工具 ...

  9. Spring Boot集成MyBatis的2种方式

    目录 写在前面 准备工作 配置数据库驱动 配置数据源 原生集成MyBatis 依赖配置 注册MyBatis核心组件 定义并使用映射器 通过MyBatis-Spring-Boot-Starter集成 默 ...

  10. 【spring boot】14.spring boot集成mybatis,注解方式OR映射文件方式AND pagehelper分页插件【Mybatis】pagehelper分页插件分页查询无效解决方法

    spring boot集成mybatis,集成使用mybatis拖沓了好久,今天终于可以补起来了. 本篇源码中,同时使用了Spring data JPA 和 Mybatis两种方式. 在使用的过程中一 ...

随机推荐

  1. Visual Studio快捷键总览,推荐VS+Resharper实现高效开发

    VS2022之后,其实还挺好用的,但个人还是习惯VS+Resharper的强强组合,尤其是Ctrl+N快捷键的全局搜,比VS自带的Ctrl+T好用太多了,Ctrl+B还能直接查看反编译之后的dll的方 ...

  2. 使用8086汇编驱动SHT11传感器

    前言:使用Proteus 7.8仿真软件实现8086接入SHT11温湿度传感器(实现读取温度数据部分功能),并学习如何在没有集成硬件控制下串行总线的驱动方式,汇编的精髓就是寄存器的操作.各种寻址方式. ...

  3. 如何实现OpenHarmony的OTA升级

    OTA简介 随着设备系统日新月异,用户如何及时获取系统的更新,体验新版本带来的新的体验,以及提升系统的稳定性和安全性成为了每个厂商都面临的严峻问题.OTA(Over the Air)提供对设备远程升级 ...

  4. DEB打包教程

    一.deb简介 deb是一种安装包的格式,linux上常见的安装包主要是deb.rpm 二.deb简单使用 # deb安装 sudo dpkg -i webcamera_1.0_amd64.deb # ...

  5. Python从 requirements.txt 安装库

    pip install -r requirements.txt

  6. Python设计模式----3.单例模式

    单例模式:主要目的是确保某一个类只有一个实例存在 代码: class A(): def __new__(self, *args, **kwargs): if not hasattr(self, 'na ...

  7. 一种新的姿势:程序try/catch抛出异常之绕过canary pwn121

    一种新的姿势:程序try/catch抛出异常之绕过canary 我前面发了不少关于绕过canary的姿势,先总结一下,现在绕过canary的姿势有泄露,爆破,格式化字符串绕过,多线程劫持TLS绕过, ...

  8. win7自带屏幕录像工具

    win7自带屏幕录像工具 2012-03-28 09:23:05      我来说两句      收藏     我要投稿 相信win7自带的屏幕录像工具很多朋友都没用过甚至没有听说过,   但是这款实 ...

  9. nginx重新整理——————http请求的11个阶段中的find_config[十三]

    前言 简单介绍一下find_config 与 preaccess 阶段. 正文 find_config 很大一部分工作是进行location的匹配. 来一张图看下location指令和merge_sl ...

  10. react native 如何用vs code 进行调试

    前言 以前做react-native 写的文章,在此分享一下. 在react-native 中有两种方式调试,一种是crome 调试,一种是本地调试,接下来介绍的是本地调试. 解决方案 在vs cod ...