這篇文章主要介紹了springboot怎么實現(xiàn)多實例crontab搶占定時任務(wù),具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)于2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都網(wǎng)站制作、成都做網(wǎng)站網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元孟村做網(wǎng)站,已為上家服務(wù),為孟村各地企業(yè)和個人服務(wù),聯(lián)系電話:18982081108
利用redisson實現(xiàn)多實例搶占定時任務(wù)
pom.xml
<dependency> <groupId>org.redisson</groupId> <artifactId>redisson</artifactId> <version>3.12.0</version> </dependency>
Kernel.java - 重寫多線程調(diào)度
package com.brand.log.scheduler; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.SchedulingConfigurer; import org.springframework.scheduling.config.ScheduledTaskRegistrar; import java.util.concurrent.Executors; @Configuration public class Kernel implements SchedulingConfigurer { @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { //設(shè)定一個長度10的定時任務(wù)線程池 taskRegistrar.setScheduler(Executors.newScheduledThreadPool(4)); } }
RedissonManager.java - 分布式鎖的實現(xiàn)
package com.brand.log.util; import lombok.extern.slf4j.Slf4j; import org.redisson.Redisson; import org.redisson.config.Config; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; @Component @Slf4j public class RedissonManager { @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; private Redisson redisson = null; private Config config = new Config(); @PostConstruct private void init() { try { config.useSingleServer().setAddress("redis://" + host + ":" + port); log.info("redisson address {} {}", host, port); redisson = (Redisson) Redisson.create(config); log.info("Redisson 初始化完成"); } catch (Exception e) { log.error("init Redisson error ", e); } } public Redisson getRedisson() { return redisson; } }
CronSynData.java
package com.brand.log.scheduler; import com.brand.log.util.DateFormatV1; import com.brand.log.util.RedisUtil; import com.brand.log.util.RedissonManager; import lombok.extern.slf4j.Slf4j; import org.redisson.Redisson; import org.redisson.api.RLock; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.concurrent.TimeUnit; @Component @Slf4j public class CronSynData { @Autowired RedissonManager redissonManager; @Autowired RedisUtil redisUtil; @Autowired DateFormatV1 dateFormatV1; private String lokFlag = ".handleKernel"; private Redisson redisson = null; /* * java定時腳本掛靠實例 * 多實例會有重復(fù)調(diào)用問題 + 使用Redisson實現(xiàn)分布式鎖 * 業(yè)務(wù)邏輯必須加鎖 + 且需要保證 tryLock 等待時間小于cron的最小間隔執(zhí)行時間 * */ @Scheduled(cron = "*/10 * * * * *") public void handleKernel() { redisson = redissonManager.getRedisson(); if (redisson != null) { RLock lock = redisson.getLock(this.getClass().getName() + lokFlag); Boolean stat = false; try { // 嘗試加鎖,立即返回,最多等待5s自動解鎖 stat = lock.tryLock(0, 5, TimeUnit.SECONDS); if (stat) { log.info("{} 取鎖成功!{}",this.getClass().getName(), Thread.currentThread().getName()); redisUtil.checkCount("log:limit_", dateFormatV1.getDate("HH", "GMT+8"), 60*10, 1000); } else { log.info("{}沒有獲取到鎖:{}", this.getClass().getName(), Thread.currentThread().getName()); } } catch (InterruptedException e) { log.error("Redisson 獲取分布式鎖異常", e); if (!stat){ return; } lock.unlock(); } } } }
kibana - 6個實例
感謝你能夠認真閱讀完這篇文章,希望小編分享的“springboot怎么實現(xiàn)多實例crontab搶占定時任務(wù)”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
分享題目:springboot怎么實現(xiàn)多實例crontab搶占定時任務(wù)
網(wǎng)站鏈接:http://chinadenli.net/article40/jsiheo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、移動網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈、企業(yè)網(wǎng)站制作、電子商務(wù)、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)