Spring中的定时任务

xiaoxiao2022-06-11  24

下面我们来看一下Spring中提供的定时任务开发: 在Spring中开发定时任务,分为3个步骤。 1 创建定时任务 2 注册定时任务 3 启动定时任务 分别来看一下 1 创建定时任务: package org.jnotnull; import java.util.TimerTask; public class MyTesk extends TimerTask{ .... public void run(){ //添加任务 } .... } 2 注册定时任务,并设置参数 我们来配置TimerConfig.xml防御WEB-INF下 <bean id="myTesk" class="edu.cumt.jnotnull.action.TaskAction"> <property name="newsManageService"> <ref bean="newsManageService" /> </property> </bean> <bean id="stTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"> <property name="delay"> <value>20000</value> </property> <property name="period"> <value>30000</value> </property> <property name="timerTask"> <ref bean="myTesk" /> </property> </bean> <bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean"> <property name="scheduledTimerTasks"> <list> <ref bean="stTask" /> </list> </property> </bean> 3 启动定时任务 <?xml version="1.0" encoding="UTF-8"?> <web-app> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/TimerConfig.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> </web-app> 下面我们再来看看在Spring中如何使用Quartz实现定时功能 1 创建定时任务: package org.jnotnull; import java.util.TimerTask; public class MyTesk extends TimerTask{ .... public void excute(){ //添加任务 } .... } 2 注册定时任务,并设置参数 我们来配置TimerConfig.xml防御WEB-INF下 <?xml version="1.0" encoding="UTF-8"> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id ="myTesk" class="org.jnotnull.MyTesk"/> <bean id ="myJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="myTask"> </property> <property ="targetMethod"> <value>execute</value> </property> </bean> <bean id ="timeTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="myJob"> </property> <property name="cronExpression"> <value>12,23****?</value> </property> </bean> <bean id ="timerFactory" class="org.springframework.scheduling.quartz.ScheduleFactoryBean"> <property name="triggers"> <list> <ref="timeTrigger"> </list> </property> </bean> </beans> 3 启动定时任务 <?xml version="1.0" encoding="UTF-8"?> <web-app> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/TimerConfig.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> </web-app> 14:57 浏览 (917) 评论 (0) 分类: Spring 收藏 2008-01-11 缩略显示正则表达式 关键字: spring 正则表达式 正则表达式有Stephen Kleene于1956年提出来 1 “.” 可以用来匹配任何一个字符。如:形式为a.b的正则表达式,它可以匹配aab,acb,a2b,a#b等等。 2 “[]” 只有[]里指定的字符才能匹配。如形式为a[xyz]b的正则表达式,它可以匹配axb,ayb,azb,不能匹配amb等其它的。 3 “*” 表示匹配次数,可以表示任意次。用来表示紧靠在该符号左边的符号出现的次数。如对于形式为a.*b的正则表达式,可以匹配azb,azzb,ab,a*b 4 “?” 表示匹配0次或者1次,用来表示紧靠在该符号左边的符号出现的次数。如对于a.?b的正则表达式,可以匹配axb,a*b 5 “\” 这个时正则表达式的连接符。如对于正则表达式a.\-b,它可以匹配a-b,az-f,a*-f
转载请注明原文地址: https://www.6miu.com/read-4930177.html

最新回复(0)