最近在开发里需要做一个定时器的功能,即到了某一特定时间执行自己所写的方法:
1:配置quartz-job.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <bean id="downVodJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="ftpmediaTask"/> <property name="targetMethod" value="downVod"/> </bean> <bean id="downVodJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="downVodJob"/> <property name="cronExpression" value="0 35 09 * * ?" /> </bean> <bean id="redVodJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="ftpreadTask"/> <property name="targetMethod" value="redWriteVod"/> </bean> <bean id="redVodJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="redVodJob"/> <property name="cronExpression" value="0 02 10 * * ?" /> </bean> <bean name="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="downVodJobTrigger" /> <ref bean="redVodJobTrigger" /> </list> </property> </bean> </beans>
1):引用两个bean,一个下载的定时任务,一个读取的定时任务(不一定非两个,视业务而定)
<bean name="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="downVodJobTrigger" /> <ref bean="redVodJobTrigger" /> </list> </property> </bean>2):以配置读取的定时任务为例:ref指向为redVodJob的bean
第一个property:对应你写的类,第二个对应你的方法。
bean为cronExpression的value即为配置时间。0 02 10 代表10点02分0秒
<bean id="redVodJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="redVodJob"/> <property name="cronExpression" value="0 02 10 * * ?" /> </bean>
<bean id="redVodJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="ftpreadTask"/> <property name="targetMethod" value="redWriteVod"/> </bean>
3):贴上自己写的方法要使用@service注解
@Service public class FtpreadTask { @Resource private ContentDao contentDao; @Resource private PictureDao pictureDao; @Resource private MultirateDao multirateDao; //读取影视资源 @SuppressWarnings({ "unused", "resource" }) public void checkOocpTxt(String files) throws IOException { } public void redWriteVod() {
checkOocpTxt();}}4:配置好的quartz.xml文件一定要在springmvc里面引入
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <context:component-scan base-package="com.hzqy"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <context:annotation-config /> <import resource="quartz-job.xml" /> </beans>