1.使用setNx做分布式锁:setnx只能设置一次值,第二次会设置失败
public Boolean setSecondTime(String key, String value, Long secondTime) {
try {
boolean flag= client.setNX(key, value);
client.expire(key, secondTime, TimeUnit.SECONDS);
} catch (Exception e) {
log.error("jimdbservice setSecondTime", e);
}
return flag;
}
try {
if (boardJimdbService.setSecondTime(Sale_Task_key, "111", 8L)) {
//TODO
}
} catch (Exception e) {
} finally {
boardJimdbService.del(Sale_Task_key);
}
来源:CSDN
作者:诸葛子房_
链接:https://blog.csdn.net/weixin_43291055/article/details/103709487