首先你得有一个用Maven搭好的SSM框架,数据库用的Mysql,这里只有关于Quartz的部分。其实有大神总结的很好了,但做完后总有些地方不一样,所以写这篇作为笔记。这里先把大神的写的分享给大家:https://blog.csdn.net/u010648555/article/details/60767633

一、准备工作

1. 在maven的pom文件中添加Quartz的Jar坐标

<!-- quartz 定时器 -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>
2. 在web.xml中添加quartz的监听器

<!-- 配置quartz监听器 -->
<listener>
<listener-class>
org.quartz.ee.servlet.QuartzInitializerListener
</listener-class>
</listener>
3. 添加quartz.properties配置文件

# Configure Main Scheduler Properties
org.quartz.scheduler.instanceName: dufy_test
org.quartz.scheduler.instanceId = AUTO
org.quartz.scheduler.rmi.export: false
org.quartz.scheduler.rmi.proxy: false
org.quartz.scheduler.wrapJobExecutionInUserTransaction: false

# Configure ThreadPool
org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 2
org.quartz.threadPool.threadPriority: 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true
org.quartz.jobStore.misfireThreshold: 60000

# Configure JobStore 持久化配置
#org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore
org.quartz.jobStore.class:org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass:org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.useProperties:true

# 是否集群
org.quartz.jobStore.isClustered = false

# 表前缀
org.quartz.jobStore.tablePrefix:qrtz_
#org.quartz.jobStore.dataSource:qzDS

# 下面是数据库的配置,交给Spring管理了
#org.quartz.dataSource.qzDS.driver:com.mysql.jdbc.Driver
#org.quartz.dataSource.qzDS.URL:jdbc:mysql://localhost:3306/quartz_test
#org.quartz.dataSource.qzDS.user:root
#org.quartz.dataSource.qzDS.password:123
#org.quartz.dataSource.qzDS.maxConnection:10
4. 添加Spring配置:applicationContext-quartz.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

<!-- =========JDBC版=========== -->
<!--
持久化数据配置,需要添加quartz.properties
-->
<bean name="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="dataSource" ref ="dataSource" />
<property name="applicationContextSchedulerContextKey" value="applicationContextKey"/>
<property name="configLocation" value="classpath:resource/quartz.properties"/>
</bean>
</beans>
5. 将Quartz自带的表导入Mysql

create table qrtz_job_details(
sched_name varchar(120) not null,
job_name varchar(80) not null,
job_group varchar(80) not null,
description varchar(120),
job_class_name varchar(128) not null,
is_durable integer not null,
is_nonconcurrent integer not null,
is_update_data integer not null,
requests_recovery integer not null,
job_data blob(2000),
primary key (sched_name,job_name,job_group)
);

create table qrtz_triggers(
sched_name varchar(120) not null,
trigger_name varchar(80) not null,
trigger_group varchar(80) not null,
job_name varchar(80) not null,
job_group varchar(80) not null,
description varchar(120),
next_fire_time bigint,
prev_fire_time bigint,
priority integer,
trigger_state varchar(16) not null,
trigger_type varchar(8) not null,
start_time bigint not null,
end_time bigint,
calendar_name varchar(80),
misfire_instr smallint,
job_data blob(2000),
primary key (sched_name,trigger_name,trigger_group),
foreign key (sched_name,job_name,job_group) references qrtz_job_details(sched_name,job_name,job_group)
);

create table qrtz_simple_triggers(
sched_name varchar(120) not null,
trigger_name varchar(80) not null,
trigger_group varchar(80) not null,
repeat_count bigint not null,
repeat_interval bigint not null,
times_triggered bigint not null,
primary key (sched_name,trigger_name,trigger_group),
foreign key (sched_name,trigger_name,trigger_group) references qrtz_triggers(sched_name,trigger_name,trigger_group)
);

create table qrtz_cron_triggers(
sched_name varchar(120) not null,
trigger_name varchar(80) not null,
trigger_group varchar(80) not null,
cron_expression varchar(120) not null,
time_zone_id varchar(80),
primary key (sched_name,trigger_name,trigger_group),
foreign key (sched_name,trigger_name,trigger_group) references qrtz_triggers(sched_name,trigger_name,trigger_group)
);

