Quartz实例:quartz定时任务代码示例
转自:http://www.blogchong.com/post/96.html
quartz定时任务调度框架,使用实例。
Job类://即实际调度任务实现
、
package net.csdn.edm.util.quartz;import java.util.HashMap;import java.util.Map;import net.csdn.common.logging.CSLogger;import net.csdn.common.logging.Loggers;import org.quartz.Job;import org.quartz.JobDataMap;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;/** * @author:blogchong * @blog: http://www.blogchong.com/ * @Version:1.0 * @CreateTime:2014年12月30日 下午3:27:02 * @Description:定时进度job */public class QuartzJob implements Job { protected CSLogger logger = Loggers.getLogger(QuartzJob.class); @SuppressWarnings("static-access") public void execute(JobExecutionContext arg0) throws JobExecutionException { // 接收参数 JobDataMap jobDataMap = arg0.getJobDetail().getJobDataMap(); //通过这种方式,传递参数 String taskId = jobDataMap.getString("taskId"); //具体的定时任务实现,在这里实现即可 //具体实现略过 }} |
添加Job类:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
package net.csdn.edm.util.quartz;import net.csdn.common.logging.CSLogger;import net.csdn.common.logging.Loggers;import net.csdn.edm.util.MacroDef;import org.quartz.CronScheduleBuilder;import org.quartz.JobBuilder;import org.quartz.JobDetail;import org.quartz.Scheduler;import org.quartz.SchedulerException;import org.quartz.SchedulerFactory;import org.quartz.Trigger;import org.quartz.TriggerBuilder;import org.quartz.impl.StdSchedulerFactory;/** * @author:blogchong * @blog: http://www.blogchong.com/ * @Version:1.0 * @CreateTime:2014年12月30日 下午3:28:32 * @Description:添加job接口 */public class QuartzAdd {public static SchedulerFactory schedFact = new StdSchedulerFactory();protected static CSLogger logger = Loggers.getLogger(QuartzAdd.class);public static Scheduler sched;public static void startSched() throws SchedulerException {try {QuartzAdd.sched = QuartzAdd.schedFact.getScheduler();QuartzAdd.sched.start();} catch (Exception e) {e.printStackTrace();}}public static boolean quartzAdd(String type, String taskId) throws Exception {try {// 若sched未赋值或者未启动,则先在全局中启动它if (QuartzAdd.sched == null || !QuartzAdd.sched.isStarted()) {QuartzAdd.startSched();}//定时规则,跟普通crontable的差不多String rule = "0 0/" + MacroDef.QUARTZ_INTERVAL + " * * * ?";//设置组名,和任务名String quartz_name = taskId;String quartz_group = type;// 创建jobDetail实例,指定job名以及所属组JobDetail jobDetail = JobBuilder.newJob(QuartzJob.class).withIdentity(quartz_name, quartz_group).build();jobDetail.getJobDataMap().put("taskId", taskId);Trigger trigger = TriggerBuilder.newTrigger().withIdentity(quartz_name, quartz_group).withSchedule(CronScheduleBuilder.cronSchedule(rule)).startNow().build();QuartzAdd.sched.scheduleJob(jobDetail, trigger);logger.info("[已添加定时获取进度任务, taskID:" + taskId + ", type:" + type + "]");return true;} catch (Exception e) {logger.error("[添加定时任务出错,任务号:" + taskId + "]");logger.error(e.toString());return false;}}} |
Quartz实例:quartz定时任务代码示例的更多相关文章
- 【Quartz】将定时任务持久化到数据库
之前的文章所做的demo是将定时任务的信息保存在内存中的,见以下配置 org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore 如果,我们需要在 ...
- 原!总结 quartz集群 定时任务 测试运行ok
由于项目优化重构,想将定时任务从quartz单机模式变成集群或分布式的方式.于是,百度了一圈....修修改改...用集群的方式部署定时任务,测试可以... 集群?分布式?什么区别? 集群:同一个业务, ...
- 3分钟掌握Quartz.net分布式定时任务的姿势
引言 长话短说,今天聊一聊分布式定时任务,我的流水账笔记: ASP.NET Core+Quartz.Net实现web定时任务 AspNetCore结合Redis实践消息队列 细心朋友稍一分析,就知道还 ...
- 使用Spring整合Quartz轻松完成定时任务
一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...
- Spring 3整合Quartz 2实现定时任务--转
常规整合 http://www.meiriyouke.net/?p=82 最近工作中需要用到定时任务的功能,虽然Spring3也自带了一个轻量级的定时任务实现,但感觉不够灵活,功能也不够强大.在考虑之 ...
- Spring 整合 Quartz 实现动态定时任务
复制自:https://www.2cto.com/kf/201605/504659.html 最近项目中需要用到定时任务的功能,虽然Spring 也自带了一个轻量级的定时任务实现,但感觉不够灵活,功能 ...
- 【转】Spring 整合 Quartz 实现动态定时任务
http://blog.csdn.net/u014723529/article/details/51291289 最近项目中需要用到定时任务的功能,虽然spring 也自带了一个轻量级的定时任务实现, ...
- Spring 整合 Quartz 实现动态定时任务(附demo)
最近项目中需要用到定时任务的功能,虽然Spring 也自带了一个轻量级的定时任务实现,但感觉不够灵活,功能也不够强大.在考虑之后,决定整合更为专业的Quartz来实现定时任务功能. 普通定时任务 首先 ...
- Spring整合Quartz轻松完成定时任务
一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...
随机推荐
- 书不在多,精读则灵 - Oracle入门书籍推荐
作者:eygle |English [转载时请标明出处和作者信息]|[恩墨学院 OCM培训传DBA成功之道]链接:http://www.eygle.com/archives/2006/08/ora ...
- 折纸---珠穆朗玛问题----简单for 循环
一张纸的厚度大约是0.08mm,对折多少次之后能达到珠穆朗玛峰的高度(8848.13米)? package com.zuoye.test; public class Zhezhi { public s ...
- 【Oracle】append
我们在生产环境中经常遇到需要往表中插入大量数据的情况,怎么样才能让插入数据的速度变快呢?Oracle中的append简直就是神器!!没图说个**,直接上图: 是不是看晕了?哈哈,莫慌,请看下面总结: ...
- Windows-Server-2008、IIS7.0环境下配置伪静态化
在Windows-Server-2008.IIS7.0环境下配置伪静态化 首先,是IIS7.0的配置,由于Windows Server 2008操作系统默认的IIS版本为 ...
- JAVA面试题基础部分(二)
10.使用 final 关键字修饰一个变量时,是引用不能变,还是引用的对象不能变?使用 final 关键字修饰一个变量时,是指引用变量不能变,引用变量所指向的对象中的内容还是可以改变的.例如,对于如下 ...
- Qt无法用UTF-8编辑问题
原因: Windows默认编码格式是GBK. 而QT-各默认版本的编码格式是UTF-8. 解决方法": Windows环境下,Qt Creator,菜单->工具->选项-> ...
- Ubuntu安装中文语言包
使用Ubuntu 默认的界面感觉不习惯,于是安装KDE界面. 1.安装kde 使用命令行: sudo apt-get install kubuntu-desktop 安装后发现不能使用中文, 在 se ...
- 作业07之《MVC模式》
MVC(Model View Controller)模型-视图-控制器 MVC与模板概念的理解 MVC本来是存在于Desktop程序中的,M是指数据模型,V是指用户界面,C则是控制器.使用MVC的目的 ...
- python tips:类的绑定方法(bound)和非绑定方法(unbound)
类属性只有类及其实例能够访问,可以理解为一个独立的命名空间. Python中类属性的引用方式有两种: 1. 通过类的实例进行属性引用,称为绑定方法(bound method),可以理解为方法与实例绑定 ...
- tomcat配置SSH加密
[root@tomcat2 ~]# keytool -genkeypair -alias tomcat -keyalg RSA -keystore /usr/local/tomcat7/keystor ...