最近做一个小项目,要每7天去调用webservice获取一次数据。所以就用定时器来完成spring是4.1.6,quartz是2.2.1.

首先配置spring的xml文件。首先定义你要被执行的类

  1. <!-- 被执行类 -->
  2. <bean id="remote" class="com.real.api.utils.InfoQuartz">
  3.  
  4. </bean>

将用来调度的类注入到job中。其中targetMethod是你在被调度的方法。

  1. <!-- 将remoteQuartzJob注入到job中 -->
  2. <bean id="remoteQuartzJob"
  3. class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  4. <property name="targetObject" ref="remote" />
  5. <property name="targetMethod" value="loadJob" />
  6. <property name="concurrent" value="false" />
  7. </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

  1. <!-- 将job注入到定时触发器 -->
  2. <bean id="remoteTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
  3. <property name="jobDetail" ref="remoteQuartzJob" />
  4. <property name="cronExpression">
  5. <!-- <value>20,40,59 * * * * ?</value> -->
  6. <value>59 * * * * ?</value>
  7. </property>
  8. </bean>

接着把触发器注入到工程中。

  1. <!-- 将触发器注入任务工程 -->
  2. <bean id="schedulerFactory"
  3. class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  4. <property name="triggers">
  5. <list>
  6. <ref bean="remoteTrigger" />
  7. </list>
  8. </property>
  9. </bean>

最后在原来被执行的的bean上加一个属性

  1. <bean id="remote" class="com.real.api.utils.InfoQuartz">
  2. <property name="scheduler" ref="schedulerFactory" />
  3. </bean>

xml配置完成。接下来编写被调度的类。要调度的方法写在loadJob()中就行了。

  1. package com.real.api.utils;
  2.  
  3. import org.quartz.Scheduler;
  4. import org.quartz.SchedulerException;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6.  
  7. import com.real.api.servlet.DataProcessing;
  8.  
  9. public class InfoQuartz {
  10. @Autowired
  11. private DataProcessing dataProcessing;
  12.  
  13. public void loadJob() throws SchedulerException{
  14. System.out.println(dataProcessing);
  15. dataProcessing.insertData();
  16. }
  17. }

运行这个工程,能正确跑起来,执行定时调度任务。能正确打印出dataProcessing对象和执行insertData()方法

还有另外一种配置,但是有一点问题,不能自动注入。如果用这个配置,定时调度还是可以执行,但是,不能自动注入。每次打印出来的都是null。看了一下源码,但是还是没找出原因,如果有知道的同学可以提出来。非常感谢

  1. <bean id="remote" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
  2. <property name="jobClass">
  3. <value>com.real.api.utils.RemoteQuartz</value>
  4. </property>
  5. </bean>
  6.  
  7. <bean id="cronReportTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
  8. <property name="jobDetail">
  9. <ref bean="remote"/>
  10. </property>
  11. <property name="cronExpression">
  12. <value>20,40,59 * * ? * *</value>
  13. </property>
  14. </bean>
  15.  
  16. <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  17. <property name="triggers">
  18. <list>
  19. <ref bean="cronReportTrigger"/>
  20. </list>
  21. </property>
  22. </bean
  1. package com.real.api.utils;
  2.  
  3. import javax.annotation.Resource;
  4.  
  5. import org.quartz.JobExecutionContext;
  6. import org.quartz.JobExecutionException;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.scheduling.quartz.QuartzJobBean;
  9. import org.springframework.stereotype.Component;
  10.  
  11. import com.real.api.servlet.DataProcessing;
  12.  
  13. @Component
  14. public class RemoteQuartz extends QuartzJobBean{
  15. @Autowired
  16. private DataProcessing dataProcessing;
  17.  
  18. @Override
  19. protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
  20. /*dataProcessing.insertData();*/
  21. System.out.println("dataProcessing:"+dataProcessing);
  22. }
  23.  
  24. }

