亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring整合TimerTask實(shí)現(xiàn)定時(shí)任務(wù)調(diào)度

 更新時(shí)間:2016年12月29日 15:44:37   作者:zdp072  
這篇文章主要介紹了Spring整合TimerTask實(shí)現(xiàn)定時(shí)任務(wù)調(diào)度的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

一. 前言

最近在公司的項(xiàng)目中用到了定時(shí)任務(wù), 本篇博文將會(huì)對(duì)TimerTask定時(shí)任務(wù)進(jìn)行總結(jié), 其實(shí)TimerTask在實(shí)際項(xiàng)目中用的不多,
因?yàn)樗荒茉谥付〞r(shí)間運(yùn)行, 只能讓程序按照某一個(gè)頻度運(yùn)行.

二. TimerTask

JDK中Timer是一個(gè)定時(shí)器類, 它可以為指定的定時(shí)任務(wù)進(jìn)行配置.
JDK中TimerTask是一個(gè)定時(shí)任務(wù)類, 該類實(shí)現(xiàn)了Runnable接口, 是一個(gè)抽象類, 我們可以繼承這個(gè)類, 實(shí)現(xiàn)定時(shí)任務(wù).

/** 
 * 繼承TimerTask實(shí)現(xiàn)定時(shí)任務(wù) 
 */ 
public class MyTask extends TimerTask { 
 
  @Override 
  public void run() { 
    String currentTime = new SimpleDateFormat("yyy-MM-dd hh:mm:ss").format(new Date()); 
    System.out.println(currentTime + " 定時(shí)任務(wù)正在執(zhí)行..."); 
  } 
 
  public static void main(String[] args) { 
    Timer timer = new Timer(); 
     
    // 1秒鐘執(zhí)行一次的任務(wù), 參數(shù)為: task, delay, peroid 
    timer.schedule(new MyTask(), 2000, 1000); 
  } 
} 

三. 整合Spring

兩個(gè)核心類: ScheduledTimerTask, TimerFactoryBean
ScheduledTimerTask類是對(duì)TimerTask的包裝器實(shí)現(xiàn), 通過該類可以為這個(gè)任務(wù)定義觸發(fā)器信息.
TimerFactoryBean類可以讓Spring使用配置創(chuàng)建觸發(fā)器, 并為一組指定的ScheduledTimerTask bean自動(dòng)創(chuàng)建Timer實(shí)例.

1. 引入Jar包: spring.jar, commons-logging.jar
2. 定時(shí)調(diào)度業(yè)務(wù)類:

/** 
 * 定時(shí)調(diào)度業(yè)務(wù)類 
 */ 
public class TaskService extends TimerTask { 
  private int count = 1; 
 
  public void run() { 
    System.out.println("第" + count + "次執(zhí)行定時(shí)任務(wù)"); 
    count++; 
  } 
} 

3. 核心配置:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  xsi:schemaLocation="http://www.springframework.org/schema/beans  
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
   
  <bean id="taskService" class="com.zdp.service.TaskService"></bean> 
  <bean id="scheduledTimerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"> 
    <property name="timerTask" ref="taskService" /> 
     
    <!-- 每隔一天執(zhí)行一次配置: 24*60*60*1000 --> 
    <!-- 每1秒鐘程序執(zhí)行一次 --> 
    <property name="period" value="1000" /> 
     
    <!-- 程序啟動(dòng)4秒鐘后開始執(zhí)行 --> 
    <property name="delay" value="4000" /> 
  </bean> 
   
  <bean id="timerFactoryBean" class="org.springframework.scheduling.timer.TimerFactoryBean"> 
    <property name="scheduledTimerTasks"> 
      <list> 
        <ref bean="scheduledTimerTask" /> 
      </list> 
    </property> 
  </bean> 
</beans> 

4. 測(cè)試類:

