spring4 定时任务
一、spring 定时任务
spring 定时任务 ,最好使用quartz 实现。下面我以spring4为例
二、实战(默认)
1、pom配置
<!-- spring time task --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>1.8.4</version> </dependency>
2、bean 配置
<?xml version="1.0" encoding="gb2312"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" default-autowire="byName"> <bean name="myTaskJob" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass" value="com.yuanmeng.spring.TaskDemo" /> <property name="jobDataAsMap"> <map> <entry key="timeout" value="36000" /> </map> </property> </bean> <bean id="myTaskTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail" ref="myTaskJob" /> <property name="startDelay" value="3000" /> <property name="repeatInterval" value="60000" /><!-- 每600秒调度一次 --> </bean>
<!--启动任务--> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="myTaskTrigger" /> </list> </property> </bean> </beans>
3、定时java类, 继承 QuartzJobBean
package com.yuanmeng.spring; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; public class TaskDemo extends QuartzJobBean { private static int i = 0; @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { System.out.println("--------- begin ---------"); System.out.println(" i = " + i++); System.out.println("--------- end ---------"); } }
4、启动
package com.yuanmeng.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.scheduling.quartz.JobDetailBean; public class Client { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring/spring-timetask.xml"); org.springframework.scheduling.quartz.JobDetailBean bean = (JobDetailBean) ac.getBean("myTaskJob"); System.out.println(bean != null); } }
5、看下结果
--------- begin --------- i = 0 --------- end --------- --------- begin --------- i = 1 --------- end --------- --------- begin --------- i = 2 --------- end --------- --------- begin --------- i = 3 --------- end --------- --------- begin --------- i = 4 --------- end --------- --------- begin --------- i = 5 --------- end --------- --------- begin --------- i = 6 --------- end --------- --------- begin --------- i = 7 --------- end --------- --------- begin --------- i = 8 --------- end --------- --------- begin --------- i = 9 --------- end --------- --------- begin --------- i = 10 --------- end --------- --------- begin --------- i = 11 --------- end --------- --------- begin --------- i = 12 --------- end ---------
三、实战(不继承)
1、bean 配置
<bean id="myTask" class="com.yuanmeng.spring.TaskDemo2" /> <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 指定定时任务执行类 --> <property name="targetObject" ref="myTask" /> <!-- 指定定时任务执行方法 --> <property name="targetMethod" value="doIt" /> <property name="concurrent" value="false" /> </bean> <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail" ref="jobDetail" /> <property name="startDelay" value="5000" /> <property name="repeatInterval" value="3000" /> </bean> <!-- 总调度用于启动Spring定时器 --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="simpleTrigger" /> </list> </property> </bean>
2、自定义任务类
package com.yuanmeng.spring; public class TaskDemo2 { private static int i = 0; public void doIt() { System.out.println("---------doIt begin ---------"); System.out.println(" i = " + i++); System.out.println("---------doIt end ---------"); } }
3、启动
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring/spring-timetask2.xml");
4、结果
---------doIt begin --------- i = 0 ---------doIt end --------- ---------doIt begin --------- i = 1 ---------doIt end --------- ---------doIt begin --------- i = 2 ---------doIt end --------- ---------doIt begin --------- i = 3 ---------doIt end --------- ---------doIt begin --------- i = 4 ---------doIt end --------- ---------doIt begin ---------