当前位置: 首页 > news >正文

北京兼职做网站建设中国建筑集团有限公司董事长

北京兼职做网站建设,中国建筑集团有限公司董事长,wordpress清除所有评论,0453牡丹江信息网官网基于Redis加锁注解AOP解决JOB重复执行问题 现象解决方案自定义注解定义AOP策略redis 加锁实践 现象 线上xxljob有时候会遇到同一个任务在调度的时候重复执行,如下图: 线上JOB服务运行了2个实例,有时候会重复调度到同一个实例,有…

基于Redis加锁+注解+AOP解决JOB重复执行问题

  • 现象
  • 解决方案
    • 自定义注解
    • 定义AOP策略
    • redis 加锁
    • 实践

现象

线上xxljob有时候会遇到同一个任务在调度的时候重复执行,如下图:
在这里插入图片描述

线上JOB服务运行了2个实例,有时候会重复调度到同一个实例,有时候会重复调度到不同实例上,对于Job重复执行会存在很多风险,可以采用Redis加锁的方式来解决。这里用统一的方式提供这个内部功能,其他Job或者从管理页面进来的请求直接执行Job可以都限制住,保证同一时间分布式环境中只有一个实例在运行。

解决方案

自定义注解

首先定义一个自定义注解,将redis加锁需要的参数可以通过注解声明:

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface JobNoRepete {
String name();
String redisKey();
long expireTime();
TimeUnit timeUnit();
}

定义AOP策略

@Component
@Aspect
@Slf4j
public class JobNoRepeteAop {
@Resourceprivate RedisService redisService;
@Around(value = "@annotation(annotation)", argNames = "pj,annotation")public Object around(ProceedingJoinPoint pj, JobRepetitionDefense annotation) throws Throwable {String name = annotation.name();String redisKey = annotation.redisKey();long expireTime = annotation.expireTime();TimeUnit timeUnit = annotation.timeUnit();log.info("job执行防重开始执行,name={},redisKey={},expireTime={},timeUnit={}",name, redisKey, expireTime, timeUnit);try {return redisService.executeOnlyOnce(redisKey, expireTime, timeUnit, pj::proceed);} finally {log.info("job执行防重执行完成,name={}", name);}}

}

redis 加锁

redis 加锁逻辑,使用spring redis中的StringRedisTemplate:

@Slf4j
@Component
public class RedisService {@Resourceprivate StringRedisTemplate stringRedisTemplate;public <T> T executeOnlyOnce(String redisKey, long expireTime, TimeUnit timeUnit, CustomCallable<T> callable) throws Throwable {if (StrUtil.isBlank(redisKey) || expireTime <= 0 || Objects.isNull(timeUnit) || Objects.isNull(callable)) {throw new IllegalArgumentException("参数错误");}String uuid = UUID.randomUUID().toString();if (!stringRedisTemplate.opsForValue().setIfAbsent(redisKey, uuid, expireTime, timeUnit)) {throw new RuntimeException("任务正在执行,请稍后再试");}//执行逻辑try {return callable.call();} finally {//执行完成主动释放锁try {String oldValue = stringRedisTemplate.opsForValue().get(redisKey);if (Objects.equals(uuid, oldValue)) {stringRedisTemplate.delete(redisKey);}} catch (Exception e) {//释放锁失败,等待expireTime后自动释放log.error("释放锁异常", e);}}}
}
public interface CustomCallable<V> {V call() throws Throwable;
}

实践

对于适用的场景就可以直接使用注解的方式进行声明,例如:

@Service
@Slf4j
public class testService {
private static final int EXPIRE_HOURS = 24;
@JobNoRepete(name = "测试redis", redisKey = Constant.JOB_LOCK_TO_REDIS,expireTime = EXPIRE_HOURS, timeUnit = TimeUnit.HOURS)public void test(LocalDate localDate) {//内部逻辑}
}
http://www.hyszgw.com/news/52050.html

相关文章:

  • 网站做微信链接怎么做苏州企业建站系统
  • dedecms医院网站wap模板(橙色)4512345页面seo优化
  • 北京 成品网站wordpress绑定域名
  • 网站改版的步骤莆田网站建设费用
  • dede wap网站模板下载番禺网站开发平台
  • wordpress 站点语言手机网站建设图
  • 智慧政务门户网站建设方案全国政务网站哪家做的好
  • 如何自己做网站推广wordpress添加keywords
  • 去哪儿网站上做民宿需要材料什么文案容易上热门
  • 网站网站做维护犯罪网站域名 文件夹
  • 长沙城乡建设部网站首页百度如何免费打广告
  • 网站挂马怎么处理网站镜像上传到域名空间
  • 肇庆制作网站软件国土网站建设自查报告
  • 资讯文章类网站织梦模板小米软件开发工程师待遇
  • 5118网站是免费的吗如何建wordpress
  • 做网站宣传有用吗破解WordPress站点
  • 万网网站空间服务范围企业网站建设定制
  • 南宁建设公司网站在线课程网站开发价格
  • 徐州网站设计价位asp.net做的网站要放到网上空间去要放哪些文件上去
  • 手机网站出现广告德成建设集团有限公司网站
  • 与铁路建设相关的网站内容营销理论
  • 长沙市师德师风建设网站我想做一个小网站搞页游该怎么做
  • 信息化网站建设有什么用智慧旅游网站建设方案ppt
  • 北京手机网站设计企业如何创建网站
  • 成都图纸设计公司seo优化策略主要包括哪些方面
  • 建设网站需要的资金清单视频拍摄案例
  • 泰安软件开发公司哪家好seo英文
  • 湖南省建设厅城乡建设网站wordpress 主题加速
  • 网站开发 指导网站设计服务要不要交文化事业建设费
  • 网站初期如何推广江苏镇江网站建设