forked from luyan/epmet-cloud-lingshan
				
			
				 14 changed files with 232 additions and 31 deletions
			
			
		| @ -0,0 +1,78 @@ | |||
| package com.epmet.commons.tools.distributedlock; | |||
| 
 | |||
| import com.epmet.commons.tools.exception.RenException; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import org.redisson.api.RLock; | |||
| import org.redisson.api.RedissonClient; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.stereotype.Component; | |||
| 
 | |||
| import java.util.concurrent.TimeUnit; | |||
| 
 | |||
| /** | |||
|  * @Author zxc | |||
|  * @DateTime 2020/10/28 9:05 上午 | |||
|  */ | |||
| @Component | |||
| public class DistributedLock { | |||
| 
 | |||
|     @Autowired | |||
|     private RedissonClient redissonClient; | |||
| 
 | |||
|     /** | |||
|      * @Description  抢锁🔒 每个锁持有十分钟 | |||
|      *  watchDog模式,每隔10s,自动更改持锁时间,假设宕机,持锁时间达到时,会自动释放锁 | |||
|      * @Param name | |||
|      * @author zxc | |||
|      * @date 2020/10/28 2:52 下午 | |||
|      */ | |||
|     public RLock getLock(String name){ | |||
|         RLock lock = null; | |||
|         if (StringUtils.isNotBlank(name)) { | |||
|             lock = redissonClient.getLock(name); | |||
|             // 持续时间为 -1 时,直到其明确释放锁才会释放【宕机就完蛋了】,
 | |||
|             lock.lock(10,TimeUnit.MINUTES); | |||
|         } | |||
|         return lock; | |||
|     } | |||
| 
 | |||
|     /** | |||
|      * @Description | |||
|      * @Param name 锁名 | |||
|      * @Param leaseTime 持锁时间 单位:min | |||
|      * @Param waitTime 获取锁最长等待时间 单位:min | |||
|      * @author zxc | |||
|      * @date 2020/10/29 9:04 上午 | |||
|      */ | |||
|     public RLock getLock(String name,Long leaseTime,Long waitTime){ | |||
|         RLock lock = null; | |||
|         if (StringUtils.isNotBlank(name) && leaseTime > 0 && waitTime > 0 ){ | |||
|             lock = redissonClient.getLock(name); | |||
|             Boolean lockStatus; | |||
|             try { | |||
|                 lockStatus = lock.tryLock(waitTime,leaseTime,TimeUnit.MINUTES); | |||
|                 if (!lockStatus){ | |||
|                     throw new RenException("获取锁🔒失败了......"); | |||
|                 } | |||
|             } catch (InterruptedException e) { | |||
|                 e.printStackTrace(); | |||
|             } | |||
|         } | |||
|         return lock; | |||
|     } | |||
| 
 | |||
|     /** | |||
|      * @Description  释放锁🔒 | |||
|      * @Param rLock | |||
|      * @author zxc | |||
|      * @date 2020/10/28 2:52 下午 | |||
|      */ | |||
|     public void unLock(RLock rLock){ | |||
|         if (null != rLock){ | |||
|             if (rLock.isHeldByCurrentThread()){ | |||
|                 rLock.unlock(); | |||
|             } | |||
|         } | |||
|     } | |||
| 
 | |||
| } | |||
| @ -0,0 +1,12 @@ | |||
| package com.epmet.commons.tools.distributedlock; | |||
| 
 | |||
| /** | |||
|  * @Author zxc | |||
|  * @DateTime 2020/10/28 9:23 上午 | |||
|  */ | |||
| public interface LockConstants { | |||
| 
 | |||
|     String TEST_LOCK_NAME = "testLock"; | |||
| 
 | |||
|     String STATS_LOCK_NAME = "stats"; | |||
| } | |||
| @ -0,0 +1,21 @@ | |||
| package com.epmet.stats.test.zxc; | |||
| 
 | |||
| import lombok.Data; | |||
| 
 | |||
| /** | |||
|  * @Author zxc | |||
|  * @DateTime 2020/10/28 3:15 下午 | |||
|  */ | |||
| @Data | |||
| public class ZxcDTO { | |||
| 
 | |||
|     private String a; | |||
|     private String b; | |||
|     private String c; | |||
|     private String d; | |||
|     private String e; | |||
|     private Integer f; | |||
|     private Integer g; | |||
|     private Integer h; | |||
| 
 | |||
| } | |||
					Loading…
					
					
				
		Reference in new issue