当前位置:网站首页>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;
}
}
边栏推荐
- Pytorch for former Torch users - Tensors
- Talking about the difference between kotlin collaboration and thread
- AutoUpdater. Net client custom update file
- QT downloading files through URL
- Opencv learning notes -day 12 (ROI region extraction and inrange() function operation)
- 8.8 heap insertion and deletion
- [paid promotion] collection of frequently asked questions, FAQ of recommended list
- Challenge transform() 2D
- Esp32 (4): overview of the overall code architecture
- Use Huawei performance management service to configure the sampling rate on demand
猜你喜欢

So the toolbar can still be used like this? The toolbar uses the most complete parsing. Netizen: finally, you don't have to always customize the title bar!

Rew acoustic test (I): microphone calibration

Opencv learning notes -day 11 (split() channel separation function and merge() channel merge function)

Wechat development tool (applet)

Agp7.0|kts makes a reinforced plug-in

Evaluation standard for audio signal quality of intelligent speakers

I'm late for school

Interpretation of orientedrcnn papers

桂林 穩健醫療收購桂林乳膠100%股權 填補乳膠產品線空白

Express get request
随机推荐
How to format an UTC date to use the Z (Zulu) zone designator in php?
Invalid update: invalid number of sections. The number of sections contained in the table view after
Deep understanding of kotlin collaboration context coroutinecontext
Opencv learning notes-day5 (arithmetic operation of image pixels, add() addition function, subtract() subtraction function, divide() division function, multiply() multiplication function
Esp32 (6): Bluetooth and WiFi functions for function development
Opencv learning notes -day 12 (ROI region extraction and inrange() function operation)
Opencv learning notes -day8 (keyboard typing (waitkey()); Wait for typing) action: triggers some action when the appropriate character is typed using the keyboard)
Design specification for smart speakers v1.0
Net framework system requirements
Anchorgenerator for mmdet line by line interpretation
About Lombok's @data annotation
Opencv learning notes -day1 (image reading display imread, imshow, namedwindow)
[shutter] solve failed assertion: line 5142 POS 12: '_ debugLocked‘: is not true.
Explanation on the use of password profiteering cracking tool Hydra
QT downloading files through URL
Couldn't load this key (openssh ssh-2 private key (old PEM format))
Rew acoustic test (V): equipment required for test
Interviewer: do you understand the principle of recyclerview layout animation?
Research on lg1403 divisor
Rew acoustic test (VI): signal and measurement