2023年11月27日发(作者:)
RedisTemplate执⾏lua脚本抛出异常IllegalStateException
基于Redis的分布式锁的释放过程,为了防⽌释放错误,需要使⽤lua脚本实现原⼦释放,但是RedisTemplate在执⾏lua
脚本时会抛出异常IllegalStateException
问题描述
分布式锁的释放
/**
*
* 释放锁lua脚本
*/
private static final String RELEASE_LOCK_LUA_SCRIPT = "if ('get', KEYS[1]) == ARGV[1] then return ('del', KEYS[1]) else return 0 end";
/**
* 释放锁成功返回值
*/
lStateException: Failed to execute ApplicationRunner
at nner(:778) [:E]
因为没有指定ReturnType,所以默认使⽤,返回值就是,这个类并没有重
Output
写中的⽅法。所以抛出异常
CommandOutputIllegalStateException
CommandOutput中的⽅法
set(long integer)
/**
* Set the command output to a 64-bit signed integer. Concrete {@link CommandOutput} implementations must override this
* method unless they only receive a byte array value.
*
* @param integer The command output.
*/
public void set(long integer) {
throw new IllegalStateException();
}
解决⽅法
/**
*
* 释放锁lua脚本
*/
private static final String RELEASE_LOCK_LUA_SCRIPT = "if ('get', KEYS[1]) == ARGV[1] then return ('del', KEYS[1]) else return 0 end";
/**
* 释放锁成功返回值
*/
private static final Long RELEASE_LOCK_SUCCESS_RESULT = 1L;
/**
* 释放锁
*
* @param key 锁ID
* @param clientId 客户端ID
参考
RedisTemplate 执⾏lua脚本出错


发布评论