定時調度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/



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