Spring Quartz 持久化解决方案
Quartz是实现了序列化接口的,包括接口,所以可以使用标准方式序列化到数据库。
而Spring2.5.6在集成Quartz时却未能考虑持久化问题。
Spring对JobDetail进行了封装,却未实现序列化接口,所以持久化的时候会产生NotSerializable问题,这也是网上一直在那边叫嚣为什么不能持久化到数据库问题,哥今天看了下Spring源码,发现Spring对Quartz持久化的问题.
1. 不知道Spring未来会不会对持久化的支持,不过我们可以有如下解决方案,比如改写
Spring的代码,实现序列化接口.
2. 不使用Spring的Fatory,自己实现任务的初始化.
既然Spring不支持持久化,那么持久化任务还是自己编写实现吧,否则每次都需要打包发布,麻烦,自己编写的类与Quartz完全兼容.
注意:为什么Spring不支持外配置任务,可能也是考虑到这方面问题所以才不提供这些任务的执行化支持.[配置文件配置与数据库配置重复]
直接使用Quartz是支持序列化功能,比如直接使用页面配置Quartz界面,设置任务执行时间等属性。
通过配置实现的是不应该初始化到数据库,否则直接在数据库中配置了。不过也是可以配置的,通过改写JobDetailBean.代码如下:
- package org.frame.auth.service;
- import java.util.Map;
- import org.quartz.Job;
- import org.quartz.JobDetail;
- import org.quartz.Scheduler;
- import org.springframework.beans.factory.BeanNameAware;
- import org.springframework.beans.factory.InitializingBean;
- import org.springframework.scheduling.quartz.DelegatingJob;
- import org.springframework.scheduling.quartz.SchedulerFactoryBean;
- public class PersistentJobDetailBean extends JobDetail
- implements BeanNameAware, InitializingBean {
- private static final long serialVersionUID = -4389885435844732405L;
- private Class actualJobClass;
- private String beanName;
- /**
- * Overridden to support any job class, to allow a custom JobFactory
- * to adapt the given job class to the Quartz Job interface.
- * @see SchedulerFactoryBean#setJobFactory
- */
- public void setJobClass(Class jobClass) {
- if (jobClass != null && !Job.class.isAssignableFrom(jobClass)) {
- super.setJobClass(DelegatingJob.class);
- this.actualJobClass = jobClass;
- }
- else {
- super.setJobClass(jobClass);
- }
- }
- /**
- * Overridden to support any job class, to allow a custom JobFactory
- * to adapt the given job class to the Quartz Job interface.
- */
- public Class getJobClass() {
- return (this.actualJobClass != null ? this.actualJobClass : super.getJobClass());
- }
- /**
- * Register objects in the JobDataMap via a given Map.
- * <p>These objects will be available to this Job only,
- * in contrast to objects in the SchedulerContext.
- * <p>Note: When using persistent Jobs whose JobDetail will be kept in the
- * database, do not put Spring-managed beans or an ApplicationContext
- * reference into the JobDataMap but rather into the SchedulerContext.
- * @param jobDataAsMap Map with String keys and any objects as values
- * (for example Spring-managed beans)
- * @see SchedulerFactoryBean#setSchedulerContextAsMap
- */
- public void setJobDataAsMap(Map jobDataAsMap) {
- getJobDataMap().putAll(jobDataAsMap);
- }
- /**
- * Set a list of JobListener names for this job, referring to
- * non-global JobListeners registered with the Scheduler.
- * <p>A JobListener name always refers to the name returned
- * by the JobListener implementation.
- * @see SchedulerFactoryBean#setJobListeners
- * @see org.quartz.JobListener#getName
- */
- public void setJobListenerNames(String[] names) {
- for (int i = 0; i < names.length; i++) {
- addJobListener(names[i]);
- }
- }
- public void setBeanName(String beanName) {
- this.beanName = beanName;
- }
- public void afterPropertiesSet() {
- if (getName() == null) {
- setName(this.beanName);
- }
- if (getGroup() == null) {
- setGroup(Scheduler.DEFAULT_GROUP);
- }
- }
- }
这里把Spring的ApplicationContext去掉了,因为这个属性没有实现序列化接口。其他配置与原告一致:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" " http://www.springframework.org/dtd/spring-beans.dtd ">
- <beans default-autowire="byName">
- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">
- <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
- <property name="url" >
- <value><![CDATA[jdbc:mysql://localhost:3306/txl?connectTimeout=1000&useUnicode=true&characterEncoding=utf-8]]></value>
- </property>
- <property name="username" value="root"/>
- <property name="password" value=""/>
- </bean>
- <bean id="jobDetail" class = "org.frame.auth.service.PersistentJobDetailBean">
- <property name="jobClass" value="org.frame.auth.service.PersistentJob"></property>
- </bean>
- <!-- <bean id="trigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean" >-->
- <!-- <property name="jobDetail" ref="jobDetail"></property>-->
- <!-- <property name="startDelay" value="1000"></property>-->
- <!-- <property name="repeatInterval" value="3000"></property>-->
- <!-- <property name="jobDataAsMap">-->
- <!-- <map>-->
- <!-- <entry key="message" value="this is trigger"></entry>-->
- <!-- </map>-->
- <!-- </property>-->
- <!-- </bean>-->
- <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean" >
- <property name="jobDetail" ref="jobDetail"/>
- <property name="cronExpression">
- <value>0/10 * * * * ?</value>
- </property>
- </bean>
- <bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
- <property name="dataSource" ref="dataSource"></property>
- <property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
- <property name="configLocation" value="classpath:quartz.properties"/>
- </bean>
- </beans>
org.frame.auth.service.PersistentJob这个类很简单,如下:
- package org.frame.auth.service;
- import org.quartz.Job;
- import org.quartz.JobExecutionContext;
- import org.quartz.JobExecutionException;
- public class PersistentJob implements Job {
- @Override
- public void execute(JobExecutionContext context) throws JobExecutionException {
- System.out.println("spring quartz!");
- }
- }
有人可能会说,你这种任务调度持久化就没有意义了,是的,一般持久化到数据库的代码如下:
- package org.frame.auth.service;
- import java.util.Map;
- import org.quartz.JobExecutionContext;
- import org.quartz.JobExecutionException;
- import org.quartz.StatefulJob;
- public class PersistentJob implements StatefulJob {
- @Override
- public void execute(JobExecutionContext context) throws JobExecutionException {
- // TODO Auto-generated method stub
- Map map = context.getJobDetail().getJobDataMap();
- System.out.println("["+context.getJobDetail().getName()+"]"+map.get("message"));
- map.put("message", "updated Message");
- }
- }
这样的话,信息message就会持久化到数据库中了.可以建立系统的连锁调度,这根据你的业务需求了.
在Spring中配置的任务通过我这种修改是可以运行,不过每次运行都需要把原先的任务删除,否则会提示任务已经存在,Quartz的优势是就算服务器停止,下次重启能够恢复原先的任务并继续执行.
Spring Quartz 持久化解决方案的更多相关文章
- 基于spring+quartz的分布式定时任务框架
问题背景 我公司是一个快速发展的创业公司,目前有200人,主要业务是旅游和酒店相关的,应用迭代更新周期比较快,因此,开发人员花费了更多的时间去更=跟上迭代的步伐,而缺乏了对整个系统的把控 没有集群之前 ...
- Spring+Quartz 集群
这几天给Spring+Quartz的集群折腾得死去活来,google了无数页总算搞定,记下一些要点备以后使用. 单独的Quartz集群在http://unmi.blogjava.net/有Unmi翻译 ...
- Spring+quartz 实现定时任务job集群配置
为什么要有集群定时任务? 因为如果多server都触发相同任务,又同时执行,那在99%的场景都是不适合的.比如银行每晚24:00都要汇总营业额.像下面3台server同时进行汇总,最终计算结果可能是真 ...
- Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群
Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群 >>>>>>>>>>>>>> ...
- Spring+quartz 实现定时任务job集群配置【原】
为什么要有集群定时任务? 因为如果多server都触发相同任务,又同时执行,那在99%的场景都是不适合的.比如银行每晚24:00都要汇总营业额.像下面3台server同时进行汇总,最终计算结果可能是真 ...
- 【示例】Spring Quartz入门
JAVA 针对定时任务,有 Timer,Scheduler, Quartz 等几种实现方式,其中最常用的应该就是 Quartz 了. 一. Quartz的基本概念 在开始之前,我们必须了解以下的几个基 ...
- Spring quartz Job不能依赖注入,Spring整合quartz Job任务不能注入
Spring quartz Job不能依赖注入,Spring整合quartz Job任务不能注入 Spring4整合quartz2.2.3中Job任务使用@Autowired不能注入 >> ...
- Spring+quartz集群解决多服务器部署定时器重复执行的问题
一.问题描述 Spring自带的Task虽然能很好使用定时任务,只需要做些简单的配置就可以了.不过如果部署在多台服务器上的时候,这样定时任务会在每台服务器都会执行,造成重复执行. 二.解决方案 Spr ...
- spring + quartz 分布式自定义注解
相关技术 本文采用spring + quartz的方案.使用mysql作为任务的持久化,支持分布式. 自定义注解 1.启用定时任务 @Target(ElementType.TYPE) @Retenti ...
随机推荐
- C++有super关键字么?
很多人在学习Java之后,看到Java里面有super关键字,用来表示父类,那么C++里面有super关键字么? 答案是否定的.这也很容易理解,C++由于支持多继承,所以假设存在super关键字,那么 ...
- CodeIgniter2.0中sqlserver驱动返回受影响行数问题解决
最近使用CI写项目时遇到的问题,当使用sqlserve链接操作时 修改和删除返回的受影响行数不对 解决办法如下: 找到ci框架目录中include\database\drivers\sqlsrv\sq ...
- LoadRunner 一参多用
LoadRunner参数化后的值在脚本中多处位置引用(LoadRunner 一参多用) LoadRunner的参数化给了我们很多便利,但是当一个脚本中同一个值出现多处,并且值都是一致的.这个时候, ...
- nginx配置web服务器
一:设置虚拟服务器 1.设置 http { server { listen 127.0.0.1:8080; server_name example.org www.example.org; } } 2 ...
- 30:最小的K个数
import java.util.ArrayList; import java.util.TreeSet; /** * 面试题30:最小的K个数 * 输入n个整数,找出其中最小的K个数.例如输入4,5 ...
- 洛谷P2671 求和 [数论]
题目传送门 求和 格式难调,题面就不放了. 分析: $ZYYS$的一道题. 很显然是大力推公式.我们分析一下题目,实际上限制条件就是:下标同奇偶且颜色相同的数,那么我们先拿这个公式$(x+z)*(nu ...
- VMware安装CentOS7教程
首先安装VM,VM破解版和激活版的百度有很多,随意下载一个 下载CentOS7 地址1:https://mirrors.btte.net/centos/7/isos/x86_64/ 地址2:http: ...
- 【基础知识】.Net基础加强 第二天
第02天 .Net基础加强 1. 封装 1> 属性的封装: 属性封装字段:把变化封装一下,保留用户的使用方式 2> 把方法的多个参数封装成一个对象 3> 将一堆代码封装到一个方法中 ...
- PAGELATCH_EX Contention on 2:1:103
This blog post is meant to help people troubleshoot page latch contention on 2:1:103. If that’s what ...
- android studio 添加有趣的注释模板 佛祖保佑无bug等
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 汉化包 百度云盘 下载地址:https://pan.baidu.com/s/1pLjwy ...