Spring配置Quartz样例

网上配置很多,这里只写个样例,方便直接拷贝使用。

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...”。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值