java Quartz定时器任务与Spring 的实现
1.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="quartzProperties">
<props>
<prop key="org.quartz.scheduler.instanceName">CRMscheduler</prop>
<prop key="org.quartz.scheduler.instanceId">AUTO</prop>
<!-- 线程池配置 -->
<prop key="org.quartz.threadPool.class">org.quartz.simpl.SimpleThreadPool</prop>
<prop key="org.quartz.threadPool.threadCount">3</prop>
<prop key="org.quartz.threadPool.threadPriority">5</prop>
<!-- JobStore 配置 -->
<prop key="org.quartz.jobStore.class">org.quartz.impl.jdbcjobstore.JobStoreTX</prop>
<!-- 集群配置 -->
<prop key="org.quartz.jobStore.isClustered">true</prop>
<prop key="org.quartz.jobStore.clusterCheckinInterval">15000</prop>
<prop key="org.quartz.jobStore.maxMisfiresToHandleAtATime">1</prop>
<!-- 数据源配置 使用DBCP连接池 数据源与dataSource一致 -->
<prop key="org.quartz.jobStore.dataSource">myDS</prop>
<prop key="org.quartz.dataSource.myDS.driver">${db.driverClassName}</prop>
<prop key="org.quartz.dataSource.myDS.URL">${db.quartz_park.url}</prop>
<prop key="org.quartz.dataSource.myDS.user">${db.username}</prop>
<prop key="org.quartz.dataSource.myDS.password">${db.password}</prop>
<prop key="org.quartz.dataSource.myDS.maxConnections">10</prop>
<prop key="org.quartz.jobStore.misfireThreshold">120000</prop>
</props>
</property>
<property name="schedulerName" value="CRMscheduler" />
<property name="startupDelay" value="30" />
<!-- 自动启动 -->
<property name="autoStartup">
<value>true</value>
</property>
<property name="overwriteExistingJobs">
<value>true</value>
</property>
<property name="triggers">
<list>
<ref local="parkCouponsIssueTrigger"/><!-- 更新优惠券管理的购买表的失效时间字段 -->
<ref local="parkCardOverTimeTrigger"/><!-- 更新超时的卡的信息-->
<ref local="createOverTimeTicketTrigger"/><!-- 创建超时券-->
<ref local="parkCouponsIssueBackupsTrigger"/><!-- 备份优惠券管理的购买表的半年数据-->
</list>
</property>
<property name="applicationContextSchedulerContextKey" value="applicationContext" />
</bean>
<!--定时同步parkCouponsIssue表的数据 start yxz -->
<bean id="parkCouponsIssueTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="parkCouponsIssueJobDetail"/>
</property>
<property name="cronExpression">
<value>0 * * * * ?</value>
</property>
</bean>
<bean id="parkCouponsIssueJobDetail" class="com.ivchat.common.bean.quartz.CommJobDetailBean">
<property name="jobDataAsMap">
<map>
<entry key="targetObject" value="parkCouponsIssueService" />
<entry key="targetMethod" value="updateInvalidTimeData" />
</map>
</property>
<property name="concurrent" value="false" />
</bean>
<!--定时同步communitySynchData表的数据 end -->
<!--定时备份TicketDetails(核销记录)表的数据 start 2017/11/06 yxz-->
<bean id="parkCouponsIssueBackupsTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="parkCouponsIssueBackupsJobDetail"/>
</property>
<property name="cronExpression">
<value>0 0/1 * * * ?</value>
</property>
</bean>
<bean id="parkCouponsIssueBackupsJobDetail" class="com.ivchat.common.bean.quartz.CommJobDetailBean">
<property name="jobDataAsMap">
<map>
<entry key="targetObject" value="ticketDetailsBackupsService" />
<entry key="targetMethod" value="updateInvalidTimeData" />
</map>
</property>
<property name="concurrent" value="false" />
</bean>
<!--定时同步communitySynchData表的数据 end 2017/11/06 yxz-->
<!--定时同步parkCouponsIssue表的数据 start -->
<bean id="parkCardOverTimeTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="parkCardOverTimeJobDetail"/>
</property>
<property name="cronExpression">
<value>0 * * * * ?</value>
</property>
</bean>
<bean id="parkCardOverTimeJobDetail" class="com.ivchat.common.bean.quartz.CommJobDetailBean">
<property name="jobDataAsMap">
<map>
<entry key="targetObject" value="parkMonthCardInfoAction" />
<entry key="targetMethod" value="updateOverTimeCard" />
</map>
</property>
<property name="concurrent" value="false" />
</bean>
<!--定时同步parkCouponsIssue表的数据 start -->
<bean id="createOverTimeTicketTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="createOverTimeTicketJobDetail"/>
</property>
<property name="cronExpression">
<value>0 * * * * ?</value>
</property>
</bean>
<bean id="createOverTimeTicketJobDetail" class="com.ivchat.common.bean.quartz.CommJobDetailBean">
<property name="jobDataAsMap">
<map>
<entry key="targetObject" value="parkDetailsAction" />
<entry key="targetMethod" value="createOverTimeTicket" />
</map>
</property>
<property name="concurrent" value="false" />
</bean>
<!--定时同步communitySynchData表的数据 end -->
</beans>
2.作业类1
package com.ivchat.common.bean.quartz;
import org.springframework.scheduling.quartz.JobDetailBean;
/**
* JOB明细对象
* @author 居里智能
*
*/
public class CommJobDetailBean extends JobDetailBean {
private boolean concurrent = false;
@Override
public void afterPropertiesSet() {
try{
if (concurrent){
this.setJobClass(CommDetailQuartzJobBean.class);
}else{
this.setJobClass(StatefulMethodInvokingJob.class);
}
}catch(Exception ex){
ex.printStackTrace();
}
// TODO Auto-generated method stub
super.afterPropertiesSet();
}
public boolean isConcurrent() {
return concurrent;
}
public void setConcurrent(boolean concurrent) {
this.concurrent = concurrent;
}
public boolean getConcurrent() {
return concurrent;
}
}
3.作业类2
package com.ivchat.common.bean.quartz;
import java.lang.reflect.Method;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.quartz.QuartzJobBean;
import com.ivchat.common.util.SpringUtil;
/**
* JOB类,到时间即时执行,有可能多个JOB并发执行
* @author 居里智能
*
*/
public class CommDetailQuartzJobBean extends QuartzJobBean implements ApplicationContextAware{
private String targetObject;
private String targetMethod;
private ApplicationContext applicationContext;
@Override
public void executeInternal(JobExecutionContext context)
throws JobExecutionException {
Object otargetObject = null;
Method m = null;
try {
otargetObject = applicationContext.getBean(targetObject);
m = otargetObject.getClass().getMethod(targetMethod,
new Class[] {});
m.invoke(otargetObject, new Object[] {});
} catch (Exception e) {
throw new JobExecutionException(e);
}
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
public ApplicationContext getApplicationContext() {
return applicationContext;
}
public String getTargetObject() {
return targetObject;
}
public void setTargetObject(String targetObject) {
this.targetObject = targetObject;
}
public String getTargetMethod() {
return targetMethod;
}
public void setTargetMethod(String targetMethod) {
this.targetMethod = targetMethod;
}
}
java Quartz定时器任务与Spring 的实现的更多相关文章
- java Quartz定时器任务与Spring task定时的几种实现,
java Quartz定时器任务与Spring task定时的几种实现 基于java 的定时任务实现, Quartz 时间详细配置 请查阅 http://www.cnblogs.com/si ...
- java定时器,Spring定时器和Quartz定时器
一.java定时器的应用 其实java很早就有解决定时器任务的方法了,java提供了了类java.util.TimerTask类基于线程的方式来实现定时任务的操作,然后再提供java.util.Tim ...
- Quartz定时器+Spring + @Autowired注入 空指针异常
在Quartz的定时方法里引用@Autowired注入Bean,会报空指针错误 解决办法: 第一种方法:(推荐,简单,亲测可行) 使用@Resource(name="指定要注入的Bean&q ...
- Spring的quartz定时器重复执行二次的问题解决
Spring的quartz定时器同一时刻重复执行二次的问题解决 最近用Spring的quartz定时器的时候,发现到时间后,任务总是重复执行两次,在tomcat或jboss下都如此. 打印出他们的ha ...
- 基于spring和Quartz定时器
最近做一个小项目,要每7天去调用webservice获取一次数据.所以就用定时器来完成spring是4.1.6,quartz是2.2.1. 首先配置spring的xml文件.首先定义你要被执行的类 & ...
- Spring的quartz定时器同一时刻重复执行二次的问题解决
最近用Spring的quartz定时器的时候,发现到时间后,任务总是重复执行两次,在tomcat或jboss下都如此. 打印出他们的hashcode,发现是不一样的,也就是说,在web容器启动的时候, ...
- spring启动quartz定时器
在很多中经常要用到定时任务,quartz是定时器中比较好用的,在Spring中使用quartz是很容易的事情,首先在spring的applicationContext.xml文件中增加如下配置: &l ...
- 实现quartz定时器及quartz定时器原理介绍(转)
一.核心概念 Quartz的原理不是很复杂,只要搞明白几个概念,然后知道如何去启动和关闭一个调度程序即可.1.Job表示一个工作,要执行的具体内容.此接口中只有一个方法void execute(Job ...
- java之定时器任务Timer用法
在项目开发中,经常会遇到需要实现一些定时操作的任务,写过很多遍了,然而每次写的时候,总是会对一些细节有所遗忘,后来想想可能是没有总结的缘故,所以今天小编就打算总结一下可能会被遗忘的小点: 1. pub ...
随机推荐
- Js 动态添加的数据,监听事件监听不到
在开发中遇到这种问题,就是有些数据,比如按钮是动态添加进去的,结果添加事件监听无效,直接写死在页面上是可以的. 这就是很明显的加载先后顺序的问题了. 解决的方法: $(document).ready( ...
- Zookeeper —— 初识
什么是 Zookeeper Zookeeper 是一个开放源代码的分布式协调服务,由雅虎创建,是 Google Chubby 的开源实现: Zookeeper 是典型的分布式数据一致性的解决方案,分布 ...
- Java编程的逻辑 (89) - 正则表达式 (中)
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...
- android手机测试中如何查看内存泄露
(一) 生成.hprof文件生成.hprof 文件的方法有很多,而且Android 的不同版本中生成.hprof 的方式也稍有差别,我使用的版本的是2.1,各个版本中生成.prof 文件的方法请参考: ...
- 【QT】QPixmap在Label中自适应大小铺满
KeepAspectRatio:设置pixmap缩放的尺寸保持宽高比. setScaledContents:设置label的属性scaledContents,这个属性的作用是允许(禁止)label缩放 ...
- 写文件的工具类,输出有格式的文件(txt、json/csv)
import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io. ...
- VS2017 编译Assimp
1. 下载Assimp:http://assimp.sourceforge.net/ 2. 要下载和安装DirectX SDK 安装出现错误,错误代码s1023,解决方法:https://blog.c ...
- Oracle导出数据EXP00106错误
在导出dmp文件的时候(命令:exp 用户名/密码@IP/实例名 file=D:\20180910.dmp log=D:\20180910.log),遇到以下错误: 错误原因: 导出使用的是Orac ...
- 没钱买windows怎么办?
ReactOS是一个与 Windows 环境二进制兼容的操作系统. 同时,他是一款开源.免费的操作系统.
- 【分布式系列】session跨域及单点登录解决方案
Cookie机制 Cookie技术是客户端的解决方案,Cookie就是由服务器发给客户端的特殊信息,而这些信息以文本文件的方式存放在客户端,然后客户端每次向服务器发送请求的时候都会带上这些特殊的信息. ...