Spring Quartz 任务调度

本文介绍如何使用Quartz作为Java的定时任务框架,并通过Spring整合实现计划任务的配置与管理。包括基本应用示例和Web环境下的配置方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


关键字: quartz

Quartz 是一个 Java 的定时任务框架 ,使用它可以方便地实现计划任务,即在某个时间或是每隔一定时间运行一个任务。

 

Spring Quartz 类库
spring.jar
quartz-all-1.6.1.jar
log4j-1.2.15.jar
commons-logging.jar
commons-collections.jar

 

简单应用示例

1.启动类

Java代码   收藏代码
package com.wisdombrave.spring.quartz;


import org.springframework.context.ApplicationContext;


import org.springframework.context.support.ClassPathXmlApplicationContext;





public class TestMain {


	public static void main(String[] args) {


		System.out.println("---开始初始化--- ");


		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");


		System.out.println("---完成初始化---");


		


		// 死循环,查看定时高度情况,本例高度为每分钟一次


		while (true) {


			


		}


	}


}

2.Spring上下文配置(applicationContext.xml)

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" xmlns:context="http://www.springframework.org/schema/context"


	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"


	xsi:schemaLocation="http://www.springframework.org/schema/beans


           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd


           http://www.springframework.org/schema/context


           http://www.springframework.org/schema/context/spring-context-2.5.xsd


           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd


           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


    <!-- bean实例 -->       


	<bean id="testQuartz" class="com.wisdombrave.spring.quartz.TestQuartz" />


	


	<!-- bean触发方法配置 -->


	<bean name="quartzBean"


		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">


		<!-- bean名字 -->


		<property name="targetObject" ref="testQuartz" />


		<!-- bean方法 -->


		<property name="targetMethod">


			<value>test</value>


		</property>


		<property name="concurrent">


			<value>false</value>


		</property>


	</bean>


	<!-- bean触发时间配置 -->


	<bean id="quartzTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">


		<!-- 触发bean配置 -->


		<property name="jobDetail">


			<ref bean="quartzBean" />


		</property>


		<!-- 触发时间配置 -->


		<property name="cronExpression">


			<value>0/5 * * * * ?</value>


		</property>


	</bean>





	<!-- quartz触发器管理 -->


	<bean id="sfb"


		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">


		<!-- 添加触发器 -->


		<property name="triggers">


			<list>


				<ref local="quartzTrigger" />


			</list>


		</property>


	</bean>


</beans>

 

 

WEB应用示例

1.调度类(TestQuartz)

Java代码   收藏代码
package com.wisdombrave.spring.quartz;





import java.util.Date;





public class TestQuartz {


	public void test() {


		System.out.println(new Date() + "调用");


	}


}


 2.Spring应用上下文配置 (applicationContext.xml)

Java代码   收藏代码
<?xml version="1.0" encoding="UTF-8"?>


<beans xmlns="http://www.springframework.org/schema/beans"


	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"


	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"


	xsi:schemaLocation="http://www.springframework.org/schema/beans


           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd


           http://www.springframework.org/schema/context


           http://www.springframework.org/schema/context/spring-context-2.5.xsd


           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd


           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


    <!-- 调度的对象 -->       


	<bean id="testQuartz" class="com.wisdombrave.spring.quartz.TestQuartz" />


	<import resource="springQuartz.xml"/>


</beans>

3. Spring资源文件(springQuartz.xml)

Java代码   收藏代码
<?xml version="1.0" encoding="UTF-8"?>


<beans xmlns="http://www.springframework.org/schema/beans"


	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"


	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"


	xsi:schemaLocation="http://www.springframework.org/schema/beans


           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd


           http://www.springframework.org/schema/context


           http://www.springframework.org/schema/context/spring-context-2.5.xsd


           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd


           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


	<!-- bean触发方法配置 -->


	<bean name="quartzBean"


		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">


		<!-- bean名字 -->


		<property name="targetObject" ref="testQuartz" />


		<!-- bean方法 -->


		<property name="targetMethod">


			<value>test</value>


		</property>


		<property name="concurrent">


			<value>false</value>


		</property>


	</bean>


	


	<!-- bean触发时间配置 -->


	<bean id="quartzTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">


		<!-- 触发bean配置 -->


		<property name="jobDetail">


			<ref bean="quartzBean" />


		</property>


		<!-- 触发时间配置 -->


		<property name="cronExpression">


			<value>0/1 * * * * ?</value>


		</property>


	</bean>





	<!-- quartz触发器管理 -->


	<bean id="sfb"


		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">


		<!-- 添加触发器 -->


		<property name="triggers">


			<list>


				<ref local="quartzTrigger" />


			</list>


		</property>


	</bean>


</beans>


 3.WEB容器配置(web.xml)

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">


  <welcome-file-list>


    <welcome-file>index.jsp</welcome-file>


  </welcome-file-list>


  


  <!-- spring -->


	<context-param>


		<param-name>contextConfigLocation</param-name>


		<param-value>classpath:applicationContext.xml</param-value>


	</context-param>


	<listener>


		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>


	</listener>


</web-app>

 

日期规则

"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 ? 3 WED" 每年三月的星期三的下午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 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值