雷火电竞-中国电竞赛事及体育赛事平台

歡迎來(lái)到入門教程網(wǎng)!

Java

當(dāng)前位置:主頁(yè) > 軟件編程 > Java >

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

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:Java|點(diǎn)擊:

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

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有