使用Spring Quartz执行定时任务
2012-08-06 22:57
771 查看
使用Spring Quartz执行定时任务
(2006-06-03 18:40:09)转载▼
分类:JAVA-Spring |
下面我就举个简单的例子。首先,配置Spring的配置文件,起名叫applicationContext.xml
<?xml version="1.0"encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- 配置 --> <bean name="randomPriceJob"class="org.springframework.scheduling.quartz.JobDetailBean"> <propertyname="jobClass"> <value>test.RandomPriceJob</value> </property> <propertyname="jobDataAsMap"> <map> <entrykey="timeout"><value>5</value></entry> </map> </property> </bean> <!-- 配置触发器 --> <bean id="cronTrigger"class="org.springframework.scheduling.quartz.CronTriggerBean"> <propertyname="jobDetail"> <refbean="randomPriceJob"/> </property> <!-- 每天的11点到11点59分中,每分钟触发RandomPriceJob,具体说明见附录 --> <propertyname="cronExpression"> <value>0* 11 * * ?</value> </property> </bean> <beanclass="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <!-- 添加触发器--> <propertyname="triggers"> <list> <reflocal="cronTrigger"/> </list> </property> </bean> </beans> |
package test; import org.apache.log4j.Category; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; /** * @author shenshan * @version 1.0 */ public class RandomPriceJob extends QuartzJobBean { private static finalCategory cat = Category .getInstance(RandomPriceJob.class ); privateint timeout; /** * @param timeout */ public void setTimeout( int timeout ) { this.timeout = timeout; } /* * (non-Javadoc) * * @seeorg.springframework.scheduling.quartz.QuartzJobBean#executeInternal(org.quartz.JobExecutionContext) */ protected void executeInternal(JobExecutionContext context ) throwsJobExecutionException { cat.debug( "Job start" ); //执行具体操作 } } |
package test; import org.quartz.Scheduler; import org.quartz.impl.StdSchedulerFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.scheduling.quartz.CronTriggerBean; import org.springframework.scheduling.quartz.JobDetailBean; /** * @author shenshan * @version 1.0 */ public class RandomPrice { public static void main( String[ ] args )throws Exception { ClassPathResource res = newClassPathResource( "applicationContext.xml" ); XmlBeanFactory factory = newXmlBeanFactory( res ); JobDetailBean job = (JobDetailBean ) factory .getBean("randomPriceJob" ); CronTriggerBean trigger = (CronTriggerBean ) factory .getBean("cronTrigger" ); Scheduler scheduler =StdSchedulerFactory.getDefaultScheduler( ); scheduler.start( ); scheduler.scheduleJob( job,trigger ); } } |
附:cronExpression配置说明
字段 | 允许值 | 允许的特殊字符 | ||
---|---|---|---|---|
秒 | 0-59 | , - */ | ||
分 | 0-59 | , - */ | ||
小时 | 0-23 | , - */ | ||
日期 | 1-31 | , - * ? / L WC | ||
月份 | 1-12 或者 JAN-DEC | , - */ | ||
星期 | 1-7 或者 SUN-SAT | , - * ? / L C# | ||
年(可选) | 留空,1970-2099 | , - */ |
表达式 | 意义 | |
---|---|---|
"0 0 12 * *?" | 每天中午12点触发 | |
"0 15 10 ? **" | 每天上午10:15触发 | |
"0 15 10 * *?" | 每天上午10:15触发 | |
"0 15 10 * * ?*" | 每天上午10:15触发 | |
"0 15 10 * * ?2005" | 2005年的每天上午10:15触发 | |
"0 * 14 * *?" | 在每天下午2点到下午2:59期间的每1分钟触发 | |
"0 0/5 14 * *?" | 在每天下午2点到下午2:55期间的每5分钟触发 | |
"0 0/5 14,18 * *?" | 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发 | |
"0 0-5 14 * *?" | 在每天下午2点到下午2:05期间的每1分钟触发 | |
"0 10,44 14 ? 3WED" | 每年三月的星期三的下午2:10和2:44触发 | |
"0 15 10 ? *MON-FRI" | 周一至周五的上午10:15触发 | |
"0 15 10 15 *?" | 每月15日上午10:15触发 | |
"0 15 10 L *?" | 每月最后一日的上午10:15触发 | |
"0 15 10 ? *6L" | 每月的最后一个星期五上午10:15触发 | |
"0 15 10 ? * 6L2002-2005" | 2002年至2005年的每月的最后一个星期五上午10:15触发 | |
"0 15 10 ? *6#3" | 每月的第三个星期五上午10:15触发 |
相关文章推荐
- Spring中使用quartz执行定时任务的两种方式
- Spring之——quartz使用@Scheduled注解执行定时任务
- 关于Java Web 使用Spring中使用Quartz(定时调用、实现固定时间执行), 触发定时器(执行某些任务)的实例
- quartz 框架定时任务,使用spring @Scheduled注解执行定时任务
- Spring中使用quartz执行定时任务的两种方式
- java web使用quartz执行定时任务 Spring配置quartz
- quartz 框架定时任务,使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务、
- 使用spring @Scheduled注解执行定时任务、
- spring注解 @Scheduled(cron = "0 0 1 * * *")的使用来实现定时的执行任务
- spring注解 @Scheduled(cron = "0 0 1 * * *")的使用来实现定时的执行任务
- spring里面使用quartz实现定时任务
- 使用spring @Scheduled注解执行定时任务、
- 使用spring quartz实现定时任务
- SSH中使用quartz定时任务,执行两次解决办法
- 使用spring @Scheduled注解执行定时任务、
- 使用Spring+Quartz的定时任务。
- 使用spring @Scheduled注解执行定时任务
- spring整合Quartz定时器(二)---基于@Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务