基于Redis實現分布式鎖,避免重復執行定時任務

Spring提供了定時任務的功能,但是在多個實例的集群中,會出現定時任務重復執行多次的情況。

使用Qutaz框架自帶的分布式定時任務可以很好的解決這個問題,但是講道理功能有些過于強大,對于需求不高,乃至可以一定程度上允許失誤的簡單任務中,性價比比較低。

使用task任務時,可以通過在redis等緩存、數據庫中創建鎖來實現避免重復執行任務的功能。

基本思路如下:

  • 在redis中設置一個key作為鎖,值為時間戳
  • 嘗試用setnx方法直接設置鎖,若成功則直接執行任務。
  • 若設置失敗,使用getset方法獲取當前鎖并更新時間戳
  • 若獲取的時間戳在有效期內,則不執行任務,否則執行任務

當然這種實現方法是很粗糙的。對于高并發的頻繁定時任務處理很不完美。若執行失敗也會導致任務丟失。

本方案實現的處理場景是間隔比較長的數據處理定時任務。服務器集群也只有三個節點,上線運行效果良好。

簡要實現代碼如下:

private boolean getLock(String key){String syncKey = "sync_lock_"+key;long curr = System.currentTimeMillis();String time = curr+"";long has = redisTemplate.setnx(syncKey,time);if(has == 1){return true;}else{String lock = redisTemplate.getSet(syncKey,time);if(lock == null){return true;}else{long l = NumberUtils.toLong(lock);//三十秒以內都算有效鎖return Math.abs(curr-l)<30000;}}}
復制代碼

轉載于:https://juejin.im/post/5a31e9f8f265da43333e6a1b

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/278893.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/278893.shtml
英文地址,請注明出處:http://en.pswp.cn/news/278893.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Input Director使用一個鍵盤和鼠標即可控制多臺Windows計算機

The problem is having two or more PC’s and having to go back and forth between workstation. Input Director solves the problem by allowing you to control multiple Windows systems with only one keyboard and mouse on the Master PC. 問題是擁有兩臺或更多臺PC…

viper4android 生效,另一種讓V4a音效在Poweramp上生效的方法

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓本人按照網上的方法進Poweramp設置—音頻—高級選項—直接音量控制—不打勾后 V4a音效沒有生效 我又把V4a音效兼容模式里的正常模式切換成為兼容模式 結果還是不行......后來我郁悶了三天三夜有一天我去了v4a官網論壇看到了admin帖…

[學習筆記]狀壓dp

狀壓 \(dp\) 1、[SDOI2009]Bill的挑戰 \(f[i][j]\) 表示匹配到字符串的第 \(i\) 位狀態為 \(j\) 的方案數 那么方程就很明顯了&#xff0c;每次枚舉第 \(i\) 位的字母 \(alpha\) 然后 \(O(n)\) 判斷就好了 時間復雜度 \(O(26Tlen2^nn)\) \(Code\ Below:\) #include <bits/st…

excel導入csv文件_如何將包含以0開頭的列的CSV文件導入Excel

excel導入csv文件Microsoft Excel will automatically convert data columns into the format that it thinks is best when opening comma-separated data files. For those of us that don’t want our data changed, we can change that behavior. Microsoft Excel將在打開…

MySQL之進化篇

MySQL之實用篇 MySQL之牛刀小試 子查詢是指出現在其他SQL語句內的SELECT子句. 例如: SELECT * FROM t1 WHERE column1 (SELECT column2 FROM t2) 其中 SELECT * FRIN t1 稱為outerQuery SELECT column2 FROM t2 稱為subQuery 注意:子查詢指嵌套在查詢內部,且必須始終出現在圓括…

android 9.0新ui,SystemUI分析(Android9.0)

8種機械鍵盤軸體對比本人程序員&#xff0c;要買一個寫代碼的鍵盤&#xff0c;請問紅軸和茶軸怎么選&#xff1f;一、SystemUI組成SystemUI是Android的系統界面&#xff0c;包括狀態欄statusbar、鎖屏keyboard、任務列表recents等等&#xff0c;都繼承于SystemUI這個類&#xf…

WMI技術介紹和應用——WMI概述

https://blog.csdn.net/breaksoftware/article/details/8424317轉載于:https://www.cnblogs.com/diyunpeng/p/9982885.html

解決App啟動時白屏的問題

第一次 03-25 11:02:34.431 6908-6908/com.newenergyjinfu.jytz D/App: before_onCreate: 239 03-25 11:02:34.513 6908-6908/com.newenergyjinfu.jytz D/App: after_initOkGo( initPicasso): 316 03-25 11:02:34.570 6908-6908/com.newenergyjinfu.jytz D/App: after_ J…

chromebook刷機_如何為不支持Chrome操作系統的網站欺騙Chromebook用戶代理

chromebook刷機Not all browsers handle websites the same, and if they don’t support your operating system or browser, you could be denied access. Luckily, you can spoof the user agent on Chrome OS to make it look like you use a completely different system.…

什么時候可以升級HarmonyOS,華為鴻蒙OS即將迎來升級 手機版本或仍需時間

原標題&#xff1a;華為鴻蒙OS即將迎來升級 手機版本或仍需時間在2019年的華為開發者大會上&#xff0c;華為消費者業務CEO余承東正式對外發布了HarmonyOS。時隔一年后&#xff0c;華為開發者大會2020即將拉開帷幕。此次大會&#xff0c;HarmonyOS無疑仍會是重頭戲之一&#xf…

Shell_mysql命令以及將數據導入Mysql數據庫

連接MYSQL數據庫 mysql -h${db_ip} -u${db_user} -p${db_pawd} -P${db_port} -D${db_name} -s -e "${sql}" db_ip&#xff1a;主機地址 db_user &#xff1a;數據庫用戶名 db_pwd&#xff1a;密碼 db_port&#xff1a;端口號 db_name&#xff1a;數據庫名稱 sql&…

cocos android-1,cocos2dx在windows下開發,編譯到android上(1)

轉自&#xff1a;http://www.2cto.com/kf/201205/130697.html下面我給大家介紹下&#xff0c;用vs2010開發cocos2dx&#xff0c;然后如何使其編譯到android上。步驟如下&#xff1a;1、必要條件&#xff0c;你的eclipse能把代碼編譯到安卓手機或虛擬機上&#xff0c;如果這一步…

中藥ppi網絡圖太雜亂_太雜亂了嗎? 這是您的iPhone,iPad,Android或臺式機的15張簡約壁紙...

中藥ppi網絡圖太雜亂Busy wallpaper images don’t work very well on your iPhone, iPad, or any device where you need to have lots of icons on the screen. Here’s a set of minimalistic wallpaper images that won’t clutter up your desktop. 繁忙的墻紙圖像在iPhon…

算法61---兩個字符串的最小ASCII刪除和【動態規劃】

一、題目&#xff1a; 給定兩個字符串s1, s2&#xff0c;找到使兩個字符串相等所需刪除字符的ASCII值的最小和。 示例 1: 輸入: s1 "sea", s2 "eat" 輸出: 231 解釋: 在 "sea" 中刪除 "s" 并將 "s" 的值(115)加入總和。 在…

android設置時間widget,【Android】時間與日期Widget(DatePicker 與 TimePicker)

public class Activity01 extends Activity{TextViewm_TextView;//聲明dataPickerDatePickerm_DatePicker;//聲明TimePickerTimePickerm_TimePicker;Button m_dpButton;Button m_tpButton;//java中的Calendar類Calendar c;/** Called when the activity is first created. */Ov…

初學者java學習計劃_初學者:計劃在Windows 7 Media Center中錄制直播電視的時間

初學者java學習計劃If you’re a new user to Windows 7 Media Center you know it can act as a DVR and pause or record Live TV. You can set up a schedule for it to record your favorite TV programs as well. 如果您是Windows 7 Media Center的新用戶&#xff0c;則知…

雙數據源配置

從此抄錄&#xff1a;https://blog.csdn.net/ll535299/article/details/78203634 1、先配置兩個數據源&#xff0c;附上主要代碼&#xff0c;給自己回憶&#xff0c;詳解見開頭鏈接 <!-- 配置數據源 --> <bean id"szDS" class"com.alibaba.druid.pool.…

如何在Office 2007中查看關于對話框和版本信息

One of our favorite readers wrote in today asking how to tell if his Word 2007 installation was running Service Pack 1, since he couldn’t find the About dialog, which got me thinking… I bet most people don’t know where it is! 我們最喜歡的一位讀者今天寫信…

windows全局熱鍵_在Windows中創建快捷方式或熱鍵以清除剪貼板

windows全局熱鍵Have you ever copied something to the clipboard that you don’t want to leave there in case somebody else is going to use your computer? Sure, you can copy something else to the clipboard real quick, but can’t you just make a shortcut or h…

android+notepad教程,Android Sample學習——NotePad

android.view.Menu專場Interface for managing the items in a menu.By default, every Activity supports an options menu of actions or options. You can add items to this menu and handle clicks on your additions. The easiest way of adding menu items is inflating…