使用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

相关推荐
csdn2015_2 小时前
kafka如何保证消息的顺序
分布式·kafka
csdn2015_5 小时前
kafka如何确保消息不丢失
数据库·分布式·kafka
952365 小时前
RabbitMQ - 高级特性
java·spring boot·分布式·后端·rabbitmq
Database_Cool_6 小时前
阿里云 PolarDB-X vs TiDB 分布式数据库选型指南
数据库·分布式·阿里云·tidb
数据库小学妹6 小时前
分布式数据库架构怎么选?三种路线对比+迁移避坑指南
数据库·分布式·分布式数据库·数据库架构·数据迁移·数据库选型
大师兄66686 小时前
HarmonyOS7 分布式流转:手机到平板无缝接续案例
分布式·跨设备·harmonyos7·分布式流转
一只积极向上的小咸鱼6 小时前
多机Docker + InfiniBand 分布式运行实操手册
分布式·docker·容器
AndrewHZ7 小时前
【LLM技术全景】混合精度与分布式训练:训练大模型的工程奥秘
人工智能·分布式·深度学习·算法·ai·语言模型·llm
ffqws_16 小时前
Redis 分布式锁:基于 Redisson 的实现与面试高频问答
redis·分布式·面试
六bring个六21 小时前
open Harmony中分布式软总线的学习任务清单
分布式·学习·c/c++·open harmony