Quartz定時任務和Spring集成的三種方式

什麼是定時任務?

  • 在規定的某一個時間點去執行相關的業務代碼

定時任務有什麼作用?

  • 在開發中的作用
    1、每天晚上12點自動備份數據庫
    2、每天晚上12點彙總當前銷售信息,把消息發給老闆
    3、每隔一定時間同步A庫和B庫

Quartz定時任務三大核心

  • 任務 JobDetail(描述任務相關情況)
  • 調度器 Scheduler (代表一個Quarts的獨立運行容器,需要JobDetail和Trigger都註冊到Scheduler中才會生效)
  • 觸發器 Trigger (描述觸發Job執行的時間觸發規則)

Quartz和Spring集成的三種方式

JobDetailBean實現
1、創建任務 MyJob1

package com.lzh.job;

public class MyJob1 {
	public void doTask() {
		System.out.println("方式一的實現方式");
	}
}

2、配置applicationContext.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
	    http://www.springframework.org/schema/context/spring-context.xsd
	    http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
	<!-- 聲明任務 -->
	<bean id="job01" class="com.lzh.job.MyJob1"></bean>
	<!-- 包裝任務詳情 -->
	<bean id="springQuartzJobBean" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<!-- 注入目標對象 -->
		<property name="targetObject" ref="job01"></property>
		<!-- 注入目標對象的方法 -->
		<property name="targetMethod" value="doTask"></property>
	</bean>
	<!-- 聲明觸發規則 -->
	<bean id="trigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
	<!-- 注入任務詳情 -->
		<property name="jobDetail" ref="springQuartzJobBean"></property>
		<!-- 定義觸發規則 -->
		<property name="cronExpression" value="0/5 * * * * ? "></property>
	</bean>
	<!-- 配置調度器 -->
	<bean id="springJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<array>
				<ref bean="trigger"/>
			</array>
		</property>
	</bean>
</beans>
   	·聲明任務
 	·聲明觸發器
 	**注意**這裏有個觸發規則的表達式的Value,有個網址可以用[規則表達式value的取值](http://cron.qqe2.com/)    			
 	·聲明調度器

3、測試TestApp

package com.lzh.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestApp {

	
	public static void main(String[] args) {

		//加載spring的IOC
		ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
		
	}
}

  • Quartz和Spring集成方式二【XML】

Method Invoke Job Detail Factory Bean實現(推薦)
1、創建任務 MyJob2

package com.lzh.job;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class MyJob2 extends QuartzJobBean{

	@Override
	protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
		System.out.println("方式二的實現方式");
	}
}

2、配置applicationContext2.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
	    http://www.springframework.org/schema/context/spring-context.xsd
	    http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
	<!-- 包裝任務詳情 -->
	<bean id="springQuartzJobBean" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
		<!-- 注入目標對象 -->
		<property name="jobClass" value="com.lzh.job.MyJob2"></property>
		<!-- 注入目標對象的方法 -->
		<property name="durability" value="true"></property>
	</bean>
	<!-- 聲明觸發規則 -->
	<bean id="trigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
	<!-- 注入任務詳情 -->
		<property name="jobDetail" ref="springQuartzJobBean"></property>
		<!-- 定義觸發規則 -->
		<property name="cronExpression" value="0/5 * * * * ? "></property>
	</bean>
	<!-- 配置調度器 -->
	<bean id="springJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<array>
				<ref bean="trigger"/>
			</array>
		</property>
	</bean>
</beans>

3、測試TestApp

package com.lzh.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestApp {

	
	public static void main(String[] args) {

		//加載spring的IOC
		ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext2.xml");
		
	}
}


  • Quartz和Spring集成方式三【註解】【掌握】
    首先在applicationContext.xml頭文件加上
    在這裏插入圖片描述
    1、創建任務MyJob3
    注意:加上註解@component 和@Scheduled(“表達式”)
package com.lzh.job;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class MyJob3 {
	@Scheduled(cron = "0/5 * * * * ? ")
	public void doTask() {
		System.out.println("方式三的實現方式");
	}
}

2、配置applicationContext3.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:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
	    http://www.springframework.org/schema/context/spring-context.xsd
	    http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/task 
	    http://www.springframework.org/schema/task/spring-task.xsd">
	<!-- 掃描任務 -->
	<context:component-scan base-package="com.lzh.job"></context:component-scan>
	<!-- 開啓定時任務的註解 -->
	<task:annotation-driven/>
</beans>

3、測試

package com.lzh.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestApp {

	
	public static void main(String[] args) {

		//加載spring的IOC
		ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext3.xml");
		
	}
}


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