spring定时器
spring定时器
最近正在做一个月报告自动发布的项目,要求根据客户端的设置动态修改定时时间,如客户可以设置 每个月的2号自动生成月报告,就用到了spring定时器+quartz。在此感谢http://blog.sina.com.cn/s/blog_49742bac0101fg67.html以及https://blog.csdn.net/gy40235/article/details/41085601和https://www.cnblogs.com/huaxingtianxia/p/5931812.html对我写这篇文章的支持。
用到的spring是4,quartz2.2.3
我单独写了一个定时器的配置文件:applicationContext-time.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:task=”http://www.springframework.org/schema/task”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:context=”http://www.springframework.org/schema/context”
xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd”>
<!– 被执行类 –>
<bean id=”timer” class=”com.walkersoft.bigdata.timer.Timer”>
<!– <property name=”scheduler” ref=”schedulerFactory”></property> –>
</bean>
<!– 将类注入到job中 –>
<bean id=”timeJob” class=”org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean”>
<property name=”targetObject” ref=”timer”/>
<property name=”targetMethod” value=”myTast” />
<property name=”concurrent” value=”false” />
</bean>
<!– job注入到定时触发器 –>
<bean id=”timeTrigger” class=”org.springframework.scheduling.quartz.CronTriggerFactoryBean”>
<property name=”jobDetail” ref =”timeJob” />
<property name=”cronExpression”>
<value>0 0 10 * * ?</value>
</property>
</bean>
<!– 总管理类 将触发器注入到任务工程 –>
<bean id=”schedulerFactory” lazy-init=”true” autowire=”no” class=”org.springframework.scheduling.quartz.SchedulerFactoryBean”>
<property name=”triggers”>
<list >
<ref bean=”timeTrigger”/>
</list>
</property>
</bean>
<!– 动态设置定时时间的类 –>
<bean id=”setTimer” class=”com.walkersoft.bigdata.timer.setTimer” lazy-init=”false” init-method=”resetJob”>
<property name=”scheduler” ref=”schedulerFactory”></property>
</bean>
</beans>