使用shedlock实现分布式互斥执行

前言

前序章节:springboot基础(82):分布式定时任务解决方案shedlock

如果你不清楚shedlock,建议先阅读前序章节,再来查看本文。

如果我们不在spring环境下,如何使用shedlock实现分布式互斥执行?

我们可以使用shedlock为我们提供的DefaultLockingTaskExecutor来实现手动调用。

示例

void executeWithLock(@NonNull Runnable var1, @NonNull LockConfiguration var2)

java 复制代码
  @GetMapping("/testRunnable")
    public R testRunnable(HttpServletRequest request) {
        log.info("进入方法");
        String name = request.getParameter("name");
        LockingTaskExecutor executor = new DefaultLockingTaskExecutor(lockProvider);
        Instant now = Instant.now();
        try {
            executor.executeWithLock(new Runnable() {
                @Override
                public void run() {
                    log.info("执行");
                    helloService.helloCn(name);
                }
            }, new LockConfiguration(now, "testRunnable", Duration.ofSeconds(30), Duration.ofSeconds(5)));
            log.info("end");
            return R.ok("success", null);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return R.fail("fail");
    }

LockingTaskExecutor.TaskResult executeWithLock(@NonNull LockingTaskExecutor.TaskWithResult task, @NonNull LockConfiguration lockConfig)

利用此API,我们可以让一个方法不能在一个时间只能有一次实例在执行,排斥调用者,且其它调用者的调用失败,这是与分布式锁不一样的地方。

java 复制代码
 @GetMapping("/testTaskWithResult")
    public R testTaskWithResult(HttpServletRequest request) {
        log.info("进入方法");
        String name = request.getParameter("name");
        LockingTaskExecutor executor = new DefaultLockingTaskExecutor(lockProvider);
        Instant now = Instant.now();
        try {
            LockingTaskExecutor.TaskResult taskResult = executor.executeWithLock(new LockingTaskExecutor.TaskWithResult() {
                @Override
                public Object call() throws Throwable {
                    log.info("执行");
                    return helloService.helloCn(name);
                }
            }, new LockConfiguration(now, "testTaskWithResult", Duration.ofSeconds(30), Duration.ofSeconds(5)));
            boolean flag = taskResult.wasExecuted();
            log.info("end");
            return R.ok("任务是否被执行:" + flag, taskResult.getResult());
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return R.fail("fail");
    }

传送门

https://github.com/lukas-krecan/ShedLock

相关推荐
SMF19191 小时前
【分布式文件存储系统Minio】使用Docker快速搭建 MinIO文件存储服务以及JAVA
分布式
2401_848009723 小时前
rabbitmq的高级知识-ttl与死信队列
分布式·rabbitmq·java-rabbitmq
wangjinxun3 小时前
LLM Xinference 安装使用(支持CPU、Metal、CUDA推理和分布式部署)
分布式
你这个代码我看不懂6 小时前
POD重启问题排查
分布式
无心水8 小时前
【任务调度:框架】10、2026最新!分布式任务调度选型决策树:再也不纠结选哪个
人工智能·分布式·算法·决策树·机器学习·架构·2025博客之星
上海锟联科技9 小时前
什么是DAS分布式光纤声波传感系统?原理与应用解析
数据结构·分布式·算法·分布式光纤传感
茶本无香9 小时前
【无标题】Kafka 系列博文(一):从零认识 Kafka,到底解决了什么问题?
java·分布式·kafka
czlczl200209259 小时前
插入时先写DB后写Redis?分布式中传统双写模式的缺陷
数据库·redis·分布式
斯普信专业组10 小时前
Kafka集群数据迁移方案:基于MirrorMaker2的集群迁移实施步骤
分布式·kafka·linq
yatum_201411 小时前
Hadoop 三种核心运行模式(伪分布式/分布式/混合模式)全总结
hadoop·分布式·wpf