基于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.mzph.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…