spring定时任务--基本使用
2013-11-29 14:22
337 查看
一、基本使用:
spring的定时任务使用起来十分方便,只需要两步:1、写好执行定时任务的类和方法;2、配置spring定时任务配置文件:
1、写好执行定时任务的类和方法:
[java] view
plaincopy
package com.test;
public class Test {
public void test() {
System.out.println("执行定时任务的方法。");
}
}
2、配置spring的定时任务配置文件(可以新建一个xml文件,也可以在已经有的xml文件中配置)
配置文件内容:
[html] view
plaincopy
<?xml version="1.0" encoding="UTF-8"?>
<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-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!--必须,QuartzScheduler 延时启动,应用启动后 QuartzScheduler 再启动-->
<property name="startupDelay" value="60"/>
<!-- 普通触发器 :触发器列表-->
<property name="triggers">
<list>
<ref local="<span style="color:#ff0000;">testTrigger</span>"/>
</list>
</property>
</bean>
<!-- 配置执行定时任务的类和方法 -->
<bean id="<span style="color:#ff0000;">testDetail</span>"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<bean class="<span style="color:#ff0000;">com.test.Test</span>"></bean>
</property>
<property name="targetMethod">
<value><span style="color:#ff0000;">test</span></value>
</property>
</bean>
[html] view
plaincopy
<!-- 配置触发器 -->
<bean id="<span style="color:#ff0000;">testTrigger</span>"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="<span style="color:#ff0000;">testDetail</span>"/> <!-- 触发器触发的 执行定时任务的bean -->
</property>
<property name="cronExpression">
<!-- 每天23时 --> <!-- 定时任务执行的间隔 -->
<value>0 0 23 * * ?</value>
</property>
</bean>
</beans>
spring的定时任务使用起来十分方便,只需要两步:1、写好执行定时任务的类和方法;2、配置spring定时任务配置文件:
1、写好执行定时任务的类和方法:
[java] view
plaincopy
package com.test;
public class Test {
public void test() {
System.out.println("执行定时任务的方法。");
}
}
2、配置spring的定时任务配置文件(可以新建一个xml文件,也可以在已经有的xml文件中配置)
配置文件内容:
[html] view
plaincopy
<?xml version="1.0" encoding="UTF-8"?>
<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-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!--必须,QuartzScheduler 延时启动,应用启动后 QuartzScheduler 再启动-->
<property name="startupDelay" value="60"/>
<!-- 普通触发器 :触发器列表-->
<property name="triggers">
<list>
<ref local="<span style="color:#ff0000;">testTrigger</span>"/>
</list>
</property>
</bean>
<!-- 配置执行定时任务的类和方法 -->
<bean id="<span style="color:#ff0000;">testDetail</span>"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<bean class="<span style="color:#ff0000;">com.test.Test</span>"></bean>
</property>
<property name="targetMethod">
<value><span style="color:#ff0000;">test</span></value>
</property>
</bean>
[html] view
plaincopy
<!-- 配置触发器 -->
<bean id="<span style="color:#ff0000;">testTrigger</span>"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="<span style="color:#ff0000;">testDetail</span>"/> <!-- 触发器触发的 执行定时任务的bean -->
</property>
<property name="cronExpression">
<!-- 每天23时 --> <!-- 定时任务执行的间隔 -->
<value>0 0 23 * * ?</value>
</property>
</bean>
</beans>
相关文章推荐
- java垃圾回收机制详解
- java jdbc 判断数据库表是否存在
- MyEclipse的一些配置
- Struts 配置
- 第五届河南省程序设计大赛nyoj545Metric Matrice(java 类goto语句)
- java 线程小例子
- java统计文件中字符,数字,汉字,空格数目
- eclipse中使用git
- Spring事务-2
- java 或 web中解决路径问题
- struts2 表单提交post
- ubuntu下快速的安装jdk7
- 简单的web登录界面加入Hibernate(二)
- 浅谈如何使用swfupload工具与struts2无缝相接
- SSH出错--Hibernate--java.lang.NoSuchMethodError: antlr.collections.AST.getLine()I
- 本地代码中使用Java对象
- the import java.util.* cannot be resolve,怎么解决
- Java 文件和byte数组转换
- 转:Myeclipse快捷键设置
- JSON与JAVA的数据转换