release branch 1.1.0
This commit is contained in:
@@ -18,13 +18,6 @@ import java.lang.annotation.*;
|
||||
@EnableAspectJAutoProxy
|
||||
public @interface EnableLock {
|
||||
|
||||
/**
|
||||
* 加锁过期时间
|
||||
* @return 时间
|
||||
* @since 1.1.0
|
||||
*/
|
||||
int lockExpireMills() default 60 * 1000;
|
||||
|
||||
/**
|
||||
* 唯一标识生成策略
|
||||
* @return 结果
|
||||
@@ -42,5 +35,18 @@ public @interface EnableLock {
|
||||
*/
|
||||
String cache() default "springRedisService";
|
||||
|
||||
/**
|
||||
* 加锁 key 格式化策略
|
||||
* @return 策略
|
||||
* @since 1.2.0
|
||||
*/
|
||||
String lockKeyFormat() default "lockKeyFormat";
|
||||
|
||||
/**
|
||||
* 锁释放失败处理类
|
||||
* @return 结果
|
||||
* @since 1.2.0
|
||||
*/
|
||||
String lockReleaseFailHandler() default "lockReleaseFailHandler";
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.github.houbb.lock.spring.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 分布式加锁注解
|
||||
@@ -20,9 +21,24 @@ public @interface Lock {
|
||||
String value() default "";
|
||||
|
||||
/**
|
||||
* 尝试获取锁等待时间
|
||||
* @return 结果
|
||||
* 时间单位
|
||||
* @return 单位
|
||||
* @since 1.2.0
|
||||
*/
|
||||
long tryLockMills() default 10 * 1000;
|
||||
TimeUnit timeUnit() default TimeUnit.SECONDS;
|
||||
|
||||
/**
|
||||
* 等待锁时间
|
||||
* @return 等待锁时间
|
||||
* @since 1.2.0
|
||||
*/
|
||||
long waitLockTime() default 10;
|
||||
|
||||
/**
|
||||
* 业务加锁时间
|
||||
* @return 加锁时间
|
||||
* @since 1.2.0
|
||||
*/
|
||||
long lockTime() default 60;
|
||||
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.github.houbb.heaven.util.lang.StringUtil;
|
||||
import com.github.houbb.heaven.util.util.ArrayUtil;
|
||||
import com.github.houbb.lock.api.exception.LockException;
|
||||
import com.github.houbb.lock.core.bs.LockBs;
|
||||
import com.github.houbb.lock.core.support.handler.LockReleaseFailHandlerContext;
|
||||
import com.github.houbb.lock.spring.annotation.Lock;
|
||||
import com.github.houbb.log.integration.core.Log;
|
||||
import com.github.houbb.log.integration.core.LogFactory;
|
||||
@@ -24,9 +25,7 @@ import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Parameter;
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author binbin.hou
|
||||
@@ -61,16 +60,14 @@ public class LockAspect {
|
||||
|
||||
boolean isLockAnnotation = method.isAnnotationPresent(Lock.class);
|
||||
if(isLockAnnotation) {
|
||||
Lock lock = method.getAnnotation(Lock.class);
|
||||
Lock lockAnnotation = method.getAnnotation(Lock.class);
|
||||
|
||||
// 如果构建 key?
|
||||
String lockKey = buildLockKey(lock, point);
|
||||
String lockKey = buildLockKey(lockAnnotation, point);
|
||||
|
||||
boolean tryLockFlag = false;
|
||||
try {
|
||||
long tryLockMills = lock.tryLockMills();
|
||||
|
||||
tryLockFlag = lockBs.tryLock(tryLockMills, TimeUnit.MILLISECONDS, lockKey);
|
||||
tryLockFlag = lockBs.tryLock(lockKey, lockAnnotation.timeUnit(), lockAnnotation.lockTime(), lockAnnotation.waitLockTime());
|
||||
if(!tryLockFlag) {
|
||||
log.warn("[LOCK] TRY LOCK FAILED {}", lockKey);
|
||||
throw new LockException("[LOCK] TRY LOCK FAILED " + lockKey);
|
||||
@@ -84,7 +81,9 @@ public class LockAspect {
|
||||
// 只有获取锁的情况下,才尝试释放锁
|
||||
if(tryLockFlag) {
|
||||
boolean unLockFlag = lockBs.unlock(lockKey);
|
||||
// 异常处理等
|
||||
|
||||
// 释放锁结果
|
||||
this.lockReleaseFailHandle(unLockFlag, lockKey);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -92,6 +91,24 @@ public class LockAspect {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 锁释放失败
|
||||
* @param unLockFlag 释放结果
|
||||
* @param lockKey 加锁信息
|
||||
*/
|
||||
private void lockReleaseFailHandle(boolean unLockFlag,
|
||||
String lockKey) {
|
||||
if(unLockFlag) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 触发通知,便于用户自定义处理。
|
||||
// 比如报警
|
||||
LockReleaseFailHandlerContext handlerContext = LockReleaseFailHandlerContext.newInstance()
|
||||
.key(lockKey);
|
||||
this.lockBs.lockReleaseFailHandler().handle(handlerContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建加锁的 key
|
||||
*
|
||||
|
||||
@@ -4,6 +4,8 @@ import com.github.houbb.common.cache.api.service.ICommonCacheService;
|
||||
import com.github.houbb.heaven.util.lang.StringUtil;
|
||||
import com.github.houbb.id.api.Id;
|
||||
import com.github.houbb.id.core.core.Ids;
|
||||
import com.github.houbb.lock.api.core.ILockKeyFormat;
|
||||
import com.github.houbb.lock.api.core.ILockReleaseFailHandler;
|
||||
import com.github.houbb.lock.core.bs.LockBs;
|
||||
import com.github.houbb.lock.spring.annotation.EnableLock;
|
||||
import com.github.houbb.redis.config.core.factory.JedisRedisServiceFactory;
|
||||
@@ -28,15 +30,16 @@ public class LockAopConfig implements ImportAware, BeanFactoryPostProcessor {
|
||||
|
||||
@Bean("lockBs")
|
||||
public LockBs lockBs() {
|
||||
int lockExpireMills = (int) enableLockAttributes.get("lockExpireMills");
|
||||
ICommonCacheService commonCacheService = beanFactory.getBean(enableLockAttributes.getString("cache"), ICommonCacheService.class);
|
||||
Id id = beanFactory.getBean(enableLockAttributes.getString("id"), Id.class);
|
||||
ILockKeyFormat lockKeyFormat = beanFactory.getBean(enableLockAttributes.getString("lockKeyFormat"), ILockKeyFormat.class);
|
||||
ILockReleaseFailHandler lockReleaseFailHandler = beanFactory.getBean(enableLockAttributes.getString("lockReleaseFailHandler"), ILockReleaseFailHandler.class);
|
||||
|
||||
return LockBs.newInstance()
|
||||
.cache(commonCacheService)
|
||||
.id(id)
|
||||
.lockExpireMills(lockExpireMills)
|
||||
.init();
|
||||
.lockKeyFormat(lockKeyFormat)
|
||||
.lockReleaseFailHandler(lockReleaseFailHandler);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,6 +2,10 @@ package com.github.houbb.lock.spring.config;
|
||||
|
||||
import com.github.houbb.id.api.Id;
|
||||
import com.github.houbb.id.core.core.Ids;
|
||||
import com.github.houbb.lock.api.core.ILockKeyFormat;
|
||||
import com.github.houbb.lock.api.core.ILockReleaseFailHandler;
|
||||
import com.github.houbb.lock.core.support.format.LockKeyFormat;
|
||||
import com.github.houbb.lock.core.support.handler.LockReleaseFailHandler;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -21,4 +25,14 @@ public class LockBeanConfig {
|
||||
return Ids.uuid32();
|
||||
}
|
||||
|
||||
@Bean("lockKeyFormat")
|
||||
public ILockKeyFormat lockKeyFormat() {
|
||||
return new LockKeyFormat();
|
||||
}
|
||||
|
||||
@Bean("lockReleaseFailHandler")
|
||||
public ILockReleaseFailHandler lockReleaseFailHandler() {
|
||||
return new LockReleaseFailHandler();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user