优化锁,自动释放为可选

This commit is contained in:
周浩
2016-07-05 10:03:42 +08:00
parent 17f2309107
commit ede9bfa73c
7 changed files with 100 additions and 7 deletions

View File

@@ -17,4 +17,6 @@ public @interface Lock {
long waitTime() default 5;
TimeUnit timeUnit() default TimeUnit.SECONDS;
boolean autoUnLock() default true;
}

View File

@@ -17,4 +17,6 @@ public @interface ReadLock {
long waitTime() default 5;
TimeUnit timeUnit() default TimeUnit.SECONDS;
boolean autoUnLock() default true;
}

View File

@@ -0,0 +1,14 @@
package org.hsweb.concurrent.lock.annotation;
import java.lang.annotation.*;
import java.util.concurrent.TimeUnit;
/**
* Created by zhouhao on 16-5-13.
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface UnLock {
}

View File

@@ -0,0 +1,13 @@
package org.hsweb.concurrent.lock.annotation;
import java.lang.annotation.*;
/**
* Created by zhouhao on 16-5-13.
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface UnReadLock {
}

View File

@@ -0,0 +1,13 @@
package org.hsweb.concurrent.lock.annotation;
import java.lang.annotation.*;
/**
* Created by zhouhao on 16-5-13.
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface UnWriteLock {
}

View File

@@ -17,4 +17,6 @@ public @interface WriteLock {
long waitTime() default 5;
TimeUnit timeUnit() default TimeUnit.SECONDS;
boolean autoUnLock() default true;
}

View File

@@ -38,6 +38,47 @@ public class AnnotationLockAopAdvice {
@Autowired(required = false)
private Map<String, ExpressionScopeBean> expressionScopeBeanMap;
@Around("@annotation(unLock)")
public Object unWriteLock(ProceedingJoinPoint pjp,
org.hsweb.concurrent.lock.annotation.UnWriteLock unLock) throws Throwable {
String name = getLockName(pjp);
ReadWriteLock lock = readWriteLockMap.get(name);
try {
return pjp.proceed();
} finally {
logger.debug("unlock :{}", name);
if (lock != null)
unlock(lock.writeLock());
}
}
@Around("@annotation(unLock)")
public Object unReadLock(ProceedingJoinPoint pjp,
org.hsweb.concurrent.lock.annotation.UnReadLock unLock) throws Throwable {
String name = getLockName(pjp);
ReadWriteLock lock = readWriteLockMap.get(name);
try {
return pjp.proceed();
} finally {
logger.debug("unlock :{}", name);
if (lock != null)
unlock(lock.readLock());
}
}
@Around("@annotation(unLock)")
public Object unlock(ProceedingJoinPoint pjp,
org.hsweb.concurrent.lock.annotation.UnLock unLock) throws Throwable {
String name = getLockName(pjp);
Lock lock = lockMap.get(name);
try {
return pjp.proceed();
} finally {
logger.debug("unlock :{}", name);
unlock(lock);
}
}
@Around("@annotation(lock)")
public Object lock(ProceedingJoinPoint pjp,
org.hsweb.concurrent.lock.annotation.Lock lock) throws Throwable {
@@ -54,8 +95,10 @@ public class AnnotationLockAopAdvice {
if (!locked) throw new LockException(name + "error");
return pjp.proceed();
} finally {
logger.debug("unlock :{}", name);
unlock(_lock);
if (lock.autoUnLock()) {
logger.debug("unlock :{}", name);
unlock(_lock);
}
}
}
@@ -76,8 +119,10 @@ public class AnnotationLockAopAdvice {
if (!locked) throw new LockException(name + "error");
return pjp.proceed();
} finally {
logger.debug("unlock readLock :{} ", name);
unlock(readLock);
if (lock.autoUnLock()) {
logger.debug("unlock readLock :{} ", name);
unlock(readLock);
}
}
}
@@ -98,8 +143,10 @@ public class AnnotationLockAopAdvice {
if (!locked) throw new LockException(name + "error");
return pjp.proceed();
} finally {
logger.debug("unlock writeLock:{} ", name);
unlock(writeLock);
if (lock.autoUnLock()) {
logger.debug("unlock writeLock:{} ", name);
unlock(writeLock);
}
}
}
@@ -142,7 +189,7 @@ public class AnnotationLockAopAdvice {
try {
lock.unlock();
} catch (Throwable e) {
logger.error("unlock error",e);
logger.error("unlock error", e);
}
}
}