一、spring整合

  网上一搜有很多整合的方式,这里我采用了其中的一种(暂时还没有对其他的方法研究过)。

  对于spring的整合其中的任务,spring提供了几个类、接口(这些类都实现了Job接口):

  org.springframework.scheduling.quartz.QuartzJobBean

  org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean.MethodInvokingJob 

  org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean.StatefulMethodInvokingJob

  

  QuartzJobBean和MethodInvokingJob是无状态的,StatefulMethodInvokingJob是有状态的。

  可以选择自己的需求选择继承与哪个类。 

  关于Scheduler在Spring上的配置:

 <bean name="quartzScheduler" lazy-init="false"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property> <property name="applicationContextSchedulerContextKey"
value="applicationContextKey" />
<property name="autoStartup" value="true" />
<property name="configLocation" value="classpath:spring/quartz.properties"/>
</bean>

注意:其中lazy-init="false",和 <property name="autoStartup" value="true"/>最好按照自己的需求作统一的配置

二、quartz.properties 配置持久化的信息

# Default Properties file for use by StdSchedulerFactory
# to create a Quartz Scheduler Instance, if a different
# properties file is not explicitly specified.
#
#============================================================================
# Configure Main Scheduler Properties
#============================================================================
org.quartz.scheduler.instanceName = DefaultQuartzScheduler
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 = 10
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true
#============================================================================
# Configure JobStore
#============================================================================
#org.quartz.scheduler.classLoadHelper.class=org.quartz.simpl.CascadingClassLoadHelper
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.useProperties = true
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#havent cluster spring
#org.quartz.jobStore.dataSource = myDS
org.quartz.jobStore.tablePrefix = qrtz_
org.quartz.jobStore.isClustered = false
org.quartz.jobStore.maxMisfiresToHandleAtATime=1
#==============================================================
#Non-Managed Configure Datasource if you don't use spring cluster
#==============================================================
#org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver
#org.quartz.dataSource.myDS.URL = jdbc\:mysql\://localhost\:3306/qrtz
#org.quartz.dataSource.myDS.user = root
#org.quartz.dataSource.myDS.password = root
#org.quartz.dataSource.myDS.maxConnections =10

这里要备注的是:org.quartz.jobStore.misfireThreshold = 60000

  这个属性是配置当发现触发器过时,允许做就之内的trigger任有效。对于详细了解其中的机制,可以查看数据库的变化,特别是数据库中的QRTZ_FIRED_TRIGGERS表。

  对于其状态有ACQUIRED和EXECUTING两个状态进行转化,并因此会影响QRTZ_TRIGGERS的表的数据。(个人猜想:quartz是利用这几个字段来保存断点的)

三、下面是项目的搭建代码:

  任务类信息类JobModel

public class JobModel {
private String jobName;
private String group;//对于job,trigger相互绑定的,采用相同的group
private Class<?> jobClass;
private Trigger trigger;
public JobModel() {
super();
}
//.....getter,setter
}

定义一个无状态的Job(若定义一个有状态的Job,需要继承与StatefulMethodInvokingJob)

public class CommonJob extends MethodInvokingJob{
@Override
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
System.out.println("commonJob executing ...");
JobDataMap dataMap = context.getJobDetail().getJobDataMap();
int value = Integer.parseInt(dataMap.getString("key"));
System.out.println("value is " + value);
dataMap.put("key", new Integer(++value).toString()); }
}

SchedulerManager一共对在scheduler中操作job

public class QrtzManager {
private static Scheduler scheduler;
static {
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext-quartz2.xml");
scheduler = (StdScheduler) context.getBean("quartzScheduler");
}
public void standBy(){
try {
scheduler.standby();
} catch (SchedulerException e) {
e.printStackTrace();
}
}
public void start(){
try {
scheduler.start();
} catch (SchedulerException e) {
e.printStackTrace();
}
}
//add a job
public void deploy(JobModel model,JobDataMap dataMap){
JobDetail jobDetail = new JobDetail(model.getJobName(),model.getGroup(),model.getJobClass());
jobDetail.setJobDataMap(dataMap);
try {
scheduler.scheduleJob(jobDetail,model.getTrigger());
} catch (SchedulerException e) {
e.printStackTrace();
}
}
//delete a job
public void unDeploy(String jobName,String group){
if (jobName.equals("") || group.equals("")) {
return ;
}
try {
scheduler.deleteJob(jobName, group);
} catch (SchedulerException e) {
e.printStackTrace();
} }
}

测试类