public class Main { 
  public static void main(String[] args) { 
    // 加載spring配置文件 
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 
    System.out.println("<<-------- 啟動(dòng)定時(shí)任務(wù) -------- >>"); 
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 
    while (true) { 
      try { 
        if (reader.readLine().equals("quit")) { 
          System.out.println("<<-------- 退出定時(shí)任務(wù) -------- >>"); 
          System.exit(0); 
        } 
      } catch (IOException e) { 
        throw new RuntimeException("error happens...", e); 
      } 
    } 
  } 
} 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java自動(dòng)添加重寫的toString方法詳解

    Java自動(dòng)添加重寫的toString方法詳解

    在本篇文章里小編給大家整理了關(guān)于Java自動(dòng)添加重寫的toString方法總結(jié),需要的朋友們學(xué)習(xí)下。
    2019-07-07
  • Spring異常實(shí)現(xiàn)統(tǒng)一處理的方法

    Spring異常實(shí)現(xiàn)統(tǒng)一處理的方法

    這篇文章主要介紹了Spring異常實(shí)現(xiàn)統(tǒng)一處理的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-12-12
  • SpringBoot屬性綁定與bean屬性校驗(yàn)實(shí)現(xiàn)方法詳解

    SpringBoot屬性綁定與bean屬性校驗(yàn)實(shí)現(xiàn)方法詳解

    這篇文章主要介紹了SpringBoot屬性綁定與bean屬性校驗(yàn)實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-11-11
  • 解析Java中所有錯(cuò)誤和異常的父類java.lang.Throwable

    解析Java中所有錯(cuò)誤和異常的父類java.lang.Throwable

    這篇文章主要介紹了Java中所有錯(cuò)誤和異常的父類java.lang.Throwable,文章中簡(jiǎn)單地分析了其源碼,說明在代碼注釋中,需要的朋友可以參考下
    2016-03-03
  • 在SpringBoot環(huán)境中使用Mockito進(jìn)行單元測(cè)試的示例詳解

    在SpringBoot環(huán)境中使用Mockito進(jìn)行單元測(cè)試的示例詳解

    Mockito是一個(gè)流行的Java?mocking框架,它允許開發(fā)者以簡(jiǎn)單直觀的方式創(chuàng)建和使用模擬對(duì)象(mocks),Mockito特別適用于在Spring?Boot環(huán)境中進(jìn)行單元測(cè)試,所以本文介紹了在SpringBoot環(huán)境中使用Mockito進(jìn)行單元測(cè)試的示例,需要的朋友可以參考下
    2024-11-11
  • Spring-data-redis操作redis知識(shí)總結(jié)

    Spring-data-redis操作redis知識(shí)總結(jié)

    這篇文章主要介紹了Spring-data-redis操作redis知識(shí)總結(jié),spring-data-redis是spring-data模塊的一部分,專門用來支持在spring管理項(xiàng)目對(duì)redis的操作。
    2017-04-04
  • Mybatis-plus?sql注入及防止sql注入詳解

    Mybatis-plus?sql注入及防止sql注入詳解

    mybatis-plus提供了許多默認(rèn)單表 CRUD 語句,對(duì)于其他SQL情況愛莫能助,下面這篇文章主要給大家介紹了關(guān)于Mybatis-plus?sql注入及防止sql注入的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-10-10
  • Java算法設(shè)計(jì)與分析分治算法

    Java算法設(shè)計(jì)與分析分治算法

    這篇文章主要介紹了Java算法設(shè)計(jì)與分析分治算法,一般分治算法在正文中分解為兩個(gè)即以上的遞歸調(diào)用,并且子類問題一般是不想交的
    2022-07-07
  • 淺談Spring IoC容器的依賴注入原理

    淺談Spring IoC容器的依賴注入原理

    這篇文章主要介紹了淺談Spring IoC容器的依賴注入原理,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-03-03
  • spring boot如何添加攔截器

    spring boot如何添加攔截器

    本篇文章主要介紹了spring boot如何添加攔截器,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-04-04

最新評(píng)論