基于spring和Quartz定时器
最近做一个小项目,要每7天去调用webservice获取一次数据。所以就用定时器来完成spring是4.1.6,quartz是2.2.1.
首先配置spring的xml文件。首先定义你要被执行的类
<!-- 被执行类 -->
<bean id="remote" class="com.real.api.utils.InfoQuartz"> </bean>
将用来调度的类注入到job中。其中targetMethod是你在被调度的方法。
<!-- 将remoteQuartzJob注入到job中 -->
<bean id="remoteQuartzJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="remote" />
<property name="targetMethod" value="loadJob" />
<property name="concurrent" value="false" />
</bean>
再把job注入到触发器中。cronExpression里面的value是调度的时间配置,分别是1.秒(0~59)2.分钟(0~59)3.小时(0~23)4.天(月)(0~31,但是你需要考虑你月的天数)5.月(0~11)6.天(星期)(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)。被注释那个表示每一分钟的第2哦,40,59秒,下面这个表示每一分钟的第59秒。具体配置可参考http://jingyan.baidu.com/article/a3761b2b8e843c1576f9aaac.html
<!-- 将job注入到定时触发器 -->
<bean id="remoteTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="remoteQuartzJob" />
<property name="cronExpression">
<!-- <value>20,40,59 * * * * ?</value> -->
<value>59 * * * * ?</value>
</property>
</bean>
接着把触发器注入到工程中。
<!-- 将触发器注入任务工程 -->
<bean id="schedulerFactory"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="remoteTrigger" />
</list>
</property>
</bean>
最后在原来被执行的的bean上加一个属性
<bean id="remote" class="com.real.api.utils.InfoQuartz">
<property name="scheduler" ref="schedulerFactory" />
</bean>
xml配置完成。接下来编写被调度的类。要调度的方法写在loadJob()中就行了。
package com.real.api.utils; import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired; import com.real.api.servlet.DataProcessing; public class InfoQuartz {
@Autowired
private DataProcessing dataProcessing; public void loadJob() throws SchedulerException{
System.out.println(dataProcessing);
dataProcessing.insertData();
}
}
运行这个工程,能正确跑起来,执行定时调度任务。能正确打印出dataProcessing对象和执行insertData()方法
还有另外一种配置,但是有一点问题,不能自动注入。如果用这个配置,定时调度还是可以执行,但是,不能自动注入。每次打印出来的都是null。看了一下源码,但是还是没找出原因,如果有知道的同学可以提出来。非常感谢
<bean id="remote" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass">
<value>com.real.api.utils.RemoteQuartz</value>
</property>
</bean> <bean id="cronReportTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="remote"/>
</property>
<property name="cronExpression">
<value>20,40,59 * * ? * *</value>
</property>
</bean> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronReportTrigger"/>
</list>
</property>
</bean
package com.real.api.utils; import javax.annotation.Resource; import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component; import com.real.api.servlet.DataProcessing; @Component
public class RemoteQuartz extends QuartzJobBean{
@Autowired
private DataProcessing dataProcessing; @Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
/*dataProcessing.insertData();*/
System.out.println("dataProcessing:"+dataProcessing);
} }
基于spring和Quartz定时器的更多相关文章
- Spring的quartz定时器同一时刻重复执行二次的问题解决
最近用Spring的quartz定时器的时候,发现到时间后,任务总是重复执行两次,在tomcat或jboss下都如此. 打印出他们的hashcode,发现是不一样的,也就是说,在web容器启动的时候, ...
- Spring的quartz定时器重复执行二次的问题解决
Spring的quartz定时器同一时刻重复执行二次的问题解决 最近用Spring的quartz定时器的时候,发现到时间后,任务总是重复执行两次,在tomcat或jboss下都如此. 打印出他们的ha ...
- 关于Spring的Quartz定时器设定
在实际的开发业务中经常会遇到定时执行某个任务,如果项目使用的ssh框架的话,就需要配合spring来使用定时器.spring的定时器是一个固定的配置格式,具体的applicationContext配置 ...
- 基于spring boot的定时器
首先,搭建好一个springboot项目 方法一:通过springboot自带入口来开启定时器. 首先我们都知道,springboot有一个自己的入口,也就是@SpringBootApplicatio ...
- 基于spring的quartz定时框架,实现简单的定时任务功能
在项目中,经常会用到定时任务,这就需要使用quartz框架去进行操作. 今天就把我最近做的个人主页项目里面的定时刷新功能分享一下,很简单. 首先需要配置一个配置文件,因为我是基于spring框架的,所 ...
- spring启动quartz定时器
在很多中经常要用到定时任务,quartz是定时器中比较好用的,在Spring中使用quartz是很容易的事情,首先在spring的applicationContext.xml文件中增加如下配置: &l ...
- Spring集成Quartz定时器
<!-- Spring集成Quartz开始 --> <bean id="startQuertz" lazy-init="false" auto ...
- 使用spring配置quartz定时器
quartz是石英钟的意思,所以用这个名字来做定时器的框架名称再适合不过.一年前做项目的时候有用过这个框架,当时没有整理,今天刚好新的商城系统也需要定时器.想要达到的效果是:每天的固定时间,比如凌晨3 ...
- Spring整合Quartz定时器
1.添加jar #此处省略spring核心jar包 <dependency> <groupId>org.quartz-scheduler</groupId> < ...
随机推荐
- oracle PL/SQL高级特性
触发器:存放在数据库中,并被隐含执行的存储过程. 由触发事件,触发条件,触发操作组成. DML触发器:指定触发器时机(before or after),触发事件(insert , delete, u ...
- div span
无牵无挂,不带任何样式,因此经常使用div完成整体样式的构建,span完成细微样式的构建. div为块级元素,span为行内元素. 使用div完成显示区域的居中.左右浮动等,完成整体的样式布局,然后在 ...
- java基础总结——基础语法1
一.标示符 二.关键字 三.java基本数据类型 3.1 常量 ● 常量表示不能改变的数值. ● java中常量的分类: 1. 整数常量.所有整数 2. 小数常量.所有小时 3. 布尔型常量.较为特有 ...
- angular模块和组件之间传递信息和操作流程的方法(笔记)
angular的模块之间,以及controller.directive等组件之间,是相对独立的,用以实现解耦合. 为实现相互之间传递信息及操作流程,有以下一些机制: 1.事件机制: $scope.$b ...
- input range样式更改,模拟滑块
input range 样式更改,js模拟滑块实时更新数据. 效果图: html 代码: <div> <span class="slider"></s ...
- 项目在vs中打开后识别不出来ashx页面的解决方法
在web.config配置文件中指定页面版本 <add key="webPages:Version" value="2.0"/> <?xml ...
- Issue 1:sigmod 撞车
11.6晚22:40,距离论文截止还有5天.在最后的紧要关头,竟然发现学术上撞车了,非常戏剧性的一幕,这么狗血的事情尽然就这么发生了. 自2015年8月份以来,本人一直在研究快速检查点领域.最近一篇t ...
- Redis: OOM command not allowed when used memory > ‘maxmemory’
现象 日志里出现异常: OOM command not allowed when used memory > 'maxmemory' 原因 内存已满,不允许再存数据了,可以通过redis-cli ...
- ios移动端部分手机不支持background-attachment: fixed 的解决办法
ios系统和某些移动端background-attachment:fixed不兼容性,没有任何效果,但可以hack一下就可以了,代码如下: ps:想在哪个标签加背景,可以在它class后:before ...
- [Bug] 解决透明 Activity 在 Android 6.0 背景不透明
如何复现 连续启动两个 Activity ,其中 Activity 1 为 不透明 的 Activity Activity 2 为 透明 的 Activity 通常用于引导页面,例如:豌豆夹锁屏引导用 ...