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

前言

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

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

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

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

在这里插入图片描述

示例

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

  @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,我们可以让一个方法不能在一个时间只能有一次实例在执行,排斥调用者,且其它调用者的调用失败,这是与分布式锁不一样的地方。

 @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

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-18 00:36:01       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-18 00:36:01       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-18 00:36:01       57 阅读
  4. Python语言-面向对象

    2024-07-18 00:36:01       68 阅读

热门阅读

  1. HTSJDK库Cigar类介绍

    2024-07-18 00:36:01       22 阅读
  2. Html_Css问答集(9)

    2024-07-18 00:36:01       17 阅读
  3. 2024.7.17

    2024-07-18 00:36:01       26 阅读
  4. Web前端-Web开发CSS基础4-显示

    2024-07-18 00:36:01       17 阅读
  5. xml 标记语言介绍

    2024-07-18 00:36:01       22 阅读
  6. C# lock关键字

    2024-07-18 00:36:01       18 阅读
  7. C# —— 泛型

    2024-07-18 00:36:01       22 阅读
  8. 利用Postman进行自动化测试:从基础到进阶

    2024-07-18 00:36:01       20 阅读
  9. 河南萌新联赛2024第(一)场:河南农业大学

    2024-07-18 00:36:01       20 阅读
  10. ZC2205-24V500mAUltralow-Quiescent-Current LDO

    2024-07-18 00:36:01       16 阅读
  11. golang项目中gorm框架的配置和具体使用

    2024-07-18 00:36:01       20 阅读