最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot實現(xiàn)多實例crontab搶占定時任務(wù)(實例代碼)

 更新時間:2020年01月08日 14:04:41   作者:總有刁民想呀么想害朕  
這篇文章主要介紹了springboot實現(xiàn)多實例crontab搶占定時任務(wù),本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下

github: https://github.com/jiasion/eslog 

wechat:minghui-666

利用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個實例

總結(jié)

以上所述是小編給大家介紹的springboot實現(xiàn)多實例crontab搶占定時任務(wù),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

相關(guān)文章

  • SpringBoot+Mybatis-plus實現(xiàn)分頁查詢的示例代碼

    SpringBoot+Mybatis-plus實現(xiàn)分頁查詢的示例代碼

    本文主要介紹了SpringBoot+Mybatis-plus實現(xiàn)分頁查詢的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-02-02
  • Java多線程編程之訪問共享對象和數(shù)據(jù)的方法

    Java多線程編程之訪問共享對象和數(shù)據(jù)的方法

    這篇文章主要介紹了Java多線程編程之訪問共享對象和數(shù)據(jù)的方法,多個線程訪問共享對象和數(shù)據(jù)的方式有兩種情況,本文分別給出代碼實例,需要的朋友可以參考下
    2015-05-05
  • 詳談Spring對IOC的理解(推薦篇)

    詳談Spring對IOC的理解(推薦篇)

    下面小編就為大家?guī)硪黄斦凷pring對IOC的理解(推薦篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • MyBatis中的mapper.xml配置教程

    MyBatis中的mapper.xml配置教程

    這篇文章主要介紹了MyBatis中的mapper.xml配置,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2024-01-01
  • 解決IDEA光標(biāo)變成白色粗條的問題

    解決IDEA光標(biāo)變成白色粗條的問題

    這篇文章主要介紹了解決IDEA光標(biāo)變成白色粗條的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • idea啟動與jar包啟動中使用resource資源文件路徑的問題

    idea啟動與jar包啟動中使用resource資源文件路徑的問題

    這篇文章主要介紹了idea啟動與jar包啟動中使用resource資源文件路徑的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • 一文徹底掌握RocketMQ 的存儲模型

    一文徹底掌握RocketMQ 的存儲模型

    這篇文章主要介紹了RocketMQ 的存儲模型,本文的重點在于分析 BrokerServer 的消息存儲模型,筆者按照自己的理解 , 嘗試分析 RocketMQ 的存儲模型,需要的朋友可以參考下
    2022-12-12
  • SpringBoot項目中使用AOP的方法

    SpringBoot項目中使用AOP的方法

    本篇文章主要介紹了SpringBoot項目中使用AOP的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • 詳解SpringBoot 多線程處理任務(wù) 無法@Autowired注入bean問題解決

    詳解SpringBoot 多線程處理任務(wù) 無法@Autowired注入bean問題解決

    這篇文章主要介紹了詳解SpringBoot 多線程處理任務(wù) 無法@Autowired注入bean問題解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-06-06
  • [Spring MVC] -簡單表單提交實例

    [Spring MVC] -簡單表單提交實例

    本篇文章主要介紹了[Spring MVC] -簡單表單提交實例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。
    2016-12-12

最新評論

新建县| 平利县| 英超| 衡阳县| 昆明市| 海盐县| 桑日县| 兴文县| 蒙自县| 田林县| 如东县| 娱乐| 泽普县| 峨眉山市| 扶风县| 黎川县| 庄河市| 南宁市| 广安市| 安国市| 宣城市| 合山市| 大化| 九江县| 云梦县| 越西县| 昌都县| 富裕县| 长子县| 正宁县| 瑞安市| 乐昌市| 枞阳县| 临沭县| 磐安县| 阿尔山市| 乌海市| 仁怀市| 安泽县| 张家川| 阳山县|