spring quartz定时器配置
- spring.xml配置
<!-- job start--> <!-- 使用MethodInvokingJobDetailFactoryBean,任务类可以不实现Job接口,通过targetMethod指定调用方法--> <!-- 定时任务类 --> <bean id="taskJob" class="com.voole.ad.http.ser.LoadTaskSer"/> <!-- 定义调用对象和调用对象的方法 --> <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="group" value="job_work"/> <property name="name" value="job_work_name"/> <!--false表示等上一个任务执行完后再开启新的任务--> <property name="concurrent" value="false"/> <property name="targetObject"> <ref bean="taskJob"/> </property> <property name="targetMethod"> <value>initTask</value> </property> </bean> <!-- 调度触发器 --> <bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="name" value="work_default_name"/> <property name="group" value="work_default"/> <property name="jobDetail"> <ref bean="jobDetail" /> </property> <property name="cronExpression"> <value>0/5 * * * * ?</value> </property> </bean> <!-- 调度工厂 可配置多个触发器 --> <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="myTrigger"/> </list> </property> </bean> <!-- job end-->
2.创建LoadTaskSer.java
public class LoadTaskSer { public void initTask() throws Exception { //do someThing 实现具体操作 System.out.println("begin start 定时任务 时间为:"+new Date()); } }
3.web.xml配置服务启动
<init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-servlet.xml</param-value> </init-param>
4.控制台输出结果如下:
begin start 定时任务 时间为:Wed Oct 19 15:03:15 CST 2016 begin start 定时任务 时间为:Wed Oct 19 15:03:20 CST 2016 begin start 定时任务 时间为:Wed Oct 19 15:03:25 CST 2016 begin start 定时任务 时间为:Wed Oct 19 15:03:30 CST 2016 begin start 定时任务 时间为:Wed Oct 19 15:03:35 CST 2016 begin start 定时任务 时间为:Wed Oct 19 15:03:40 CST 2016 begin start 定时任务 时间为:Wed Oct 19 15:03:45 CST 2016 begin start 定时任务 时间为:Wed Oct 19 15:03:50 CST 2016 begin start 定时任务 时间为:Wed Oct 19 15:03:55 CST 2016