Spring利用Quartz实现定时调度
2016-08-25 17:51
351 查看
Quartz是一个强大的企业级任务调度框架,Spring中继承并简化了Quartz,下面就看看在Spring中怎样配置Quartz:
首先我们来写一个被调度的类:
这里设置的调度时间为:每日17:41
执行结果:
Test start:
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Test end.
Quartz的任务调度!!!
首先我们来写一个被调度的类:
package com.kay.quartz; public class QuartzJob { public void work(){ System.out.println("Quartz的任务调度!!!"); } }Spring的配置文件:
<?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="quartzJob" class="com.kay.quartz.QuartzJob"></bean> <!-- 定时调用对象和调用对象的方法 --> <bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 调用的类 --> <property name="targetObject"> <ref bean="quartzJob"/> </property> <!-- 调用类中的方法 --> <property name="targetMethod"> <value>work</value> </property> </bean> <!-- 定义触发时间 --> <bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="jobtask"></ref> </property> <!-- cron表达式 --> <property name="cronExpression"> <value>0 41 17 ? * *</value> </property> </bean> <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 --> <bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="doTime"/> </list> </property> </bean> </beans>测试程序:
package com.kay.quartz; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainTest { public static void main(String[] args) { System.out.println("Test start:"); ApplicationContext context = new ClassPathXmlApplicationContext("web.xml"); //如果配置文件中将startQuertz bean的lazy-init设置为false 则不用实例化 //context.getBean("startQuertz"); System.out.println("Test end."); } }
这里设置的调度时间为:每日17:41
执行结果:
Test start:
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Test end.
Quartz的任务调度!!!
相关文章推荐
- Spring整合Quartz实现定时任务调度
- Spring3.0与Quartz的整合实现定时任务调度
- Spring与Quartz的整合实现定时任务调度
- Spring与Quartz的整合实现定时任务调度(转)
- Spring与Quartz的整合实现定时任务调度
- Spring与Quartz的整合实现定时任务调度
- Spring--超简单利用quartz实现定时作业
- spring定时任务(利用Quartz实现)
- 在spring中用quartz实现定时调度
- Spring与Quartz的整合实现定时任务调度
- Quartz 框架和cron表达式任务调度的例子(spring下实现定时发送邮件)
- Spring3.0与Quartz的整合实现定时任务调度
- Spring与Quartz的整合实现定时任务调度
- Spring与Quartz的整合实现定时任务调度
- Spring和Quartz实现的定时任务调度
- Spring与Quartz的整合实现定时任务调度
- Spring与Quartz的整合实现定时任务调度
- Spring整合quartz框架实现任务定时调度
- Spring与Quartz的整合实现定时任务调度
- Spring与Quartz的整合实现定时任务调度