spring定时任务和quartz定时任务
spring定时任务:
1、maven下载spring依赖,在pom.xml下添加如下:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.2.10.RELEASE</version> </dependency>
2、编写配置文件applicationContext-*.xml
<?xml version="1.0" encoding="UTF-8"> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:security="http://www.springframework.org/schema/security" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd "> <!-- 要调用的工作类 循环覆盖 --> <bean id="deleteDateTask" class="com.task.DeleteDataTask"></bean> <!-- 循环覆盖 --> <bean id="doDeleteDateTaskTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 调用的类 --> <property name="targetObject"> <ref bean="deleteDateTask" /> </property> <!-- 调用类中的方法 --> <property name="targetMethod"> <value>deleteHbaseAndESData</value> </property> <!-- 指定concurrent设为false,多个job不会并发运行,第二个job将不会在第一个job完成之前开始。 --> <property name="concurrent"> <value>false</value> </property> </bean> <!-- 定义触发时间 --> <bean id="doDeleteDateTaskTime" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail"> <ref bean="doDeleteDateTaskTask" /> </property> <!-- cron表达式 --> <property name="cronExpression">
<!-- 配置时间 --!> <value>* * * * * ?</value> </property> </bean> <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 --> <bean id="startQuertz" lazy-init="true" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="doDeleteDateTaskTime"/> </list> </property> </bean> </beans>
3、在web.xml的<param-value>下加入applicationContext-*.xml,配置如下
<context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:applicationContext-*.xml, </param-value> </context-param>
注:spring的时间配置如下:
quartz定时任务: