-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
backup commit - reentrant aspectJ lock
- Loading branch information
1 parent
ff961bb
commit c0fc1d3
Showing
11 changed files
with
275 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 0 additions & 20 deletions
20
examples/spring-boot3-simple/src/main/java/module-info.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
.../src/main/java/org/eclipse/store/integrations/spring/boot/types/concurent/LockAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package org.eclipse.store.integrations.spring.boot.types.concurent; | ||
|
||
/*- | ||
* #%L | ||
* spring-boot3 | ||
* %% | ||
* Copyright (C) 2023 MicroStream Software | ||
* %% | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* #L% | ||
*/ | ||
|
||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.reflect.MethodSignature; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.locks.ReadWriteLock; | ||
import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
|
||
@Aspect | ||
@Component | ||
public class LockAspect | ||
{ | ||
private final ReentrantReadWriteLock globalLock = new ReentrantReadWriteLock(); | ||
|
||
private final Map<String, ReentrantReadWriteLock> locks = new HashMap<>(); | ||
|
||
@Around("@annotation(org.eclipse.store.integrations.spring.boot.types.concurent.Read)") | ||
public Object readOperation(ProceedingJoinPoint joinPoint) throws Throwable | ||
{ | ||
ReentrantReadWriteLock lock = this.findLock(joinPoint); | ||
lock.readLock().lock(); | ||
System.out.println("read lock"); | ||
Object proceed; | ||
try | ||
{ | ||
proceed = joinPoint.proceed(); | ||
} finally | ||
{ | ||
System.out.println("read unlock"); | ||
lock.readLock().unlock(); | ||
} | ||
return proceed; | ||
} | ||
|
||
@Around("@annotation(org.eclipse.store.integrations.spring.boot.types.concurent.Write)") | ||
public Object writeOperation(ProceedingJoinPoint joinPoint) throws Throwable | ||
{ | ||
ReentrantReadWriteLock lock = this.findLock(joinPoint); | ||
lock.readLock().lock(); | ||
|
||
System.out.println("write lock"); | ||
Object proceed; | ||
try | ||
{ | ||
proceed = joinPoint.proceed(); | ||
} finally | ||
{ | ||
System.out.println("write unlock"); | ||
lock.writeLock().unlock(); | ||
} | ||
return proceed; | ||
} | ||
|
||
private ReentrantReadWriteLock findLock(ProceedingJoinPoint joinPoint) | ||
{ | ||
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); | ||
Method method = methodSignature.getMethod(); | ||
|
||
//method annotation first | ||
ReentrantReadWriteLock finalLock; | ||
Lockable annotation = method.getAnnotation(Lockable.class); | ||
if (annotation != null) | ||
{ | ||
String lockName = annotation.value(); | ||
finalLock = processLockByName(lockName); | ||
} else | ||
{ | ||
//class annotation second | ||
Class<?> declaringClass = method.getDeclaringClass(); | ||
Lockable classAnnotation = declaringClass.getAnnotation(Lockable.class); | ||
if (classAnnotation != null) | ||
{ | ||
String classLockName = classAnnotation.value(); | ||
finalLock = processLockByName(classLockName); | ||
} else | ||
{ | ||
// no annotation, use global lock | ||
finalLock = globalLock; | ||
} | ||
} | ||
return finalLock; | ||
} | ||
|
||
private ReentrantReadWriteLock processLockByName(String lockName) | ||
{ | ||
ReentrantReadWriteLock newLock; | ||
|
||
if (locks.containsKey(lockName)) | ||
{ | ||
newLock = locks.get(lockName); | ||
} else | ||
{ | ||
newLock = new ReentrantReadWriteLock(); | ||
locks.put(lockName, newLock); | ||
} | ||
return newLock; | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
...t3/src/main/java/org/eclipse/store/integrations/spring/boot/types/concurent/Lockable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.eclipse.store.integrations.spring.boot.types.concurent; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Lockable | ||
{ | ||
String value(); | ||
} |
26 changes: 26 additions & 0 deletions
26
...-boot3/src/main/java/org/eclipse/store/integrations/spring/boot/types/concurent/Read.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.eclipse.store.integrations.spring.boot.types.concurent; | ||
|
||
/*- | ||
* #%L | ||
* spring-boot3 | ||
* %% | ||
* Copyright (C) 2023 MicroStream Software | ||
* %% | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* #L% | ||
*/ | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Read | ||
{ | ||
} |
26 changes: 26 additions & 0 deletions
26
...boot3/src/main/java/org/eclipse/store/integrations/spring/boot/types/concurent/Write.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.eclipse.store.integrations.spring.boot.types.concurent; | ||
|
||
/*- | ||
* #%L | ||
* spring-boot3 | ||
* %% | ||
* Copyright (C) 2023 MicroStream Software | ||
* %% | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* #L% | ||
*/ | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Write | ||
{ | ||
} |
Oops, something went wrong.