Spring配置Quartz样例

来源:互联网 发布:mac版百度网盘 编辑:程序博客网 时间:2024/05/17 01:56
网上配置很多,这里只写个样例,方便直接拷贝使用。

1. 需要的jar包有:
spring-framework-2.5.6\dist\spring-2.5.6.jar
spring-framework-2.5.6\lib\quartz\quartz-all-1.6.1.jar
spring-framework-2.5.6\lib\jakarta-commons\commons-logging.jar
spring-framework-2.5.6\lib\jakarta-commons\commons-collections.jar
spring-framework-2.5.6\lib\log4j\log4j-1.2.15.jar

2. 需要执行任务的java类

package com.servlet.backup;public class QuartzJob{    /**     * 固定间隔时间执行此任务     */    public void showQuartzMessageEachseconds()    {        System.out.println("固定间隔时间执行:showQuartzMessageEachseconds is called...");    }    /**     * 指定时间执行此任务     */    public void showQuartzMessageAtFixedTime()    {        System.out.println("固定时间点执行:showQuartzMessageAtFixedTime is called...");    }}

3. Spring的bean配置文件,WebRoot\WEB-INF\conf\springbeans.service.xml :

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"><!-- 默认为singleton --><bean id="quartzJob" class="com.servlet.backup.QuartzJob"scope="singleton" /></beans>

4. 定时任务配置文件,WebRoot\WEB-INF\conf\timer.service.xml :

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"><!-- 间隔固定时间打印信息的监控 --><bean id="showQuartzEveryTimeJob"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject" ref="quartzJob"></property><property name="targetMethod"value="showQuartzMessageEachseconds"></property><property name="concurrent" value="false"></property></bean><!-- 间隔时间执行的定时任务 --><bean id="showQuartzEveryTimeTrigger"class="org.springframework.scheduling.quartz.SimpleTriggerBean"><property name="jobDetail" ref="showQuartzEveryTimeJob" /><property name="startDelay" value="10000" /><!-- 定时任务延时10秒启动 --><property name="repeatInterval" value="10000" /><!-- 间隔10秒执行 --></bean><!-- 固定时间点打印信息的监控 --><bean id="showQuartzFixedTimeJob"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject" ref="quartzJob"></property><property name="targetMethod"value="showQuartzMessageAtFixedTime"></property><property name="concurrent" value="false"></property></bean><!-- 固定时间点执行的定时任务 --><bean id="showQuartzFixedTimeTrigger"class="org.springframework.scheduling.quartz.CronTriggerBean"><property name="jobDetail" ref="showQuartzFixedTimeJob" /><property name="cronExpression" value="0 * * * * ?" /><!-- 每分钟的0秒执行 --></bean><!-- 时间间隔执行任务processSQLToSCPJob --><bean id="showQuartzScheduler"class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="triggers"><list><ref bean="showQuartzEveryTimeTrigger" /><ref bean="showQuartzFixedTimeTrigger" /></list></property><!-- 线程池线程个数 --><property name="quartzProperties"><props><prop key="org.quartz.threadPool.threadCount">1</prop></props></property><!-- 初始化之后延迟10秒启动scheduler --><property name="startupDelay"><value>10</value></property></bean></beans>

说明:
1> 这些bean可以与Spring的bean放到一起定义。
2> 关于“固定时间点执行定时任务”,其表达式格式: [秒] [分] [小时] [日] [月] [周] [年],详情请baidu搜索“Quartz CronTrigger”。


5. web配置文件配置,WebRoot\WEB-INF\web.xml :

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><!-- 配置文件路径 --><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/conf/*.service.xml</param-value></context-param><!-- Spring监听 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener></web-app>

6. 将Web服务部署到tomcat上,启动tomcat。

7. 结果检查:
启动服务10秒后,每隔10秒,控制台打印:“固定间隔时间执行:showQuartzMessageEachseconds is called...”;
每分钟的0秒时,控制台打印:“固定时间点执行:showQuartzMessageAtFixedTime is called...”。



原创粉丝点击