欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot定時(shí)任務(wù)兩種(Spring Schedule 與 Quartz 整合 )實(shí)現(xiàn)方法

 更新時(shí)間:2017年03月13日 16:21:26   作者:JavaNoob  
本篇文章主要介紹了SpringBoot定時(shí)任務(wù)兩種(Spring Schedule 與 Quartz 整合 )實(shí)現(xiàn)方法,詳細(xì)的介紹了Spring Schedule 與 Quartz 整合的兩種方法,有興趣的可以了解一下。

前言

最近在項(xiàng)目中使用到定時(shí)任務(wù),之前一直都是使用Quartz 來實(shí)現(xiàn),最近看Spring 基礎(chǔ)發(fā)現(xiàn)其實(shí)Spring 提供 Spring Schedule 可以幫助我們實(shí)現(xiàn)簡單的定時(shí)任務(wù)功能。

下面說一下兩種方式在Spring Boot 項(xiàng)目中的使用。

Spring Schedule 實(shí)現(xiàn)定時(shí)任務(wù)

Spring Schedule 實(shí)現(xiàn)定時(shí)任務(wù)有兩種方式 1. 使用XML配置定時(shí)任務(wù), 2. 使用 @Scheduled 注解。 因?yàn)槭荢pring Boot 項(xiàng)目 可能盡量避免使用XML配置的形式,主要說注解的形式.

Spring Schedule 提供三種形式的定時(shí)任務(wù):

固定等待時(shí)間 @Scheduled(fixedDelay = 時(shí)間間隔 )

@Component
public class ScheduleJobs {
  public final static long SECOND = 1 * 1000;
  FastDateFormat fdf = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");


  @Scheduled(fixedDelay = SECOND * 2)
  public void fixedDelayJob() throws InterruptedException {
    TimeUnit.SECONDS.sleep(2);
    System.out.println("[FixedDelayJob Execute]"+fdf.format(new Date()));
  }
}

固定間隔時(shí)間 @Scheduled(fixedRate = 時(shí)間間隔 )

@Component
public class ScheduleJobs {
  public final static long SECOND = 1 * 1000;
  FastDateFormat fdf = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");


  @Scheduled(fixedRate = SECOND * 4)
  public void fixedRateJob() {
    System.out.println("[FixedRateJob Execute]"+fdf.format(new Date()));
  }
}

Corn表達(dá)式 @Scheduled(cron = Corn表達(dá)式)

@Component
public class ScheduleJobs {
  public final static long SECOND = 1 * 1000;
  FastDateFormat fdf = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");


  @Scheduled(cron = "0/4 * * * * ?")
  public void cronJob() {
    System.out.println("[CronJob Execute]"+fdf.format(new Date()));
  }
}

Spring Boot 整合 Quartz 實(shí)現(xiàn)定時(shí)任務(wù)

添加Maven依賴

    <dependency>
      <groupId>org.quartz-scheduler</groupId>
      <artifactId>quartz</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
    </dependency>

Spring Boot 整合 Quartz

Spring 項(xiàng)目整合 Quartz 主要依靠添加 SchedulerFactoryBean 這個(gè) FactoryBean ,所以在maven 依賴中添加 spring-context-support 。

首先添加 QuartzConfig 類 來聲明相關(guān)Bean

@Configuration
public class QuartzConfig {
  @Autowired
  private SpringJobFactory springJobFactory;

  @Bean
  public SchedulerFactoryBean schedulerFactoryBean() {
    SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
    schedulerFactoryBean.setJobFactory(springJobFactory);
    return schedulerFactoryBean;
  }

  @Bean
  public Scheduler scheduler() {
    return schedulerFactoryBean().getScheduler();
  }
}

這里我們需要注意 我注入了一個(gè) 自定義的JobFactory ,然后 把其設(shè)置為SchedulerFactoryBean 的 JobFactory。其目的是因?yàn)槲以诰唧w的Job 中 需要Spring 注入一些Service。

 所以我們要自定義一個(gè)jobfactory, 讓其在具體job 類實(shí)例化時(shí) 使用Spring 的API 來進(jìn)行依賴注入。

SpringJobFactory 具體實(shí)現(xiàn):

@Component
public class SpringJobFactory extends AdaptableJobFactory {

  @Autowired
  private AutowireCapableBeanFactory capableBeanFactory;

