歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

maven構建使用Spring執行定時任務

maven構建使用Spring執行定時任務

首先,pom.xml中加入依賴:

版本配置信息:

<properties>
  <java-version>1.6</java-version>
  <org.springframework-version>3.1.1.RELEASE</org.springframework-version>
  <tiles-version>2.2.1</tiles-version>
  <org.aspectj-version>1.6.10</org.aspectj-version>
  <org.slf4j-version>1.6.6</org.slf4j-version>
 </properties>

依賴:

                <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>${org.springframework-version}</version>
   <exclusions>
    <!-- Exclude Commons Logging in favor of SLF4j -->
    <exclusion>
     <groupId>commons-logging</groupId>
     <artifactId>commons-logging</artifactId>
    </exclusion>
    <exclusion>
     <groupId>commons-beanutils</groupId>
     <artifactId>commons-beanutils</artifactId>
    </exclusion>
    <exclusion>
     <groupId>commons-digester</groupId>
     <artifactId>commons-digester</artifactId>
    </exclusion>
   </exclusions>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context-support</artifactId>
   <version>${org.springframework-version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-tx</artifactId>
   <version>${org.springframework-version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-web</artifactId>
   <version>${org.springframework-version}</version>
  </dependency>
  <dependency>
  <groupId>org.quartz-scheduler</groupId>
   <artifactId>quartz</artifactId>
   <version>1.8.5</version>
  </dependency>


  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>${org.springframework-version}</version>
  </dependency>
  <!-- AspectJ -->
  <dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjrt</artifactId>
   <version>${org.aspectj-version}</version>
  </dependency>

java代碼:

package com.cetc.yun.service.impl;

import org.apache.log4j.Logger;

import com.cetc.yun.service.TaskTestService;

public class TaskTestServiceImpl implements TaskTestService {

 private static Logger log = Logger.getLogger(TaskTestServiceImpl.class);

 @Override
 public void sayHello() {
  // TODO Auto-generated method stub
        try {
              log.info("處理任務開始>........");
              // 業務邏輯代碼調用
              log.info("時間[" + new java.util.Date().toGMTString()
                            + "]----->大家好啊!");
              log.info("處理任務結束!");
        } catch (Exception e) {
              log.error("處理任務出現異常", e);
        }
 }

}

關鍵配置信息:

<?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:task="http://www.springframework.org/schema/task"
 xmlns:beans="http://www.springframework.org/schema/mvc"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
  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/task 
        http://www.springframework.org/schema/task/spring-task-3.1.xsd">
 <!-- Root Context: defines shared resources visible to all other web components -->
 <bean name="taskJob" class="com.cetc.yun.service.impl.TaskTestServiceImpl" />

    <bean id="methodInvokingJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
      <property name="targetObject">
          <ref bean="taskJob" />
      </property>
      <property name="targetMethod">
          <value>sayHello</value>
      </property>
    </bean>
    <!-- 配置觸發器 -->
    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <!-- 這裡不可以直接在屬性jobDetail中引用taskJob,因為他要求的是一個jobDetail類型的對象,所以我們得通過MethodInvokingJobDetailFactoryBean來轉一下 -->
      <property name="jobDetail"><ref bean="methodInvokingJobDetail" /></property>
      <!-- 每天的8點到21點每隔1分鐘觸發,具體說明見附錄 -->
      <property name="cronExpression"><value>0/5 * 08-21 * * ?</value></property>

    </bean>

    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
      <!-- 添加觸發器 -->
      <property name="triggers">
          <list>
              <ref local="cronTrigger" />
          </list>
      </property>
    </bean> 
</beans>

Spring 的詳細介紹:請點這裡
Spring 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved