88import org .aspectj .lang .reflect .MethodSignature ;
99import org .springframework .lock .annotation .ReadLock ;
1010import org .springframework .lock .timer .InterruptTimer ;
11+ import org .springframework .util .StringUtils ;
1112
1213import java .lang .reflect .Field ;
1314import java .lang .reflect .Method ;
@@ -35,49 +36,52 @@ public class ReadLockAspect {
3536 */
3637 @ Around ("@annotation(org.springframework.lock.annotation.ReadLock)" )
3738 public Object aroundReadLock (ProceedingJoinPoint jp ) throws Throwable {
38- Object obj = jp .getTarget ();
39- Class <?> clz = obj .getClass ();
40- Lock readLock = null ;
41- ReentrantReadWriteLock lock = null ;
42- for (Field field : clz .getDeclaredFields ()) {
43- if ("$readLock" .equals (field .getName ())){
44- field .setAccessible (true );
45- readLock = (Lock ) field .get (obj );
46- }
47- if ("$lock" .equals (field .getName ())){
48- field .setAccessible (true );
49- lock = (ReentrantReadWriteLock ) field .get (obj );
50- }
51- if (lock != null && readLock != null )
52- // 都找到了
53- break ;
54- }
55- if (readLock == null || lock == null ){
56- // 连锁都没拿到,说明编译期间出了问题
57- LOGGER .warn (clz .getSimpleName () + "编译时生成读写锁锁失败,未能加锁" );
58- return jp .proceed ();
59- }
60-
39+ // 获取注解的属性
6140 long waitTime = Long .MAX_VALUE ;
6241 long executeTime = Long .MAX_VALUE ;
6342 boolean isContinueIfElapsed = false ;
6443 boolean withLockIfContinue = false ;
44+ String lockName = null ;
6545
6646 MethodSignature signature = (MethodSignature ) jp .getSignature ();
6747 Method method = signature .getMethod ();
68- if (method == null ) {
69- // 没拿到方法,那注解给了谁呢?
70- LOGGER .warn ("没拿到方法" + signature );
71- return jp .proceed ();
72- }
7348 ReadLock annotation = method .getAnnotation (ReadLock .class );
7449 if (annotation != null ) {
7550 waitTime = annotation .waitTime ();
7651 executeTime = annotation .executeTime ();
7752 isContinueIfElapsed = annotation .isContinueIfElapsed ();
7853 withLockIfContinue = annotation .withLockIfContinue ();
54+ lockName = annotation .value ();
55+ }
56+
57+ // 获取锁对象
58+ Lock readLock = null ;
59+ ReentrantReadWriteLock lock = null ;
60+ Object obj = jp .getTarget ();
61+ Class <?> clz = obj .getClass ();
62+ Field field = null ;
63+
64+ if (StringUtils .hasText (lockName )){
65+ field = clz .getDeclaredField (lockName );
66+ field .setAccessible (true );
67+ lock = (ReentrantReadWriteLock ) field .get (obj );
68+ readLock = lock .readLock ();
69+ }else {
70+ field = clz .getDeclaredField ("$readLock" );
71+ field .setAccessible (true );
72+ readLock = (Lock ) field .get (obj );
73+ field = clz .getDeclaredField ("$lock" );
74+ field .setAccessible (true );
75+ lock = (ReentrantReadWriteLock ) field .get (obj );
76+ }
77+
78+ if (readLock == null || lock == null ){
79+ // 连锁都没拿到,说明编译期间出了问题
80+ LOGGER .warn (clz .getSimpleName () + "获取锁错误,请检查锁名称是否正确" );
81+ return jp .proceed ();
7982 }
8083
84+ // 尝试加锁,不行就结束掉别人的线程
8185 Object result = null ;
8286 boolean locked = readLock .tryLock (waitTime , MILLISECONDS );
8387 if (locked ) {
@@ -91,12 +95,12 @@ public Object aroundReadLock(ProceedingJoinPoint jp) throws Throwable {
9195 Thread lockedThread = (Thread ) getOwner .invoke (lock );
9296 lockedThread .interrupt ();
9397 if (readLock .tryLock (waitTime , MILLISECONDS )){
94- LOGGER .warn ("等待时间耗尽,终止线程" + lockedThread + "以强制获得锁" );
98+ LOGGER .warn ("等待时间耗尽,终止线程" + lockedThread + "以强制获得读锁" + readLock );
9599 result = this .processMethod (jp , readLock , executeTime );
96100 }else {
97101 lockedThread .stop ();
98102 if (readLock .tryLock (waitTime , MILLISECONDS )){
99- LOGGER .warn ("等待时间耗尽,终止线程" + lockedThread + "以强制获得锁" );
103+ LOGGER .warn ("等待时间耗尽,终止线程" + lockedThread + "以强制获得读锁" + readLock );
100104 result = this .processMethod (jp , readLock , executeTime );
101105 }
102106 }
@@ -105,7 +109,7 @@ public Object aroundReadLock(ProceedingJoinPoint jp) throws Throwable {
105109 result = jp .proceed ();
106110 }
107111 }else {
108- LOGGER .warn ("等待时间耗尽,将不执行 " + method .getName ());
112+ LOGGER .warn ("等待时间耗尽,放弃执行 " + method .getName ());
109113 }
110114 }
111115 return result ;
@@ -121,13 +125,13 @@ public Object aroundReadLock(ProceedingJoinPoint jp) throws Throwable {
121125 */
122126 private Object processMethod (ProceedingJoinPoint jp , Lock readLock , long executeTime ) throws Throwable {
123127 Object result = null ;
124- LOGGER .info (Thread .currentThread ().getName () + "获得读锁" );
128+ LOGGER .info (Thread .currentThread ().getName () + "获得读锁" + readLock );
125129 new InterruptTimer (Thread .currentThread (), executeTime );
126130 try {
127131 result = jp .proceed ();
128132 } finally {
129133 readLock .unlock ();
130- LOGGER .info (Thread .currentThread ().getName () + "释放读锁" );
134+ LOGGER .info (Thread .currentThread ().getName () + "释放读锁" + readLock );
131135 }
132136 return result ;
133137 }
0 commit comments