springboot實(shí)現(xiàn)多實(shí)例crontab搶占定時(shí)任務(wù)(實(shí)例代碼)
github: https://github.com/jiasion/eslog
wechat:minghui-666
利用redisson實(shí)現(xiàn)多實(shí)例搶占定時(shí)任務(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è)定一個(gè)長(zhǎng)度10的定時(shí)任務(wù)線程池
taskRegistrar.setScheduler(Executors.newScheduledThreadPool(4));
}
}
RedissonManager.java - 分布式鎖的實(shí)現(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定時(shí)腳本掛靠實(shí)例
* 多實(shí)例會(huì)有重復(fù)調(diào)用問題 + 使用Redisson實(shí)現(xiàn)分布式鎖
* 業(yè)務(wù)邏輯必須加鎖 + 且需要保證 tryLock 等待時(shí)間小于cron的最小間隔執(zhí)行時(shí)間
* */
@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自動(dòng)解鎖
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個(gè)實(shí)例
總結(jié)
以上所述是小編給大家介紹的springboot實(shí)現(xiàn)多實(shí)例crontab搶占定時(shí)任務(wù),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)我們網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
上一篇:Java web實(shí)現(xiàn)動(dòng)態(tài)圖片驗(yàn)證碼的示例代碼
欄 目:Java
下一篇:Java如何把數(shù)組轉(zhuǎn)換為ArrayList
本文標(biāo)題:springboot實(shí)現(xiàn)多實(shí)例crontab搶占定時(shí)任務(wù)(實(shí)例代碼)
本文地址:http://www.jygsgssxh.com/a1/Java/8703.html
您可能感興趣的文章
- 01-10Java實(shí)現(xiàn)動(dòng)態(tài)模擬時(shí)鐘
- 01-10Springboot中@Value的使用詳解
- 01-10利用Java實(shí)現(xiàn)復(fù)制Excel工作表功能
- 01-10JavaWeb實(shí)現(xiàn)郵件發(fā)送功能
- 01-10Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘
- 01-10java實(shí)現(xiàn)液晶數(shù)字字體顯示當(dāng)前時(shí)間
- 01-10如何解決線程太多導(dǎo)致java socket連接池出現(xiàn)的問題
- 01-10springboot實(shí)現(xiàn)文件上傳步驟解析
- 01-10java實(shí)現(xiàn)的順時(shí)針/逆時(shí)針打印矩陣操作示例
- 01-10springboot jta atomikos實(shí)現(xiàn)分布式事物管理


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10Java實(shí)現(xiàn)動(dòng)態(tài)模擬時(shí)鐘
- 01-10Springboot中@Value的使用詳解
- 01-10JavaWeb實(shí)現(xiàn)郵件發(fā)送功能
- 01-10利用Java實(shí)現(xiàn)復(fù)制Excel工作表功能
- 01-10Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘
- 01-10java基于poi導(dǎo)出excel透視表代碼實(shí)例
- 01-10java實(shí)現(xiàn)液晶數(shù)字字體顯示當(dāng)前時(shí)間
- 01-10基于Java驗(yàn)證jwt token代碼實(shí)例
- 01-10Java動(dòng)態(tài)顯示當(dāng)前日期和時(shí)間
- 01-10淺談Java中真的只有值傳遞么
隨機(jī)閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 04-02jquery與jsp,用jquery
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子


