update - 测试分布式锁
This commit is contained in:
parent
67865c8be0
commit
8326f878e5
@ -17,6 +17,7 @@ import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.data.redis.core.types.Expiration;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
@ -107,15 +108,18 @@ public class CustomLockProcessInstanceInterceptor extends AbstractCommandInterce
|
||||
RedisConnectionFactory factory = redisTemplate.getConnectionFactory();
|
||||
RedisConnection conn = factory.getConnection();
|
||||
try {
|
||||
Boolean result = conn.set(name.getBytes(Charset.forName("UTF-8")), //设置name为key
|
||||
token.getBytes(Charset.forName("UTF-8")), //设置token为value
|
||||
Boolean result = conn.set(name.getBytes(StandardCharsets.UTF_8), //设置name为key
|
||||
token.getBytes(StandardCharsets.UTF_8), //设置token为value
|
||||
// Expiration.from(expire, TimeUnit.MILLISECONDS), //设置过期时间:MILLISECONDS毫秒
|
||||
Expiration.persistent(),
|
||||
RedisStringCommands.SetOption.SET_IF_ABSENT); //如果name不存在创建
|
||||
if (result != null && result)
|
||||
log.info("tryGetLock result: {}", result);
|
||||
if (Objects.isNull(result)) {
|
||||
throw new IllegalStateException("tryGetLock processInstance fail");
|
||||
}
|
||||
if (result)
|
||||
return token;
|
||||
} catch (Exception e) {
|
||||
unlock(name, token);
|
||||
log.warn("tryGetLock error: {}", e.getMessage(), e);
|
||||
return null;
|
||||
} finally {
|
||||
@ -142,11 +146,10 @@ public class CustomLockProcessInstanceInterceptor extends AbstractCommandInterce
|
||||
try {
|
||||
Long result = (Long) conn.scriptingCommands().eval(unlockScript.getBytes(Charset.forName("UTF-8")),
|
||||
ReturnType.INTEGER, 1, keysAndArgs);
|
||||
if (result != null && result > 0) {
|
||||
return true;
|
||||
} else {
|
||||
throw new IllegalStateException("unLock ProcessInstance fail");
|
||||
if (Objects.isNull(result)) {
|
||||
throw new IllegalStateException("unLock processInstance fail");
|
||||
}
|
||||
return result > 0;
|
||||
} catch (Exception e) {
|
||||
log.warn("unlock error: {}", e.getMessage(), e);
|
||||
try {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user