定时调度1--Quartz

Quartz 就是启动定时任务的框架!!


Quartz快速入门

1.建立maven项目

2.导入quartz座标

3.quartzspring整合应用

第一步:创建maven工程,并导入quartzspring相关的座标


第二步:开发一个Job

第三步

       1提供spring配置文件,注册自定义的Job

    2配置JobDetail,由这个对象负责通过反射调用自定义的Job的方法

    3配置触发器,指定触发的时间

    4 配置任务调度工厂

            
<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
	xmlns:task="http://www.springframework.org/schema/task"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xmlns:soap="http://cxf.apache.org/bindings/soap"
	xsi:schemaLocation="
						http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
						http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
						http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
						http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
						http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
						http://www.springframework.org/schema/data/jpa 
						http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
						http://cxf.apache.org/bindings/soap 
						http://cxf.apache.org/schemas/configuration/soap.xsd
						http://cxf.apache.org/jaxws 
						http://cxf.apache.org/schemas/jaxws.xsd">
	
	
	<!-- 注册自定义的Job -->
	<bean id="myJob" class="cn.itcast.bos.jobs.MailJob"></bean>
	
	<!-- 注册JobDetail,负责通过反射调用Job -->
	<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<!-- 注入目标对象 -->
		<property name="targetObject" ref="myJob"/>
		<!-- 注入目标方法 -->
		<property name="targetMethod" value="sendMail"/>
	</bean>
	
	<!-- 注册触发器,指定触发时间 -->
	<bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
		<property name="jobDetail" ref="jobDetail"/>
		<!-- 表达式,用于定义触发的时间 -->
		<property name="cronExpression">
			<!-- 每隔5秒触发一次 -->
			<value>0/10 * 12 * * ?</value>
		</property>
	</bean>
	
	<!-- 注册调度工厂,统一进行任务调度 -->
	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="myTrigger"/>
			</list>
		</property>
	</bean>
</beans>

第四步:加载spring配置文件,创建spring工厂


Cron表达式


cron表达式在线生成器:http://cron.qqe2.com/



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章