Redisson watchdog mechanism

public Object test(a) {
        RLock lock = redisson.getLock("my-lock");
        boolean b = false;
        try {
// b = lock.tryLock(20, 10, TimeUnit.SECONDS); / / 1.
            b = lock.tryLock(40, TimeUnit.SECONDS); / / 2.
            if (b) {
                System.out.println("Thread:" + Thread.currentThread().getName() + "Locked");
                System.err.println(Thread.currentThread().getName() + "Task start execution......");
                Thread.sleep(100000);
                System.err.println(Thread.currentThread().getName() + "Mission completed.");
            } else {
                System.out.println("Thread:" + Thread.currentThread().getName() + "Unlocked."); }}catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            System.out.println("Thread:" + Thread.currentThread().getName() + "Release lock......");
            if (b) {
                lock.unlock();
                System.out.println("Thread:" + Thread.currentThread().getName() + "Lock release complete."); }}return b;
    }
Copy the code
  • When internalLockLeaseTime (leaseTime) is not set, the default value is 30 seconds, and the watchdog function is enabled, and the lock is released every 10 seconds until the thread runs out of position. If a task is not completed within the default 30 seconds, the watchdog mechanism will renew the time until the task is complete and reset the time to 30 seconds every 10 seconds.

  • In case 1, the specified lock timeout time is 10 seconds, the task is not completed, the lock will be released, the watchdog will not start.

  • If leaseTime is not set to -1, the key will be automatically deleted. If leaseTime is not set to -1, the key will be automatically deleted. Will automatically get the inside of the configuration parameter Config. LockWatchdogTimeout value, default is 30 seconds.

The above is a summary of my own understanding based on my experiments and the introduction of official documents.

reference

Redisson official documentation