package com.lps.schedulerserver.service02;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; import org.junit.Test;
import org.quartz.CronTrigger;
import org.quartz.JobDataMap;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger; public class Main {
public static void main(String[] args) { Trigger trigger = new SimpleTrigger("trigger02", "group02", new Date(), parse("2012-12-12 18:30:00"), 5, 10000L);
JobModel model1 = new JobModel("job01", "group01", CommonJob.class, trigger);
String expression = "0/15 * * ? * *";
Trigger trigger2 = null;
try {
trigger2 = new CronTrigger("trigger01", "group", expression);
trigger2.setStartTime(new Date());
} catch (ParseException e) {
e.printStackTrace();
}
JobModel model2 = new JobModel("job02","group02",StatefulJob.class,trigger2);
QrtzManager manager = new QrtzManager();
//jobdatamap
JobDataMap dataMap = new JobDataMap();
dataMap.put("key", "1");
manager.deploy(model2, dataMap);
System.out.println("deploy model2 at:"+new Date());
}
public static Date parse(String str){
DateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
Date date = null ;
try {
date = format.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
@Test
public void deleteJob(){
QrtzManager manager = new QrtzManager();
manager.unDeploy("job01","group01");
}
}

本文转自:http://www.cnblogs.com/interdrp/p/3551446.html

Spring整合Quartz实现持久化、动态设定时间的更多相关文章

  1. spring整合quartz并持久化

    spring整合quartz有两种方式: 一.常见是使用配置文件,将定时任务保存到内存中 简单示例: <!-- 短信催还提醒任务调度 --> <bean id="overd ...

  2. spring整合quartz时间任务调度框架

    spring整合quartz框架 1.创建maven工程 2.导入jar包(pom.xml) <dependencies> <dependency> <groupId&g ...

  3. Spring整合Quartz定时任务 在集群、分布式系统中的应用(Mysql数据库环境)

    Spring整合Quartz定时任务 在集群.分布式系统中的应用(Mysql数据库环境)   转载:http://www.cnblogs.com/jiafuwei/p/6145280.html 单个Q ...

  4. 初识quartz 并分析 项目中spring整合quartz的配置【原创+转载】

    初识quartz 并分析 项目中spring整合quartz的配置[原创+转载]2018年01月29日 12:08:07 守望dfdfdf 阅读数:114 标签: quartz 更多个人分类: 工具 ...

  5. 使用Spring整合Quartz轻松完成定时任务

    一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...

  6. Spring quartz Job不能依赖注入,Spring整合quartz Job任务不能注入

    Spring quartz Job不能依赖注入,Spring整合quartz Job任务不能注入 Spring4整合quartz2.2.3中Job任务使用@Autowired不能注入 >> ...

  7. Spring整合Quartz分布式调度

    前言 为了保证应用的高可用和高并发性,一般都会部署多个节点:对于定时任务,如果每个节点都执行自己的定时任务,一方面耗费了系统资源,另一方面有些任务多次执行,可能引发应用逻辑问题,所以需要一个分布式的调 ...

  8. Spring整合Quartz分布式调度(山东数漫江湖)

    前言 为了保证应用的高可用和高并发性,一般都会部署多个节点:对于定时任务,如果每个节点都执行自己的定时任务,一方面耗费了系统资源,另一方面有些任务多次执行,可能引发应用逻辑问题,所以需要一个分布式的调 ...

  9. Spring整合Quartz (cronTrigger和simpleTrigger实现方法)

    Spring整合Quartz (cronTrigger和simpleTrigger实现方法) 之前有记录过一次springboot整合Quartz的文章,由于偶尔一次自己使用spring需要整合Qua ...

随机推荐

  1. 【云计算】Docker Nginx示例

    使用数据卷容器,配置Nginx Docker作为静态文件服务器 . 该方法是直接使用命令行,当然也可使用Dockerfile文件进行创建. 其实,使用docker创建nginx容器是很简单的,但要和数 ...

  2. 【OpenStack】OpenStack系列10之Horizon详解

    一.参考其他资料即可.可以采用haproxy+apache+horizon方式部署,haproxy/httpd支持ssl.

  3. Trailing Zeros

    Write an algorithm which computes the number of trailing zeros in n factorial. Have you met this que ...

  4. poj 1318

    http://poj.org/problem?id=1318 这个题目还是比较水的,不过也可以提升你对字符串的熟悉度以及对一些排序函数和字符函数的使用. 大概的题意就是给你一个字典,这个字典有一些单词 ...

  5. css 之盒子模型

    display: none  inline   block float :none   right   left clear  left   right  both    清除浮动  是清楚它之前的左 ...

  6. iOS 中使用类别简化代码开发

    最近需要一个函数,把CLLocation对象转化为NSDictionary,按照我以前的想法,我会写一个工具类,之后添加一个函数,类似这样 - (NSDictionary *)turnLocation ...

  7. jQuery操作复选框的简单使用

    开发中为了实现一个小功能,就是复选框的相互影响事件,如下图: 就是通过复选框设置权限,权限是分等级的,这是一个web管理系统的应用,一个管理员具有三个权限赋予,权限也是有等级的,其中删除和编辑权限相当 ...

  8. 使用 TRegistry 类[1]: 显示各主键下的项

    使用 TRegistry 类[1]: 显示各主键下的项 {XP 注册表中的主键} HKEY_CLASSES_ROOT    {文件类型信息} HKEY_CURRENT_USER    {当前用户信息} ...

  9. Java for LeetCode 166 Fraction to Recurring Decimal

    Given two integers representing the numerator and denominator of a fraction, return the fraction in ...

  10. Heap:Expedition(POJ 2431)

    远征队 题目大意:一部车要从一个地方走到另一个地方,开始的时候车的油箱有P升油,汽车每走1个距离消耗1升油,没有油汽车无法行驶,路上有加油站,可以为汽车加油,设汽车的油缸是无限大小的,问你汽车能否走到 ...