CREATE TABLE qrtz_simprop_triggers(
sched_name varchar(120) not null,
TRIGGER_NAME VARCHAR(200) NOT NULL,
TRIGGER_GROUP VARCHAR(200) NOT NULL,
STR_PROP_1 VARCHAR(512) NULL,
STR_PROP_2 VARCHAR(512) NULL,
STR_PROP_3 VARCHAR(512) NULL,
INT_PROP_1 INT NULL,
INT_PROP_2 INT NULL,
LONG_PROP_1 BIGINT NULL,
LONG_PROP_2 BIGINT NULL,
DEC_PROP_1 NUMERIC(13,4) NULL,
DEC_PROP_2 NUMERIC(13,4) NULL,
BOOL_PROP_1 VARCHAR(1) NULL,
BOOL_PROP_2 VARCHAR(1) NULL,
PRIMARY KEY (sched_name,TRIGGER_NAME,TRIGGER_GROUP),
FOREIGN KEY (sched_name,TRIGGER_NAME,TRIGGER_GROUP)
REFERENCES QRTZ_TRIGGERS(sched_name,TRIGGER_NAME,TRIGGER_GROUP)
);

create table qrtz_blob_triggers(
sched_name varchar(120) not null,
trigger_name varchar(80) not null,
trigger_group varchar(80) not null,
blob_data blob(2000),
primary key (sched_name,trigger_name,trigger_group),
foreign key (sched_name,trigger_name,trigger_group) references qrtz_triggers(sched_name,trigger_name,trigger_group)
);

create table qrtz_calendars(
sched_name varchar(120) not null,
calendar_name varchar(80) not null,
calendar blob(2000) not null,
primary key (calendar_name)
);

create table qrtz_fired_triggers(
sched_name varchar(120) not null,
entry_id varchar(95) not null,
trigger_name varchar(80) not null,
trigger_group varchar(80) not null,
instance_name varchar(80) not null,
fired_time bigint not null,
sched_time bigint not null,
priority integer not null,
state varchar(16) not null,
job_name varchar(80),
job_group varchar(80),
is_nonconcurrent integer,
requests_recovery integer,
primary key (sched_name,entry_id)
);

create table qrtz_paused_trigger_grps(
sched_name varchar(120) not null,
trigger_group varchar(80) not null,
primary key (sched_name,trigger_group)
);

create table qrtz_scheduler_state(
sched_name varchar(120) not null,
instance_name varchar(80) not null,
last_checkin_time bigint not null,
checkin_interval bigint not null,
primary key (sched_name,instance_name)
);

create table qrtz_locks(
sched_name varchar(120) not null,
lock_name varchar(40) not null,
primary key (sched_name,lock_name)
);
6. 自己建立管理定时任务的表,我简单写了几个必须的属性

