您可以使用redisson来执行分布式定时任务吗,如何确保不重复执行任务

时间:2020-08-14 02:43:23

标签: redisson

在部署多个节点时,现在使用分布式锁来防止重复执行任务。 Redisson是否有其他方法可以解决此问题?

  public static void main(String[] args) throws InterruptedException {
        Config config = new Config();
        config. useSingleServer().setAddress("redis://127.0.0.1:6379");
        
        RedissonClient redisson = Redisson.create(config);

        RedissonNodeConfig nodeConfig = new RedissonNodeConfig(config);
        nodeConfig.setExecutorServiceWorkers(Collections.singletonMap("myExecutor24", 1));
        RedissonNode node = RedissonNode.create(nodeConfig);
        node.start();
        RLock lock = redisson.getLock("/myExecutor24/lock");
        boolean getLock = false;
        if(getLock = lock.tryLock(0,50, TimeUnit.SECONDS)) {
            System.out.println("Obtain a distributed lock..............");
            RScheduledExecutorService e = redisson.getExecutorService("myExecutor24");
            WorkerOptions options = WorkerOptions.defaults().workers(2);
            e.registerWorkers(options);

            boolean shutdown = e.isShutdown();
            System.out.println(shutdown);
            e.schedule(new RedisRunning(), CronSchedule.of("0/6 * * * * ?"));
            Thread.sleep(100000);
        e.shutdown();
        node.shutdown();
        }

    }

0 个答案:

没有答案