当前位置:网站首页>Simple redis lock
Simple redis lock
2022-06-30 09:27:00 【Xiao Lu, a migrant worker in Beijing】
@Component
public class DistributedLock {
private static final Logger LOGGER = LoggerFactory.getLogger(DistributedLock.class);
@Resource(name = "cscRedisClient")
private RedisStoreClient redisStoreClient;
private static final String LOCK_CATEGORY = "oms_distribut_lock";
private static final String APP_KEY = "csc-oms-task-process-service";
/**
* Lock
* @param locaName The lock key
* @param acquireTimeout Get timeout
* @param timeout The timeout of the lock
* @return Lock logo
*/
public String lockWithTimeout(String locaName,
long acquireTimeout, int timeout) {
String retIdentifier = null;
try {
// Randomly generate one value
String identifier = UUID.randomUUID().toString();
// Lock name , namely key value
String lockKey = "lock:" + locaName;
// Timeout time , After locking, the lock will be released automatically
// int lockExpire = (int)(timeout / 1000);
// The timeout for obtaining the lock , After this time, the lock is abandoned
long end = System.currentTimeMillis() + acquireTimeout * 1000;
final StoreKey storeKey = new StoreKey(LOCK_CATEGORY, APP_KEY, lockKey);
while (System.currentTimeMillis() < end) {
if (redisStoreClient.setnx(storeKey, identifier)) {
redisStoreClient.expire(storeKey, timeout);
// return value value , Used to release lock time confirmation
retIdentifier = identifier;
return retIdentifier;
}
// return -1 representative key No timeout set , by key Set a timeout
if (redisStoreClient.ttl(storeKey) == -1) {
redisStoreClient.expire(storeKey, timeout);
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
} catch (Exception e) {
LOGGER.error(" Lock acquisition failed " + locaName, e);
Cat.logError(" Lock acquisition failed " + locaName, e);
}
return retIdentifier;
}
/**
* Release the lock
* @param lockName The lock key
* @param identifier Release the lock logo
* @return
*/
public boolean releaseLock(String lockName, long acquireTimeout, String identifier) {
String lockKey = "lock:" + lockName;
boolean retFlag = false;
try {
final StoreKey storeKey = new StoreKey(LOCK_CATEGORY, APP_KEY, lockKey);
long end = System.currentTimeMillis() + acquireTimeout * 1000;
while (System.currentTimeMillis() < end) {
if (redisStoreClient.compareAndDelete(storeKey, identifier)) {
LOGGER.info("delelte lock");
break;
}
}
} catch (Exception e) {
LOGGER.warn("releaseLock,{}",e);
}
return retFlag;
}
}
边栏推荐
- Why must redis exist in distributed systems?
- ES6 learning road 5 symbol
- Row column (vertical and horizontal table) conversion of SQL
- 桂林 稳健医疗收购桂林乳胶100%股权 填补乳胶产品线空白
- Microsoft. Bcl. Async usage summary -- in Net framework 4.5 project Net framework version 4.5 and above can use async/await asynchronous feature in C 5
- Small program learning path 1 - getting to know small programs
- Rew acoustic test (II): offline test
- Anchorgenerator for mmdet line by line interpretation
- Rew acoustic test (III): generate test signal
- Rew acoustic test (V): equipment required for test
猜你喜欢

Harmonyos actual combat - ten thousand words long article understanding service card development process

Microsoft. Bcl. Async usage summary -- in Net framework 4.5 project Net framework version 4.5 and above can use async/await asynchronous feature in C 5

100 lines of code and a voice conversation assistant

Resnet50+fpn for mmdet line by line code interpretation

Anchorgenerator for mmdet line by line interpretation

Rew acoustic test (II): offline test

4. use ibinder interface flexibly for short-range communication

Flutter 0001, environment configuration

Mmdet line by line deltaxywhbboxcoder

Opencv learning notes -day 11 (split() channel separation function and merge() channel merge function)
随机推荐
Detailed explanation of pipline of mmdetection
100 lines of code and a voice conversation assistant
Esp32 things (3): overview of the overall system design
float
Metasploit practice - SSH brute force cracking process
Six implementation methods of singleton mode
Unsupportedclassversionerror is reported when starting jar package. How to repair it
Opencv learning notes -day3 (mat object and creation related operations mat:: clone(), mat:: copyto(), mat:: zeros(), mat:: ones(), scalar()...)
Design specification for smart speakers v1.0
Talk about how the kotlin collaboration process establishes structured concurrency
Explanation on the use of password profiteering cracking tool Hydra
Application of hongruan face recognition
Talking about the difference between kotlin collaboration and thread
AutoUpdater. Net client custom update file
Cronexpression expression explanation and cases
Express の post request
桂林 穩健醫療收購桂林乳膠100%股權 填補乳膠產品線空白
Mysq database remote connection error, remote connection is not allowed
Invalid update: invalid number of sections. The number of sections contained in the table view after
Abstract factory pattern