网站开发 net,湛江网页设计开发,wordpress完美商城,成都网页设计培训班一#xff1a;业务锁
在代码业务逻辑加锁#xff0c;防止不同业务操作相同业务表导致数据错乱#xff0c;设置锁进行等待。这里锁使用的是ReentrantLock。详细的介绍可以参考#xff1a; https://blog.csdn.net/jerry11112/article/details/112375167
Slf4j
public class…一业务锁
在代码业务逻辑加锁防止不同业务操作相同业务表导致数据错乱设置锁进行等待。这里锁使用的是ReentrantLock。详细的介绍可以参考 https://blog.csdn.net/jerry11112/article/details/112375167
Slf4j
public class MyLock {private static ConcurrentHashMapLong, ReentrantLock reenTranLock new ConcurrentHashMap();/*** 业务加锁* param businessId 业务id* param lockCode 需要加锁的代码执行完后自动解锁* param timeOutSeconds 锁超时时间* return**/public static T T lockAndUnLock(Long businessId, SupplierT lockCode, long timeOutSeconds) {try {//可重入公平锁true-公平锁false-非公平锁ReentrantLock lock reenTranLock.putIfAbsent(businessId, new ReentrantLock(true));lock lock ! null ? lock : reenTranLock.get(businessId);//超时加锁单位秒boolean lockSuccess lock.tryLock(timeOutSeconds, TimeUnit.SECONDS);if (lockSuccess) {try {return lockCode.get();} catch (Exception ex) {throw ex;} finally {//解锁lock.unlock();}} else {throw new Exception(操作失败);}} catch (Exception ex) {if (ex instanceof IllegalStateException) {throw new IllegalStateException(ex.getMessage());}throw new RuntimeException(ex);}}/*** 加锁和解锁默认60S超时* param businessId 业务id* param lockCode 需要加锁的代码执行完后自动解锁* return**/public static T T lockAndUnLock(Long businessId, SupplierT lockCode) {return lockAndUnLock(businessId, lockCode, 60);}public static void main(String[] args) {Boolean a lockAndUnLock(1L, () - {String aaa 111111;return true;});System.out.println(a);}
}二定时任务锁
当多个负载服务同时启用相同程序定时任务需加上分布式锁这里使用的是redis加锁。
Slf4j
public class TaskTest {Autowiredprivate RedisTemplate redisTemplate;Scheduled(cron 0 0/10 * * * ?)public void task() {//redis key名称String redisKey test.concat(:).concat(this.getClass().getSimpleName()).concat(task);//加上redis锁适应分布式场景boolean setOk redisTemplate.opsForValue().setIfAbsent(redisKey, redisKey, 10, TimeUnit.SECONDS); //单位秒根据不同的业务处理自行设置大小时间if (!setOk) {log.info(redisKey 任务已执行);return;}try {//业务逻辑处理.....} catch (Exception ex) {log.error(定时任务异常, ex.getMessage());} finally {//手动解锁redisTemplate.delete(redisKey);}}
}