Spring定时器的应用
Spring定时器的使用
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "spring-beans.dtd" > <beans> <!-- <bean id="reportManagerImpl" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass" ref="reportManager" /> </bean> --> <bean id="reportManagerImpl_month" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="reportManager" /> <property name="targetMethod" value="addMonthReport" /> </bean> <bean id="reportManagerImpl_week" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="reportManager" /> <property name="targetMethod" value="addWeekReport" /> </bean> <bean id="reportManagerImpl_xun" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="reportManager" /> <property name="targetMethod" value="addXunReport" /> </bean> <!-- 旬报生成提醒 --> <bean id="checkXunAndMail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="reportManager" /> <property name="targetMethod" value="checkXunAndMail" /> </bean> <bean id="monthReportTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="reportManagerImpl_month" /> <!-- 关键在配置此表达式 --> <property name="cronExpression"> <value>0 0 17 1C * ?</value> <!--每月的第一天的17:00触发 0 0 17 1C * ? --> </property> </bean> <bean id="weekReportTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="reportManagerImpl_week" /> <!-- 关键在配置此表达式 --> <property name="cronExpression"> <!--每周一的00:00触发 0 0 00 ? * MON --> <value>0 0 0 ? * MON</value> </property> </bean> <bean id="xunReportTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="reportManagerImpl_xun" /> <!-- 关键在配置此表达式 --> <property name="cronExpression"> <!--每两个周周末的00:00触发0 0 00 ? * MON/2--> <value>0 0 0 ? * MON/2</value> </property> </bean> <!-- 旬报生成提醒 --> <bean id="xunReportMailTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="checkXunAndMail" /> <!-- 关键在配置此表达式 --> <property name="cronExpression"> <!--每周一的10:00触发0 0 10 ? * MON--> <value>0 0 10 ? * MON</value> </property> </bean> <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="monthReportTrigger" /> <ref bean="weekReportTrigger" /> <!-- <ref bean="xunReportTrigger" /> --> <ref bean="xunReportMailTrigger" /> </list> </property> </bean> </beans>
以红色代码为例说明。Spring自带一个定时器,通过某种格式设置日期,然后执行对应类的对应方法。
从下到上来看:scheduler用来执行所有的触发器也就是说把配置好的触发器拿来执行,至于每个触发器是什么样的属性?往上看,第二段红色代码,这里配置了一个触发器,触发中有触发时间的配置和触发对象的配置。触发时间的配置这里不说了,大家查资料可以找到的。触发对象的配置为:reportManagerImpl_month,至于这个对象对应了什么属性。再往上看,第一段红色代码。<property name="targetObject" ref="reportManager" /><property name="targetMethod" value="addMonthReport" />这里,targetObject指的是触发对象所在的类reportManager。当然了,这个类已经在spring中注册了,这里直接把bean的id拿过来用就可以了。targetMethod指的是触发的方法,当触发条件符合时,就会自动执行addMonthReport方法。
这里需要注意的事情:
1,三层或者说是四层结构,一定要分开来写,不止要符合语法结构,也使思路更加的清晰。
2,时间的填写,要多测试多查资料,不能想当然尔。
3,targetObject要在spring中注册,以上这个文件一定要在配置文件中导入。这是常识。