基于spring和Quartz定时器的更多相关文章

  1. Spring的quartz定时器同一时刻重复执行二次的问题解决

    最近用Spring的quartz定时器的时候,发现到时间后,任务总是重复执行两次,在tomcat或jboss下都如此. 打印出他们的hashcode,发现是不一样的,也就是说,在web容器启动的时候, ...

  2. Spring的quartz定时器重复执行二次的问题解决

    Spring的quartz定时器同一时刻重复执行二次的问题解决 最近用Spring的quartz定时器的时候,发现到时间后,任务总是重复执行两次,在tomcat或jboss下都如此. 打印出他们的ha ...

  3. 关于Spring的Quartz定时器设定

    在实际的开发业务中经常会遇到定时执行某个任务,如果项目使用的ssh框架的话,就需要配合spring来使用定时器.spring的定时器是一个固定的配置格式,具体的applicationContext配置 ...

  4. 基于spring boot的定时器

    首先,搭建好一个springboot项目 方法一:通过springboot自带入口来开启定时器. 首先我们都知道,springboot有一个自己的入口,也就是@SpringBootApplicatio ...

  5. 基于spring的quartz定时框架,实现简单的定时任务功能

    在项目中,经常会用到定时任务,这就需要使用quartz框架去进行操作. 今天就把我最近做的个人主页项目里面的定时刷新功能分享一下,很简单. 首先需要配置一个配置文件,因为我是基于spring框架的,所 ...

  6. spring启动quartz定时器

    在很多中经常要用到定时任务,quartz是定时器中比较好用的,在Spring中使用quartz是很容易的事情,首先在spring的applicationContext.xml文件中增加如下配置: &l ...

  7. Spring集成Quartz定时器

    <!-- Spring集成Quartz开始 --> <bean id="startQuertz" lazy-init="false" auto ...

  8. 使用spring配置quartz定时器

    quartz是石英钟的意思,所以用这个名字来做定时器的框架名称再适合不过.一年前做项目的时候有用过这个框架,当时没有整理,今天刚好新的商城系统也需要定时器.想要达到的效果是:每天的固定时间,比如凌晨3 ...

  9. Spring整合Quartz定时器

    1.添加jar #此处省略spring核心jar包 <dependency> <groupId>org.quartz-scheduler</groupId> < ...

随机推荐

  1. mysql数据库日期,ip等处理

    一.日期 1.select now(); 查询当前时间,格式为:年-月-日 时:分:秒,如2015-12-17 17:37:20 2.select unix_timestamp(); 将字符串类型的日 ...

  2. centos关闭防火墙

    Centos7 关闭防火墙 CentOS 7.0默认使用的是firewall作为防火墙,使用iptables必须重新设置一下 1.直接关闭防火墙 systemctl stop firewalld.se ...

  3. 手动编译安装LNMP

    yum -y install gcc gcc-c++ autoconf nss_ldap libjpeg libjpeg-devel libpng libpng-devel freetype free ...

  4. 【SIGGRAPH】【最终幻想XV】的战斗场景实时演示的要点解说

    [SIGGRAPH][最终幻想XV]的战斗场景实时演示的要点解说 原文:西川善司 http://www.4gamer.net/games/999/G999902/20160730004/        ...

  5. 关于Apache/Tomcat/JBOSS/Neginx/lighttpd/Jetty等一些常见服务器的区别比较和理解

    先说Apache和Tomcat的区别: Apache是世界使用排名第一的Web服务器软件.它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一. ...

  6. org.springframework.web.servlet.DispatcherServlet noHandlerFound

    1 请求URL: http://localhost:8080/mvc/rojas 2 control  RequestMapping  : @RequestMapping(value="xx ...

  7. 《Linux及安全》实践3.1

    <Linux及安全>实践三 ELF格式文件分析 一.基础操作 1.查看大小端.32还是64 由此可以看出,本人实践所用到的是32位Ubuntu,数据存储采用小端法. 2.编写hello.c ...

  8. OC中协议, 类目, 时间, 延展, 属性

    只有继承和协议需要引IMPORT "头文件"; 必须接受marryprotocol协议, id<marryprotocol>基于类型的限定, 才能给实例变量赋值 @pr ...

  9. centos 安装redis并加入系统服务

    1.安装redis wget http://download.redis.io/releases/redis-3.2.5.tar.gz 解压:tar -zxvf redis-3.2.5.tar.gz ...

  10. 去掉tableview顶部留白

    self.automaticallyAdjustsScrollViewInsets = NO;   //去掉tableVIew顶部留白