天天看點

spring 整合maven 定時任務排程

這裡特别對版本作一下說明,是因為spring和quartz的整合對版本是有要求的。

spring3.1以下的版本必須使用quartz1.x系列,3.1以上的版本才支援quartz 2.x,不然會出錯。

至于原因,則是spring對于quartz的支援實作,org.springframework.scheduling.quartz.CronTriggerBean繼承了org.quartz.CronTrigger,在quartz1.x系列中org.quartz.CronTrigger是個類,而在quartz2.x系列中org.quartz.CronTrigger變成了接口,進而造成無法用spring的方式配置quartz的觸發器(trigger)。

1、spring-quartz.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:util="http://www.springframework.org/schema/util"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
	http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"
	default-lazy-init="false">


	<!-- 定義目标bean和bean中的方法 -->  
    <!-- =====================日常任務job========================== -->
    
    <bean id="MyTask" class="com.jn.commons.quartz.AutoTask">
    </bean>  
    <bean id="MyTask1" class="com.jn.commons.quartz.AutoTask">
    </bean>  
     
    <bean id="MyTaskMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
        <property name="targetObject">  
            <ref bean="MyTask"/>  
        </property>  
        <property name="targetMethod">  <!-- 要執行的方法名稱 -->  
            <value>execute</value>  
        </property>  
    </bean> 
    
    <bean id="MyTaskMethod1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
        <property name="targetObject">  
            <ref bean="MyTask1"/>  
        </property>  
        <property name="targetMethod">  <!-- 要執行的方法名稱 -->  
            <value>auto</value>  
        </property>  
    </bean> 
    
   
    <!-- ======================== 排程觸發器 ======================== -->  
  <bean id="DailyTaskCronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">  
        <property name="jobDetail" ref="MyTaskMethod"></property>  
        <!-- 每分鐘的第一秒觸發   -->
        <property name="cronExpression" value="1 * * * * ?"></property>
         <!-- 每小時的第一分鐘觸發  -->
       <!--  <property name="cronExpression" value="0 1 * * * ?"></property>  -->
    </bean>
      
  <bean id="DailyTaskCronTriggerBean1" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">  
        <property name="jobDetail" ref="MyTaskMethod1"></property>  
        <!-- 每分鐘的第一秒觸發   -->
        <property name="cronExpression" value="1 * * * * ?"></property>
         <!-- 每小時的第一分鐘觸發  -->
       <!--  <property name="cronExpression" value="0 1 * * * ?"></property>  -->
    </bean>  
    
   
    <!-- ======================== 排程工廠 ======================== -->  
    <bean id="SpringJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
        <property name="triggers">  
            <list>   
                <ref bean="DailyTaskCronTriggerBean"/> 
                <ref bean="DailyTaskCronTriggerBean1"/> 
            </list>  
        </property>  
    </bean>   
    

</beans>
           

2、在sping主配置檔案中引入

<import resource="classpath:spring/spring-quartz.xml" /> 
           

3、在pom.xm檔案中引入需要下載下傳的jar

<dependency>
		<groupId>org.quartz-scheduler</groupId>
		<artifactId>quartz</artifactId>
		<version>1.8.5</version>
	</dependency>
           

4、編寫測試java檔案

package com.jn.commons.quartz;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Logger;

/**
 * ClassName: AutoTask <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON(可選). <br/>
 * date: 2016-5-23 上午10:13:40 <br/>
 * 
 * @author weber
 * @version
 * @since JDK 1.6
 */
public class AutoTask {
	private static  Logger logger = Logger.getLogger(AutoTask.class
			.getName());

	protected void execute() throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = format.format(new Date());
		System.out.println("這是第一個定時任務:" + date);
		logger.info("任務1:每分鐘定時擷取目前系統時間");
	}

	public void auto() {
		MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
		MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage(); //椎記憶體使用情況
		long totalMemorySize = memoryUsage.getInit(); //初始的總記憶體
		long maxMemorySize = memoryUsage.getMax(); //最大可用記憶體
		long usedMemorySize = memoryUsage.getUsed(); //已使用的記憶體
        System.out.println("這是第二個定時任務"+"\r"+"椎記憶體使用情況:"+"1.初始的總記憶體:"+totalMemorySize+"2.最大可用記憶體:"+maxMemorySize+"3.已使用的記憶體:"+usedMemorySize);
        logger.info("任務2:背景監控椎記憶體使用情況");
	}

}
           

5、測試結果

spring 整合maven 定時任務排程