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

相关推荐
_waylau15 小时前
鸿蒙架构师修炼之道-面向对象的分布式架构
分布式·华为·架构·架构师·harmonyos·鸿蒙
Francek Chen17 小时前
【大数据存储与管理】NoSQL数据库:03 NoSQL与关系数据库的比较
大数据·数据库·分布式·nosql
FeBaby19 小时前
Java 高并发场景下 Redis 分布式锁(UUID+Lua)最佳实践
java·redis·分布式
richard_yuu21 小时前
工控场景落地|分布式协调与动态重配置管理,如何实现产线不停机升级?
分布式
MoFe11 天前
【.net core】【RabbitMq】rabbitmq在.net core中的简单使用
分布式·rabbitmq·.netcore
何中应1 天前
在windows本地部署RabbitMQ
分布式·消息队列·rabbitmq
Wild API1 天前
按任务轻重做模型分流的实战思路
分布式·微服务·架构
低客的黑调1 天前
RabbitMQ-从入门到生产落地
分布式·rabbitmq
宸津-代码粉碎机1 天前
Spring Boot 4.0虚拟线程实战续更预告:高阶技巧、监控排查与分布式场景落地指南
java·大数据·spring boot·分布式·后端·python
霖霖总总1 天前
[Redis小技巧32]Redis分布式锁的至暗时刻:从原理演进到时钟跳跃的终极博弈
数据库·redis·分布式