  @Override
  protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
    Object jobInstance = super.createJobInstance(bundle);
    capableBeanFactory.autowireBean(jobInstance);
    return jobInstance;
  }
}

具體使用 (摘取自項(xiàng)目代碼):

@Service
public class QuartzEventServiceImpl implements QuartzEventService {
  private static final String JOB_GROUP = "event_job_group";
  private static final String TRIGGER_GROUP = "event_trigger_group";
  @Autowired
  private Scheduler scheduler;

  @Override
  public void addQuartz(Event event) throws SchedulerException {
    JSONObject eventData = JSONObject.parseObject(event.getEventData());
    Date triggerDate = eventData.getDate("date");
    JobDetail job = JobBuilder.newJob(EventJob.class).withIdentity(event.getId().toString(), JOB_GROUP).usingJobData(buildJobDateMap(event)).build();
    Trigger trigger = TriggerBuilder.newTrigger().withIdentity(event.getId().toString(), TRIGGER_GROUP).startAt(triggerDate).build();
    scheduler.scheduleJob(job, trigger);
  }

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

相關(guān)文章

  • springboot更新配置Swagger3的一些小技巧

    springboot更新配置Swagger3的一些小技巧

    今天給大家分享springboot更新配置Swagger3的方法,大家需要注意Swagger3版本需要引入依賴,具體示例代碼參考下本文
    2021-07-07
  • 深入理解Java設(shè)計(jì)模式之狀態(tài)模式

    深入理解Java設(shè)計(jì)模式之狀態(tài)模式

    這篇文章主要介紹了JAVA設(shè)計(jì)模式之職責(zé)鏈模式的的相關(guān)資料,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解
    2021-11-11
  • java鎖升級(jí)過程過程詳解

    java鎖升級(jí)過程過程詳解

    這篇文章主要介紹了Java鎖升級(jí)的實(shí)現(xiàn)過程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-08-08
  • Spring MVC的國際化實(shí)現(xiàn)代碼

    Spring MVC的國際化實(shí)現(xiàn)代碼

    本篇文章主要介紹了Spring MVC的國際化實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • Java?多線程并發(fā)LockSupport

    Java?多線程并發(fā)LockSupport

    這篇文章主要介紹了Java?多線程并發(fā)LockSupport,LockSupport?類是用于創(chuàng)建鎖和其他同步類的基本線程阻塞原語,更多相關(guān)內(nèi)容需要得小伙伴可以參考一下下面文章內(nèi)容
    2022-06-06
  • Spring?Boot?教程之創(chuàng)建項(xiàng)目的三種方式

    Spring?Boot?教程之創(chuàng)建項(xiàng)目的三種方式

    這篇文章主要分享了Spring?Boot?教程之創(chuàng)建項(xiàng)目的三種方式,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-05-05
  • SpringBoot整合mybatis使用Druid做連接池的方式

    SpringBoot整合mybatis使用Druid做連接池的方式

    這篇文章主要介紹了SpringBoot整合mybatis使用Druid做連接池的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Java AOP動(dòng)態(tài)代理詳細(xì)介紹

    Java AOP動(dòng)態(tài)代理詳細(xì)介紹

    AOP是一種設(shè)計(jì)思想,是軟件設(shè)計(jì)領(lǐng)域中的面向切面編程,它是面向?qū)ο缶幊痰囊环N補(bǔ)充和完善。本文將用Java實(shí)現(xiàn)AOP代理的三種方式,需要的可以參考一下
    2022-08-08
  • Spring?Boot中KafkaListener的介紹、原理和使用方法案例詳解

    Spring?Boot中KafkaListener的介紹、原理和使用方法案例詳解

    本文介紹了Spring Boot中 @KafkaListener 注解的介紹、原理和使用方法,通過本文的介紹,我們希望讀者能夠更好地理解Spring Boot中 @KafkaListener 注解的使用方法,并在項(xiàng)目中更加靈活地應(yīng)用
    2023-09-09
  • Java利用ITextPdf庫生成PDF預(yù)覽文件的具體實(shí)現(xiàn)

    Java利用ITextPdf庫生成PDF預(yù)覽文件的具體實(shí)現(xiàn)

    這篇文章主要給大家介紹了Java利用ITextPdf庫生成PDF預(yù)覽文件的具體實(shí)現(xiàn),文中通過代碼示例和圖文給大家介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下
    2024-04-04

最新評(píng)論