SpringBoot使用shedlock做定時(shí)任務(wù)的實(shí)現(xiàn)示例
1、pom依賴(lài)
<dependency> <groupId>net.javacrumbs.shedlock</groupId> <artifactId>shedlock-spring</artifactId> <version>4.15.1</version> </dependency> <dependency> <groupId>net.javacrumbs.shedlock</groupId> <artifactId>shedlock-provider-redis-spring</artifactId> <version>4.15.1</version> </dependency>
2、shedlock使用Redis鎖
解決多實(shí)例下任務(wù)重復(fù)執(zhí)行問(wèn)題。
@Configuration public class ScheduleLockConfig { @Bean public LockProvider lockProvider(RedisConnectionFactory redisConnectionFactory) { return new RedisLockProvider(redisConnectionFactory); } }
3、配置定時(shí)任務(wù)線程池
解決多任務(wù)并發(fā)執(zhí)行時(shí)等待問(wèn)題。
@Configuration public class ScheduleConfig implements SchedulingConfigurer { @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); taskScheduler.setPoolSize(10); taskScheduler.setThreadNamePrefix("taskScheduler-"); taskRegistrar.setScheduler(taskScheduler); } }
4、定時(shí)任務(wù)代碼
@Configuration public class ScheduleTask { /** * Concurrent task 1. */ @Scheduled(cron = "0/5 * * * * ?") @SchedulerLock(name = "syncTask1") public void syncTask1() throws InterruptedException { System.err.println("并發(fā)執(zhí)行任務(wù)1: " + LocalDateTime.now()); Thread.sleep(7000); } /** * Concurrent task 2. */ @Scheduled(cron = "0/5 * * * * ?") @SchedulerLock(name = "syncTask2") public void syncTask2() { System.err.println("并發(fā)執(zhí)行任務(wù)2: " + LocalDateTime.now()); } }
到此這篇關(guān)于SpringBoot使用shedlock做定時(shí)任務(wù)的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)SpringBoot shedlock定時(shí)任務(wù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot項(xiàng)目中實(shí)現(xiàn)文件上傳功能的示例
這篇文章主要介紹了Spring Boot項(xiàng)目中實(shí)現(xiàn)文件上傳功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12Java中Map集合遍歷的多種實(shí)現(xiàn)方式
本文主要介紹了Java中Map集合遍歷的多種實(shí)現(xiàn)方式,包括KeySet、EntrySet、Lambda及Stream API,具有一定的參考價(jià)值,感興趣的可以了解一下2025-05-05mybatis查詢(xún)數(shù)據(jù)賦值到model里面為空的解決
這篇文章主要介紹了mybatis查詢(xún)數(shù)據(jù)賦值到model里面為空的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01Java基于MySQL實(shí)現(xiàn)學(xué)生管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java基于MySQL實(shí)現(xiàn)學(xué)生管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01