JavaWeb定時器-Spring3.x及更高版本Task使用

大背景java

現代的 Web 應用程序框架在範圍和複雜性方面都有所發展,應用程序的每一個底層組件也必須相應地發展。做業調度是現代系統中對 Java 應用程序的通常要求,並且也是對 Java 開發人員一向的要求。雖然目前的調度技術比起原始的數據庫觸發器標誌和獨立的調度器線程來講,已經發展了許多,可是做業調度仍然不是個小問題spring

技術選型數據庫

  • Java自帶的java.util.Timer類,這個類容許你調度一個java.util.TimerTask任務。使用這種方式可讓你的程序按照某一個頻度執行,但不能在指定時間運行。通常用的較少,這篇文章將不作詳細介紹。
  • 使用Quartz,這是一個功能比較強大的的調度器,可讓你的程序在指定時間執行,也能夠按照某一個頻度執行,配置起來稍顯複雜,這篇文章將不作詳細介紹。
  • Spring3.0之後自帶的task,能夠將它當作一個輕量級的Quartz,並且使用起來比Quartz簡單許多,這篇文章重點介紹。

乾貨bash

Spring Task,能夠將它比做一個輕量級的Quartz,並且使用起來很簡單,除spring相關的包外不須要額外的包,並且支持兩種使用方式一、註解 二、配置app

Spring Task 註解方式使用說明框架

一、applicationContext.xmlspa

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:task="http://www.springframework.org/schema/task" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="  
        http://www.springframework.org/schema/task 
        http://www.springframework.org/schema/task/spring-task-3.0.xsd
        http://www.springframework.org/schema/beans        
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
        http://www.springframework.org/schema/context                
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config />
    <!-- spring掃描註解的配置 -->
    <context:component-scan base-package="com.antation" />

    <!-- 開啓這個配置,spring才能識別@Scheduled註解 -->
    <task:annotation-driven scheduler="qbScheduler" mode="proxy" />
    <task:scheduler id="qbScheduler" pool-size="10" />

</beans>

 二、Java普通類線程

import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component("taskJob")
public class TaskJob {

    @Scheduled(cron = " 0/2 * * * * ?") // 每兩秒執行一次
    public void myMethod() {
        System.out.println(new Date().toLocaleString() + " 我是基於註解的後臺任務...");
    }
}

 Spring Task 配置方式使用說明component

一、applicationContext.xmlxml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:task="http://www.springframework.org/schema/task" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="  
        http://www.springframework.org/schema/task 
        http://www.springframework.org/schema/task/spring-task-3.0.xsd
        http://www.springframework.org/schema/beans        
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
        http://www.springframework.org/schema/context                
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 定時任務要執行的方法 -->
<bean id="taskJob" class="com.task.TaskJob" />
<bean id="taskJob2" class="com.task.TaskJob2" />

<!--用於定時任務的執行 -->
<task:scheduled-tasks>
    <!-- 指定要運行的類的方法,每2秒執行一次 -->
    <task:scheduled ref="taskJob" method="myMethod" cron=" 0/2 * * * * ?" />
    <!-- 指定要運行的類的方法,每2秒執行一次 -->
    <task:scheduled ref="taskJob2" method="myMethod" cron=" 0/3 * * * * ?" />
</task:scheduled-tasks>
</beans>

  二、Java普通類

// 定時任務1
import java.util.Date;
public class TaskJob {

    public void myMethod() {
        System.out.println(new Date().toLocaleString() + " 我是後臺任務1...");
    }
} 

//定時任務2
import java.util.Date;
public class TaskJob2 {

    public void myMethod() {
        System.out.println(new Date().toLocaleString() + " 我是後臺任務2...");
    }
}

 

結論

以上兩種方式,均可以輕鬆完成定時任務。使用哪一種,看我的喜愛。

 

驗證

啓動服務,控制檯每兩秒輸入一條信息,便可完成驗證。


 

    以上

謝謝!

2018/04/17 16:15 章力

於 陝西·西安·逸翠園二期

整理於採集分析項目

相關文章
相關標籤/搜索