1. 程式人生 > >Spring+Quartz定時器結合呼叫service

Spring+Quartz定時器結合呼叫service

一、Spring+Quartz定時器

1、所需JAR包:

spring-context-support版本4.1.4、

quartz版本2.2.1

2、spring-scheduler.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"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">  
   
    <context:component-scan base-package="com.ches.timer" />  
   
   
   	<!-- 整合方式:JobDetailFactoryBean,並且任務類需要繼承QuartzJobBean-->
	<!-- 定義jobDetail -->
    <bean id="noticeJobDetailBean" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
		<property name="name" value="noticeJob" />
		<!--	 目標類 -->
		<property name="jobClass" value="com.ches.timer.NoticeTimer" />
		<!-- durability 表示任務完成之後是否依然保留到資料庫,預設false   -->
        <property name="durability" value="true" />
	</bean>
	
	<!-- 觸發器CornTrigger -->								
	<bean id="noticeTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
		<property name="jobDetail" ref="noticeJobDetailBean" />
		<property name="cronExpression">  
	        <!-- 每天零點執行 0 0 0 0 * * ? -->  
	        <value>0 0 0 * * ?</value>  
	    </property>
	</bean>

	<!-- 定義核心排程器 -->
	<bean  class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
        <property name="jobDetails">  
            <list>  
                <ref bean="noticeJobDetailBean" />  
            </list>  
        </property>  
   
        <property name="triggers">  
            <list>  
                <ref bean="noticeTaskTrigger" />  
            </list>  
        </property>  
    </bean>
</beans>

3、NoticeTimer.java
package com.ches.timer;

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

import com.ches.service.meeting.NoticeService;
import com.ches.utils.SpringContext;


public class NoticeTimer  extends QuartzJobBean{
	
	private NoticeService noticeService;

	@Override
	protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
		try {
			noticeService = (NoticeService) SpringContext.getBean("noticeService");
			noticeService.topNoticeTimer();
		} catch (Exception e) {
			e.printStackTrace();
		}     
		
	}
}

二、ApplicationContextAware初始化

1、SpringContext.java

package com.ches.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class SpringContext implements ApplicationContextAware {

	private static ApplicationContext applicationContext;

	public void setApplicationContext(ApplicationContext ac)
			throws BeansException {
		applicationContext = ac;
	}

	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}


	public static Object getBean(String name) {
		return applicationContext.getBean(name);
	}
}

2、在Spring的配置檔案中配置這個類,Spring容器會在載入完Spring容器後把上下文物件呼叫這個物件中的setApplicationContext方法:
<bean id="springContext" class="com.ches.utils.SpringContext" lazy-init="false" />

3、在web專案中的web.xml中配置載入Spring容器的Listener:
<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

4、在專案中即可通過這個SpringContext呼叫getBean()方法得到Spring容器中的物件了。

附加說明:

1、呼叫spring中的service使用的是ApplicationContext獲取,並沒有在配置檔案中引入

2、定時器時間設定解說:

cronExpression 表示式包括以下 7 個欄位:
 


小時
月內日期

周內日期
年(可選欄位)
特殊字元
 
cronExpression 觸發器利用一系列特殊字元,如下所示:
 
反斜線(/)字元表示增量值。例如,在秒欄位中“5/15”代表從第 5 秒開始,每 15 秒一次。
 
問號(?)字元和字母 L 字元只有在月內日期和周內日期欄位中可用。問號表示這個欄位不包含具體值。所以,如果指定月內日期,可以在周內日期欄位中插入“?”,表示周內日期值無關緊要。字母 L 字元是 last 的縮寫。放在月內日期欄位中,表示安排在當月最後一天執行。在周內日期欄位中,如果“L”單獨存在,就等於“7”,否則代表當月內周內日期的最後一個例項。所以“0L”表示安排在當月的最後一個星期日執行。
 
在月內日期欄位中的字母(W)字元把執行安排在最靠近指定值的工作日。把“1W”放在月內日期欄位中,表示把執行安排在當月的第一個工作日內。
 
井號(#)字元為給定月份指定具體的工作日例項。把“MON#2”放在周內日期欄位中,表示把任務安排在當月的第二個星期一。
 
星號(*)字元是通配字元,表示該欄位可以接受任何可能的值。
欄位 允許值 允許的特殊字元
秒 0-59 , - * /
分 0-59 , - * /
小時 0-23 , - * /
日期 1-31 , - * ? / L W C
月份 1-12 或者 JAN-DEC , - * /
星期 1-7 或者 SUN-SAT , - * ? / L C #
年(可選) 留空, 1970-2099 , - * /
 
表示式意義
"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觸發
每天早上6點
 
0 6 * * *
 
每兩個小時
 
0 */2 * * *
晚上11點到早上8點之間每兩個小時,早上八點
 
0 23-7/2,8 * * *
 
每個月的4號和每個禮拜的禮拜一到禮拜三的早上11點
 
0 11 4 * 1-3
1月1日早上4點
 
0 4 1 1 *