SpringBoot定時(shí)任務(wù)兩種(Spring Schedule 與 Quartz 整合 )實(shí)現(xiàn)方法
前言
最近在項(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í)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot中實(shí)現(xiàn)定時(shí)任務(wù)的4種方式詳解
- SpringBoot中實(shí)現(xiàn)定時(shí)任務(wù)的幾種方式
- SpringBoot2 task scheduler 定時(shí)任務(wù)調(diào)度器四種方式
- SpringBoot下使用定時(shí)任務(wù)的方式全揭秘(6種)
- SpringBoot實(shí)現(xiàn)固定和動(dòng)態(tài)定時(shí)任務(wù)的三種方法
- Springboot實(shí)現(xiàn)定時(shí)任務(wù)的4種方式舉例詳解
- SpringBoot實(shí)現(xiàn)定時(shí)任務(wù)的三種方式小結(jié)
- SpringBoot最新定時(shí)任務(wù)的7種實(shí)現(xiàn)方案
相關(guān)文章
深入理解Java設(shè)計(jì)模式之狀態(tài)模式
這篇文章主要介紹了JAVA設(shè)計(jì)模式之職責(zé)鏈模式的的相關(guān)資料,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解2021-11-11Spring?Boot?教程之創(chuàng)建項(xiàng)目的三種方式
這篇文章主要分享了Spring?Boot?教程之創(chuàng)建項(xiàng)目的三種方式,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-05-05SpringBoot整合mybatis使用Druid做連接池的方式
這篇文章主要介紹了SpringBoot整合mybatis使用Druid做連接池的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08Java AOP動(dòng)態(tài)代理詳細(xì)介紹
AOP是一種設(shè)計(jì)思想,是軟件設(shè)計(jì)領(lǐng)域中的面向切面編程,它是面向?qū)ο缶幊痰囊环N補(bǔ)充和完善。本文將用Java實(shí)現(xiàn)AOP代理的三種方式,需要的可以參考一下2022-08-08Spring?Boot中KafkaListener的介紹、原理和使用方法案例詳解
本文介紹了Spring Boot中 @KafkaListener 注解的介紹、原理和使用方法,通過本文的介紹,我們希望讀者能夠更好地理解Spring Boot中 @KafkaListener 注解的使用方法,并在項(xiàng)目中更加靈活地應(yīng)用2023-09-09Java利用ITextPdf庫生成PDF預(yù)覽文件的具體實(shí)現(xiàn)
這篇文章主要給大家介紹了Java利用ITextPdf庫生成PDF預(yù)覽文件的具體實(shí)現(xiàn),文中通過代碼示例和圖文給大家介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2024-04-04