基于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); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?