java quartz spring_JavaLib-quartz | 基于Spring Boot Quartz开发的定时任务
基于Spring Boot Quartz开发的JavaLib-quartz,目的是帮你快速构建定时任务系统,你可以专心编写你的业务逻辑,而不必关注定时任务具体是如何实现的,他的性能如何,有没有异常以及异常处理,监控等等问题。这些你可以在文档中得知。
快速使用
第1步、添加依赖
jitpack.io
https://jitpack.io
com.github.fengwenyi
JavaLib-quartz
1.0-gamma
第2步、HelloTask.java
package com.fengwenyi.example.javalib_quartz.start;
import com.fengwenyi.javalib.quartz.QuartzTask;
import org.springframework.stereotype.Component;
/**
* @author Wenyi Feng
*/
@Component
public class HelloTask extends QuartzTask {
}
第3步、HelloJob.java
package com.fengwenyi.example.javalib_quartz.start;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import java.util.Date;
/**
* @author Wenyi Feng
*/
public class HelloJob extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
System.out.println("Hello : " + new Date());
}
}
第4步、HelloController.java
package com.fengwenyi.example.javalib_quartz.start;
import com.fengwenyi.javalib.quartz.ScheduleBean;
import com.fengwenyi.javalib.quartz.TimeTypeEnum;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
/**
* @author Wenyi Feng
*/
@RestController
@RequestMapping("/hello")
public class HelloController {
@Autowired
private Scheduler scheduler;
@Autowired
private HelloTask helloTask;
@RequestMapping("/job")
public boolean job() {
String jobName = "JOB";
String triggerName = "TRIGGER";
ScheduleBean scheduleBean = new ScheduleBean(scheduler, HelloJob.class, jobName, triggerName);
scheduleBean.setTimeType(TimeTypeEnum.AT_TIME);
scheduleBean.setAtTime(System.currentTimeMillis() + 1000 * 10); // 10s之后运行
boolean rs = false;
try {
rs = helloTask.start(scheduleBean);
System.out.println("cTime : " + new Date());
} catch (SchedulerException e) {
e.printStackTrace();
}
return rs;
}
}
第5步、浏览器访问
http://localhost:8080/hello/job
如果看到 true ,那就继续下一步,否则就是出错了,需要去检查错误。
第6步、运行效果
API
名称
方法
参数
返回类型
说明
开启定时任务
start
(ScheduleBean)
boolean
开启是否成功,true:成功,false:失败
定时任务当前状态
status
-
boolean
定时任务当前状态,true:运行中,false:已停止
停止定时任务
stop
-
boolean
定时任务停止是否成功,true:成功,false:失败
ScheduleBean字段说明
名称
参数
类型
说明
Scheduler
scheduler
Scheduler
Scheduler 对象
编号
id
Long
-,保留字段
名称
name
String
-,保留字段
描述
description
String
-,保留字段
选用类型
timeTime
TimeTypeEnum
选用构造Trigger对象类型
job类
clazz
Class extends Job>
定时任务执行的job类
job参数
paramJobMap
Map
定时任务执行的job类
job类
paramTriggerMap
Map
定时任务执行的job类
cron表达式
cron
String
cron表达式
时间间隔
time
Integer
每隔一段时间执行一次
时间间隔
atTime
Long
指定一个时间点执行(毫秒数)
Job名称
jobName
String
Job名称
Job组
jobGroup
String
Job组名称
TriggerName
triggerName
String
Trigger名称
Trigger组
triggerGroup
String
Trigger组名称
TimeTypeEnum
字段说明
字段
类型
说明
code
Integer
代码
msg
String
说明
值说明
名称
代码
说明
SIMPLE
1
简单的定时任务,每隔一段时间执行一次
AT_TIME
2
指定一个时间点执行(毫秒数[Long])
CRON
3
使用cron表达式(时间点、循环、自定义时间)
wiki
一、需要在Job中注入Service
使用 @Autowired 注解
@Autowired
private DBService dbService;
二、每隔一段时间执行一次
int time;
ScheduleBean scheduleBean;
scheduleBean.setTimeType(TimeTypeEnum.SIMPLE);
scheduleBean.setTime(time);
三、指定一个时间点执行一次
long atTime;
ScheduleBean scheduleBean;
scheduleBean.setTimeType(TimeTypeEnum.AT_TIME);
scheduleBean.setAtTime(atTime);
四、通过使用cron表达式执行
String cron;
ScheduleBean scheduleBean;
scheduleBean.setTimeType(TimeTypeEnum.CRON);
scheduleBean.setCron(cron);
五、参数
// 将参数放到job中
Map jobMap;
ScheduleBean scheduleBean;
scheduleBean.setParamJobMap(jobMap);
// 将参数放到trigger中
Map triggerMap;
ScheduleBean scheduleBean;
scheduleBean.setParamTriggerMap(triggerMap);
六、关于在job获取参数
两种思路: 一是通过map的key获取值, 二是通过构造与map的key相同的属性,提供set方法
// 获取自己的参数
JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
jobDataMap.getInt("");
jobDataMap.getString("");
jobDataMap.getFloat("");
jobDataMap.getDouble("");
JobDataMap triggerDataMap = context.getTrigger().getJobDataMap();
// 合并
// 如果job和trigger的key相同,trigger会覆盖job的值
JobDataMap dataMap = context.getMergedJobDataMap();
七、在job中获取jobDetail、trigger基础信息
// 获取jobDetail相关
JobKey jobKey = context.getJobDetail().getKey();
jobKey.getName();
jobKey.getGroup();
jobKey.getClass().getName();
// 获取trigger相关
TriggerKey triggerKey = context.getTrigger().getKey();
triggerKey.getName();
triggerKey.getGroup();
八、task目前支持的方法
开启任务:start
查看当前任务状态:status
停止任务:stop
策略
1、优先选用指定方式构造Trigger
2、检查顺序:cron->atTime->simple,执行顺序:simple > atTime > cron 自下而上进行覆盖
资料
About Me
©author Wenyi Feng
Licensed
Copyright 2018 Wenyi Feng(xfsy_2015@163.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
java quartz spring_JavaLib-quartz | 基于Spring Boot Quartz开发的定时任务相关推荐
- 基于Spring boot框架开发的电商网站系统
目 录 第一章 绪论- 2 1.1 编写目的 - 2 1.2 项目背景 - 2 1.3 项目需求 - 2 第二章 系统体系结构 - 3 2.1 系统体系结构 - 3 2.2 数据库设计 E-R 图 - ...
- java天气信息管理系统,基于 Spring Boot + Spring Cloud 实现天气预报系统
[课程内容] 第1章 导学及SpringCloud介绍 1-1 spring boot简介 1-2 开启第一个spring boot 项目 第2章 基于Spring Boot快速构建天气预报系统 2- ...
- java restful接口开发实例_实战:基于Spring Boot快速开发RESTful风格API接口
写在前面的话 这篇文章计划是在过年期间完成的,示例代码都写好了,结果亲戚来我家做客,文章没来得及写.已经很久没有更新文章了,小伙伴们,有没有想我啊.言归正传,下面开始,今天的话题. 目标 写一套符合规 ...
- 招聘管理系统软件java源码_基于Spring Boot的java开源招聘源码-铭阳招聘管理系统...
铭阳招聘管理系统 铭阳招聘管理系统,采用流行的框架Spring Boot+mybatis+ehcache开发,实现了权限管理,solr全文搜索引擎,系统具执行效率高.模板自由切换.后台管理功能灵活等诸 ...
- 牛皮!竟然有大佬基于 Spring Boot + Vue 开发了一套网易云amp;QQ音乐(附源码)。。。...
来源:segmentfault.com/a/1190000021376934 # 前言 虽然 B/S 是目前开发的主流,但是 C/S 仍然有很大的市场需求.受限于浏览器的沙盒限制,网页应用无法满足某些 ...
- 瑞吉外卖项目 基于spring Boot+mybatis-plus开发 超详细笔记,有源码链接
本项目是基于自学b站中 黑马程序员 的瑞吉外卖项目:视频链接: 黑马程序员Java项目实战<瑞吉外卖>,轻松掌握springboot + mybatis plus开发核心技术的真java实 ...
- java集合系列之18 spring boot程序员的必修课
Spring Boot 2.0 的推出又激起了一阵学习 Spring Boot 热,就单从我个人的博客的访问量大幅增加就可以感受到大家对学习 Spring Boot 的热情,那么在这么多人热衷于学习 ...
- 基于spring boot + MybatisPlus 商城管理系统的Java开源商城系统
前言 Mall4j项目致力于为中小企业打造一个完整.易于维护的开源的电商系统,采用现阶段流行技术实现.后台管理系统包含商品管理.订单管理.运费模板.规格管理.会员管理.运营管理.内容管理.统计报表.权 ...
- 【毕业设计】基于spring boot的图书管理系统 -java 计算机 软件工程
文章目录 1 前言 2 系统简介 2.1 领域模型 2.2 技术栈 2.3 表结构设计 2.4 接口设计 2.4.1 接口定义 2.4.2 接口测试 2.5 权限设计 3 运行效果 3.1 系统登录 ...
最新文章
- 入机器学习大坑,需要什么样的数学水平?
- Redis学习-String
- 桌面笔记工具KeepNote
- 谈谈分布式事务之三: System.Transactions事务详解[下篇]
- 第八章 PX4-SDlog解析
- ImportError: cannot import name 'six' from 'django.utils'
- eeprom的wp 引脚_EEPROM存储芯片24C02
- nginx 注释配置及详解
- Ubuntu 忘记密码的处理方法
- “心脏出血”后,OpenSSL 起死回生靠什么?
- 计算机c语言在线课堂,计算机(C语言)
- PC端打开微信公众号文章 图片加载慢的解决方法
- java 堆栈内存例子,内存溢出OOM和堆栈溢出SOF的示例
- 微信公众平台服务号与订阅号区别详解
- 【C语言】计算日期差
- 蛋白结构分析实操教程
- 使用echarts的3D地图中的map3D与scatter3D混合使用时出现坐标位移的情况
- 【FFmpeg编码】了解速率控制模式(x264、x265、vpx)
- 固定资产减值准备、累计折旧
- 详译:RESIDUAL AND PLAIN CONVOLUTIONAL NEURAL NETWORKS FOR 3D BRAIN MRICLASSIFICATION
热门文章
- 八种常见的 SQL 错误用法
- C#下2\10\16进制互转代码总汇
- 提问:访问服务器时提示system.componentmodel.win32exception: 拒绝访问
- 【MATLAB】求点到多边形的最短距离
- CentOS7 源码编译安装NodeJS 最新版本Shell脚本
- char数组拷贝wchar数组
- iframe父页面与子页面之间的元素获取与方法调用
- 如何用c语言从txt文件中读取数据
- linux coreutils升级,Coreutils
- 西南民族大学计算机考试试题,西南民族大学预科教育学院 2007级《计算机》模拟试题(含答案)...