提交 62fa3795 编写于 作者: ibiz4j's avatar ibiz4j

lock

上级 958624c0
......@@ -121,6 +121,8 @@ public class webSecurityConfig extends WebSecurityConfigurerAdapter {
.antMatchers("/"+uploadpath).permitAll()
.antMatchers("/"+previewpath+"/**").permitAll()
.antMatchers("/ibizutil/**").permitAll()
.antMatchers("/dictionarys/**").permitAll()
.antMatchers("/lite/**").permitAll()
.antMatchers("/dst/**").permitAll();
......
......@@ -119,6 +119,7 @@ public class DevBootSecurityConfig extends WebSecurityConfigurerAdapter {
.antMatchers("/dst/**").permitAll()
.antMatchers("/lite/**").permitAll()
.antMatchers("/ibizutil/**").permitAll()
.antMatchers("/dictionarys/**").permitAll()
.antMatchers("/"+previewpath+"/**").permitAll();
......
......@@ -124,6 +124,7 @@ public class apiSecurityConfig extends WebSecurityConfigurerAdapter {
.antMatchers("/"+previewpath+"/**").permitAll()
.antMatchers("/dst/**").permitAll()
.antMatchers("/ibizutil/**").permitAll()
.antMatchers("/dictionarys/**").permitAll()
.antMatchers("/lite/**").permitAll();
for (String excludePattern : excludesPattern) {
......
......@@ -18,6 +18,7 @@ import cn.ibizlab.core.lite.service.IMetaEntityService;
import cn.ibizlab.core.lite.service.IMetaFieldService;
import cn.ibizlab.core.rule.domain.RuleItem;
import cn.ibizlab.core.rule.service.IRuleItemService;
import cn.ibizlab.util.annotation.RunLock;
import cn.ibizlab.util.dict.CodeItem;
import cn.ibizlab.util.dict.CodeList;
import cn.ibizlab.util.dict.Option;
......@@ -29,7 +30,6 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
......@@ -59,6 +59,22 @@ public class DstCoreResource
@Autowired
private IRuleItemService ruleItemService;
@RequestMapping(method = RequestMethod.GET, value = {
"/dst/lock/{rulecode}/{key}" })
public ResponseEntity<Boolean> testLock(@PathVariable("rulecode") String rulecode,@PathVariable("key") String key)
{
System.out.println("start");
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("end");
return ResponseEntity.status(HttpStatus.OK).body(true);
}
@RequestMapping(method = RequestMethod.GET, value = {
"/dst/test/{rulecode}/{key}" })
public ResponseEntity<Boolean> testRule(@PathVariable("rulecode") String rulecode,@PathVariable("key") String key)
......
package cn.ibizlab.util.annotation;
import org.springframework.core.annotation.AliasFor;
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD})
public @interface RunLock
{
String value() default "";
long lockTimeMillis() default 300000;
}
package cn.ibizlab.util.aspect;
import cn.ibizlab.util.annotation.RunLock;
import cn.ibizlab.util.annotation.VersionCheck;
import cn.ibizlab.util.errors.BadRequestAlertException;
import cn.ibizlab.util.service.LockService;
import lombok.SneakyThrows;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.core.annotation.Order;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
@Component
@Order(5000)
@Aspect
public class LockAspect {
@Autowired
@Lazy
private LockService lockService;
@SneakyThrows
@Around("@annotation(runLock)")
public Object checkLock(ProceedingJoinPoint point, RunLock runLock) {
String targetName = point.getTarget().getClass().getName();
String simpleName = point.getTarget().getClass().getSimpleName();
String methodName = point.getSignature().getName();
Object[] arguments = point.getArgs();
Class targetClass = Class.forName(targetName);
Method[] methods = targetClass.getMethods();
String key = "";
long lockTimeMillis=0L;
String[] paramNames = {};
for(Method method:methods){
if(method.getName().equals(methodName)){
key = runLock.value();
lockTimeMillis=runLock.lockTimeMillis();
paramNames = getParamterNames(method);
}
}
ExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression(key);
EvaluationContext context = new StandardEvaluationContext();
for(int i=0;i<arguments.length;i++){
context.setVariable(paramNames[i],arguments[i]);
}
String lockName=(expression.getValue(context,String.class));
boolean locked=lockService.isLocked(lockName,lockTimeMillis);
System.out.println(lockName+locked);
if(locked)
throw new BadRequestAlertException("服务正在执行中,已上锁",methodName,lockName);
try {
return point.proceed();
}
finally {
if(!locked)
lockService.unLock(lockName);
}
}
public String[] getParamterNames(Method method){
LocalVariableTableParameterNameDiscoverer u =
new LocalVariableTableParameterNameDiscoverer();
return u.getParameterNames(method);
}
}
\ No newline at end of file
......@@ -16,24 +16,26 @@ public class LockService {
private LockService proxy;
@Cacheable( value="lock", key = "'key:'+#p0")
protected Long getLock(String key)
public synchronized Long getLock(String key)
{
return System.currentTimeMillis();
long lockTime=System.currentTimeMillis();
System.out.println("lockTime"+lockTime);
return lockTime;
}
@Cacheable( value="lock", key = "'key:'+#p0")
private Long resetLock(String key,Long newLockTime)
public Long resetLock(String key,Long newLockTime)
{
return newLockTime;
}
@CacheEvict( value="lock", key = "'key:'+#p0")
private void deleteLock(String key)
public void deleteLock(String key)
{
}
public boolean lock(String key,Long lockTimeMillis)
public synchronized boolean isLocked(String key,Long lockTimeMillis)
{
if(StringUtils.isEmpty(key))
return false;
......@@ -41,10 +43,11 @@ public class LockService {
Long lockTime=proxy.getLock(key);
System.out.println(lockTime+":"+now);
if(lockTime>=now)
return true;
return false;
if(lockTime+lockTimeMillis>now)
if(lockTime+lockTimeMillis<now)
{
proxy.deleteLock(key);
proxy.resetLock(key,now);
......@@ -58,11 +61,11 @@ public class LockService {
}
proxy.resetLock(key,now);
}
});
return true;
}).start();
return false;
}
return false;
return true;
}
public void unLock(String key)
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册