public class QuartzTask {

/*
* 这个类用于展示定时的任务,同时作用于定时任务的恢复、删除、中止;
**/

private Long jobId;

private String jobClass; //任务类的全限定类名

private String jobGroup; //任务组名

private String jobName; //任务名

private String triggerName; //任务触发器名

private String triggerGroupName; //任务触发器组名

private String cronExpr; //时间表达式

private Integer jobStatus; //任务状态

private String startTime; //任务开始时间
7. 准备俩简单的Jsp页面用于添加定时任务和定时任务列表展示

大致效果如下:

二、简单代码实现

1.写个任务类,本想做个功能,失败了,就简单控制台输出了

public class MyJob implements Job{

private static final Logger log = LoggerFactory.getLogger(MyJob.class);

@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {

log.info("MyJob is start ..................");

log.info("Hello quzrtz "+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ").format(new Date()));

log.info("MyJob is end .....................");

}
}
2. Controller层:QuartzController

@Controller
@RequestMapping("user/quartz")
public class QuartzController {

@Autowired
private QuartzTaskService quartzTaskService;

/**
* 添加定时任务
* */
@RequestMapping(value = "add",method = RequestMethod.POST)
public ModelAndView addQtz(QuartzTask qt){

try {
//1.成功启动定时任务
this.quartzTaskService.addQtz(qt);

//2.封装QuartzTask,执行保存
this.quartzTaskService.saveAddQtz(qt);

return new ModelAndView("success");

} catch (Exception e) {
e.printStackTrace();
}
return new ModelAndView("error");

}

/**
* 定时任务列表
* */
@RequestMapping(value = "list",method = RequestMethod.GET)
public ModelAndView listQtz(){

try {
//1.查询所有定时任务
List<QuartzTask> list = this.quartzTaskService.listQtz();
ModelAndView mv = new ModelAndView();
mv.addObject("qtzList",list);
mv.setViewName("quartz-list");

return mv;

} catch (Exception e) {
e.printStackTrace();
}
return new ModelAndView("error");

}

/**
* 删除定时任务
* */
@RequestMapping(value = "delete",method = RequestMethod.GET)
public ModelAndView deleteQtz(QuartzTask qt){

try {
// 执行删除
this.quartzTaskService.deleteQtz(qt);

// 根据id删除定时任务列表数据
this.quartzTaskService.deleteQtzTask(qt.getJobId());

return new ModelAndView("success");

} catch (Exception e) {
e.printStackTrace();
}
return new ModelAndView("error");

}

/**
* 中止定时任务
* */
@RequestMapping(value = "pause",method = RequestMethod.GET)
public ModelAndView pauseQtz(QuartzTask qt){

try {
// 执行中止
this.quartzTaskService.pauseQtz(qt);

return new ModelAndView("success");

} catch (Exception e) {
e.printStackTrace();
}
return new ModelAndView("error");

}

/**
* 恢复定时任务
* */
@RequestMapping(value = "resume",method = RequestMethod.GET)
public ModelAndView resumeQtz(QuartzTask qt){

try {
// 执行中止
this.quartzTaskService.resumeQtz(qt);

return new ModelAndView("success");

} catch (Exception e) {
e.printStackTrace();
}
return new ModelAndView("error");

}
}
3. Service层:QuartzTaskServiceImpl

@Service
public class QuartzTaskServiceImpl implements QuartzTaskService {

@Autowired
private QuartzDao quartzDao;

@Autowired
private Scheduler scheduler;

@Override
public void addQtz(QuartzTask qt) {

try {
// 1.创建一个JobDetail实例,指定Quartz
String cla = qt.getJobClass();
Class clazz = Class.forName(cla);
JobDetail jobDetail = JobBuilder.newJob(clazz) // 任务执行类
.withIdentity(qt.getJobName(), qt.getJobGroup())// 任务名,任务组
.build();
CronScheduleBuilder builder = CronScheduleBuilder.cronSchedule(qt.getCronExpr());

// 2.创建Trigger(触发器)
Trigger trigger = TriggerBuilder.newTrigger().withIdentity(qt.getTriggerName(), qt.getTriggerGroupName())
.startNow().withSchedule(builder).build();

// 3.告诉调度器使用该触发器来安排作业
scheduler.scheduleJob(jobDetail, trigger);

// 4.启动
if (!scheduler.isShutdown()) {
scheduler.start();
}

} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public void saveAddQtz(QuartzTask qt) {

qt.setJobId(1l);
qt.setJobStatus(1);
qt.setStartTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ").format(new Date()));

this.quartzDao.saveAddQtz(qt);

}

@Override
public List<QuartzTask> listQtz() {

return quartzDao.listQtz();
}

@Override
public void deleteQtz(QuartzTask qt) {

try {
// 停止触发器
scheduler.pauseTrigger(TriggerKey.triggerKey(qt.getTriggerName(), qt.getTriggerGroupName()));
// 移除触发器
scheduler.unscheduleJob(TriggerKey.triggerKey(qt.getTriggerName(), qt.getTriggerGroupName()));
// 删除任务
scheduler.deleteJob(JobKey.jobKey(qt.getJobName(), qt.getJobGroup()));
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public void deleteQtzTask(Long jobId) {

this.quartzDao.deleteQtzTask(jobId);

}

@Override
public void pauseQtz(QuartzTask qt) {

try {
scheduler.pauseJob(JobKey.jobKey(qt.getJobName(), qt.getJobGroup()));
} catch (SchedulerException e) {
e.printStackTrace();
}

}

@Override
public void resumeQtz(QuartzTask qt) {

try {
scheduler.resumeJob(JobKey.jobKey(qt.getJobName(), qt.getJobGroup()));
} catch (SchedulerException e) {
e.printStackTrace();
}

}

}
4. 与数据库交互的QuartzTask.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<!-- 根标签,对应一个Dao接口 -->
<mapper namespace="cn.jindou.crm.mapper.QuartzDao">

<resultMap type="cn.jindou.crm.pojo.QuartzTask" id="quartzMap">
<id column="job_id" property="jobId"/>
<result column="job_class" property="jobClass"/>
<result column="job_group" property="jobGroup"/>
<result column="job_name" property="jobName"/>
<result column="trigger_name" property="triggerName"/>
<result column="trigger_groupName" property="triggerGroupName"/>
<result column="cron_expr" property="cronExpr"/>
<result column="job_status" property="jobStatus"/>
<result column="start_time" property="startTime"/>
</resultMap>

<insert id="saveAddQtz" parameterType="cn.jindou.crm.pojo.QuartzTask">
INSERT INTO quartz
(job_id,job_class,job_group,job_name,trigger_name,trigger_groupName,cron_expr,job_status,start_time)
VALUES(#{jobId},#{jobClass},#{jobGroup},#{jobName},#{triggerName},#{triggerGroupName},#{cronExpr},#{jobStatus},#{startTime});
</insert>

<select id="listQtz" resultMap="quartzMap">
select * from quartz;
</select>

<delete id="deleteQtzTask" parameterType="java.lang.Long">
DELETE from quartz where job_id = #{jobId};
</delete>

</mapper>

我这里只做了简单实现,还有很多地方不懂,继续努力。
---------------------
原文:https://blog.csdn.net/weixin_38943098/article/details/87807678

Quartz简单实现定时任务管理(SSM+Quartz)的更多相关文章

  1. springmvc+quartz简单实现定时调度

    一.简介:Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用.Quartz可以用来创建简单或为运行十 ...

  2. Quartz 定时任务管理

    前言 将项目中的所有定时任务都统一管理吧,使用 quartz 定时任务 设计思路 使用 quartz 的相关jar 包,懒得去升级了,我使用的是 quart 1.6 写一个定时任务管理类 用一张数据库 ...

  3. 基于Quartz实现简单的定时发送邮件

    一.什么是Quartz Quartz 是一个轻量级任务调度框架,只需要做些简单的配置就可以使用:它可以支持持久化的任务存储,即使是任务中断或服务重启后,仍可以继续运行.Quartz既可以做为独立的应用 ...

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

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

  5. SpringBoot2.x集成Quartz实现定时任务管理(持久化到数据库)

    1. Quartz简介   Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目.   Quartz是一个完全由Java编写的开源作业调度框架,为在Java应 ...

  6. Spring结合Quartz实现多任务定时调用(转载)

    Quartz框架提供了丰富的任务调度支持,比如,在 何时执行何种任务,它是一个开源的由OpenSymphony维护的项目,开发者能够在Java EE,或单独的Java SE应用中使用它.无论是简单的任 ...

  7. 定时调度框架Quartz随笔

    最近项目中的定时批处理用到了quartz定时任务,在此记录下quartz的配置吧,一个小demo仅供参考,也方便自己今后复习! 下面直接来步骤吧! 一.首先,要搭起能让quartz正常运行的环境,至少 ...

  8. Quartz.net 的开源任务管理平台

    Quartz.net 的开源任务管理平台 前面总结了很多,关于Quartz.net 的文章,介绍了如何使用Quartz.net.不清楚的朋友,可以看我之前的系列文章,http://www.cnblog ...

  9. windows 服务实现定时任务调度(Quartz.Net)

    我们通常在一些情况下需要软件具有一个自动执行某些任务的功能,但是又不希望直接启动软件,或者每次都要手动的来启动软件,这时我们可可以考虑到windows服务了. 首先创建一个windows服务项目(详细 ...

随机推荐

  1. 一次数组越界的bug经历

    数组和指针都是C里面的好东西,但是一旦使用不当,真的会让人抓狂. 下面是写程序时遇到的一次数组越界的经历,感觉对以后写程序有点启发,所以记录下来. 起因: 我想用OLED动态显示一组浮点数,而且浮点数 ...

  2. python中读写excel并存入mysql

    为了一个突如其来的想法:用python简单解决就好.现在算是把这个项目需要的基础功能坑都填完了.剩下就是AI和数据展示方面的坑了. 今天遇到的坑是: 1.从excel读出的中文是乱码 2.中文写入my ...

  3. Project facet Java version 1.8 not supported JDK版本不对无法启动项目解决办法

    https://jingyan.baidu.com/article/6c67b1d69a59a02787bb1e30.html

  4. 巩固java(六)----java中可变参数方法(非常实用哦)

    java提供了可变参数的方法,即方法的参数个数可以不确定,用"..."定义. import java.util.ArrayList; import java.util.List; ...

  5. C++雾中风景9:emplace_back与可变长模板

    C++11的版本在vector容器添加了emplace_back方法,相对于原先的push_back方法能够在一定程度上提升vector容器的表现性能.所以我们从STL源码角度来切入,看看这两种方法有 ...

  6. (二)Web应用体系结构

    容器 Servlet没有main()方法,它们受控于另一个Java应用,这个Java应用称为容器(Container).我们最常见的tomcat就是这样一个容器. Web服务器应用(如Apache)得 ...

  7. BZOJ_1801_[Ahoi2009]chess 中国象棋_DP

    BZOJ_1801_[Ahoi2009]chess 中国象棋_DP Description 在N行M列的棋盘上,放若干个炮可以是0个,使得没有任何一个炮可以攻击另一个炮. 请问有多少种放置方法,中国像 ...

  8. Hadoop大数据部署

    Hadoop大数据部署 一. 系统环境配置: 1. 关闭防火墙,selinux 关闭防火墙: systemctl stop firewalld systemctl disable firewalld ...

  9. C++线程安全日志库-Win32接口实现

    分享一个C++日志库,使用Win32接口编写,而且是线程安全的日志库.比较简单,只有2个文件,容易上手,使用起来也很简单 头文件 如下是日志库的头文件,接口看似很多,但是使用起来最常用的也就那么几个 ...

  10. asp.net core系列 43 Web应用 Session分布式存储(in memory与Redis)

    一.概述 HTTP 是无状态的协议. 默认情况下,HTTP 请求是不保留用户值或应用状态的独立消息. 本文介绍了几种保留请求间用户数据和应用状态的方法.下面以表格形式列出这些存储方式,本篇专讲Sess ...