javaspring-bootspring-amqpredisson

Can not unlock redisson lock in rabbit listener in spring boot


I have the following code block in a rbbit listener in spring boot. I try to get a lock using redisson:

@RabbitListener(queues = Const.Rabbit.QUEUE_NAME, concurrency = "1")
    public void consumer(byte[] payload) throws IOException {
        String lockName = "lock-name";
        RLock lock = redissonClient.getLock(lockName);
        try {
            // call a method
        } catch (Exception e) {
            log.error("error: {}", e.getMessage());
        } finally {
            lock.unlock();
        }
    }

Then I get the following error. It can not release the lock because it is not locked by the current thread. Next, because rabbit message get not acknoledged. rabbit redelivers it infinitely.

org.springframework.amqp.rabbit.support.ListenerExecutionFailedException: Listener method 'public void consumer(byte[]) throws java.io.IOException' threw exception
.
.
.
Caused by: java.lang.IllegalMonitorStateException: attempt to unlock lock, not locked by current thread by node id: e8289934-2820-42f2-aa3d-30c3f56ce1ac thread-id: 100

Solution

  • According to the code snippet you share there is has to be this code yet before try:

    lock.lock();
    

    See more info in Redisson docs: https://github.com/redisson/redisson/wiki/8.-Distributed-locks-and-synchronizers