Skip to content

Commit 3234f99

Browse files
committed
Revert back to java 8
1 parent da25afd commit 3234f99

File tree

28 files changed

+170
-145
lines changed

28 files changed

+170
-145
lines changed

README.adoc

+8-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Distributed lock ensures your method cannot be run in parallel from multiple JVMs (cluster of servers, microservices, ...).
44
It uses a common store to keep track of used locks and your method needs to acquire one or more locks to run.
55

6-
By default, locks follow methods lifecycle. They are obtained at the start of the method and released at the end of the method.
6+
By default, locks follow methods lifecycle.They are obtained at the start of the method and released at the end of the method.
77
Manual controlling is supported and explained later in this document.
88

99
All locks acquired by lock implementations in this project will expire after 10 seconds, timeout after 1 second if unable to acquire lock and sleep for 50 ms between retries.
@@ -19,9 +19,9 @@ This will import the configuration that will autoconfigure the
1919

2020
Project provides the following out-of-the-box lock implementations:
2121

22-
* JDBC
23-
* Mongo
24-
* Redis
22+
* JDBC
23+
* Mongo
24+
* Redis
2525

2626
=== JDBC locks
2727

@@ -124,6 +124,10 @@ public class LockConfiguration {
124124

125125
Started tracking the changes since 1.2.0 so no changelogs available for earlier versions.
126126

127+
==== 1.4.0
128+
129+
- CHANGE: Switched back to Java 1.8 from 11 since most projects don't yet use 11
130+
127131
==== 1.3.0
128132

129133
- CHANGE: Updated Java from 1.8 to 11

distributed-lock-api/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<parent>
99
<groupId>com.github.alturkovic</groupId>
1010
<artifactId>distributed-lock</artifactId>
11-
<version>1.3.0</version>
11+
<version>1.4.0</version>
1212
</parent>
1313

1414
<artifactId>distributed-lock-api</artifactId>

distributed-lock-core/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<parent>
99
<groupId>com.github.alturkovic</groupId>
1010
<artifactId>distributed-lock</artifactId>
11-
<version>1.3.0</version>
11+
<version>1.4.0</version>
1212
</parent>
1313

1414
<artifactId>distributed-lock-core</artifactId>

distributed-lock-core/src/main/java/com/github/alturkovic/lock/AbstractSimpleLock.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public abstract class AbstractSimpleLock implements Lock {
4141
public String acquire(final List<String> keys, final String storeId, final long expiration) {
4242
Assert.isTrue(keys.size() == 1, "Cannot acquire lock for multiple keys with this lock");
4343

44-
final var token = tokenSupplier.get();
44+
final String token = tokenSupplier.get();
4545
if (StringUtils.isEmpty(token)) {
4646
throw new IllegalStateException("Cannot lock with empty token");
4747
}

distributed-lock-core/src/main/java/com/github/alturkovic/lock/advice/LockBeanPostProcessor.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.github.alturkovic.lock.key.KeyGenerator;
3030
import com.github.alturkovic.lock.retry.RetriableLockFactory;
3131
import lombok.AllArgsConstructor;
32+
import org.aopalliance.intercept.Interceptor;
3233
import org.springframework.aop.framework.AbstractAdvisingBeanPostProcessor;
3334
import org.springframework.aop.support.DefaultPointcutAdvisor;
3435
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;
@@ -48,8 +49,8 @@ public class LockBeanPostProcessor extends AbstractAdvisingBeanPostProcessor imp
4849

4950
@Override
5051
public void afterPropertiesSet() {
51-
final var pointcut = new AnnotationMatchingPointcut(null, Locked.class, true);
52-
final var interceptor = new LockMethodInterceptor(keyGenerator, lockTypeResolver, intervalConverter, retriableLockFactory, taskScheduler);
52+
final AnnotationMatchingPointcut pointcut = new AnnotationMatchingPointcut(null, Locked.class, true);
53+
final Interceptor interceptor = new LockMethodInterceptor(keyGenerator, lockTypeResolver, intervalConverter, retriableLockFactory, taskScheduler);
5354

5455
this.advisor = new DefaultPointcutAdvisor(pointcut, interceptor);
5556
}

distributed-lock-core/src/main/java/com/github/alturkovic/lock/advice/LockMethodInterceptor.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class LockMethodInterceptor implements MethodInterceptor {
5454

5555
@Override
5656
public Object invoke(final MethodInvocation invocation) throws Throwable {
57-
final var context = new LockContext(invocation);
57+
final LockContext context = new LockContext(invocation);
5858
try {
5959
return executeLockedMethod(invocation, context);
6060
} finally {
@@ -63,7 +63,7 @@ public Object invoke(final MethodInvocation invocation) throws Throwable {
6363
}
6464

6565
private Object executeLockedMethod(final MethodInvocation invocation, final LockContext context) throws Throwable {
66-
final var expiration = intervalConverter.toMillis(context.getLocked().expiration());
66+
final long expiration = intervalConverter.toMillis(context.getLocked().expiration());
6767
try {
6868
context.setToken(retriableLockFactory.generate(context.getLock(), context.getLocked()).acquire(context.getKeys(), context.getLocked().storeId(), expiration));
6969
} catch (final Exception e) {
@@ -77,7 +77,7 @@ private Object executeLockedMethod(final MethodInvocation invocation, final Lock
7777
}
7878

7979
private void scheduleLockRefresh(final LockContext context, final long expiration) {
80-
final var refresh = intervalConverter.toMillis(context.getLocked().refresh());
80+
final long refresh = intervalConverter.toMillis(context.getLocked().refresh());
8181
if (refresh > 0) {
8282
context.setScheduledFuture(taskScheduler.scheduleAtFixedRate(constructRefreshRunnable(context, expiration), refresh));
8383
}
@@ -88,13 +88,13 @@ private Runnable constructRefreshRunnable(final LockContext context, final long
8888
}
8989

9090
private void cleanAfterExecution(final LockContext context) {
91-
final var scheduledFuture = context.getScheduledFuture();
91+
final ScheduledFuture<?> scheduledFuture = context.getScheduledFuture();
9292
if (scheduledFuture != null && !scheduledFuture.isCancelled() && !scheduledFuture.isDone()) {
9393
scheduledFuture.cancel(true);
9494
}
9595

9696
if (StringUtils.hasText(context.getToken()) && !context.getLocked().manuallyReleased()) {
97-
final var released = context.getLock().release(context.getKeys(), context.getLocked().storeId(), context.getToken());
97+
final boolean released = context.getLock().release(context.getKeys(), context.getLocked().storeId(), context.getToken());
9898
if (released) {
9999
log.debug("Released lock for keys {} with token {} in store {}", context.getKeys(), context.getToken(), context.getLocked().storeId());
100100
} else {

distributed-lock-core/src/main/java/com/github/alturkovic/lock/configuration/DistributedLockConfiguration.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public LockBeanPostProcessor lockBeanPostProcessor(final KeyGenerator keyGenerat
5454
final IntervalConverter intervalConverter,
5555
final RetriableLockFactory retriableLockFactory,
5656
@Autowired(required = false) final TaskScheduler taskScheduler) {
57-
final var processor = new LockBeanPostProcessor(keyGenerator, configurableBeanFactory::getBean, intervalConverter, retriableLockFactory, taskScheduler);
57+
final LockBeanPostProcessor processor = new LockBeanPostProcessor(keyGenerator, configurableBeanFactory::getBean, intervalConverter, retriableLockFactory, taskScheduler);
5858
processor.setBeforeExistingAdvisors(true);
5959
return processor;
6060
}

distributed-lock-core/src/main/java/com/github/alturkovic/lock/interval/BeanFactoryAwareIntervalConverter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public long toMillis(final Interval interval) {
4242
}
4343

4444
private String resolveMilliseconds(final Interval interval) {
45-
final var value = beanFactory.resolveEmbeddedValue(interval.value());
45+
final String value = beanFactory.resolveEmbeddedValue(interval.value());
4646
if (!StringUtils.hasText(value)) {
4747
throw new IllegalArgumentException("Cannot convert interval " + interval + " to milliseconds");
4848
}

distributed-lock-core/src/main/java/com/github/alturkovic/lock/key/SpelKeyGenerator.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.github.alturkovic.lock.exception.EvaluationConvertException;
2828
import java.lang.reflect.Method;
2929
import java.util.Collection;
30+
import java.util.Collections;
3031
import java.util.List;
3132
import java.util.Map;
3233
import java.util.Objects;
@@ -47,9 +48,8 @@
4748
@Data
4849
@EqualsAndHashCode(callSuper = false)
4950
public class SpelKeyGenerator extends CachedExpressionEvaluator implements KeyGenerator {
50-
private final ConversionService conversionService;
5151
private final Map<ExpressionKey, Expression> conditionCache = new ConcurrentHashMap<>();
52-
private final Map<AnnotatedElementKey, Method> targetMethodCache = new ConcurrentHashMap<>();
52+
private final ConversionService conversionService;
5353

5454
@Override
5555
public List<String> resolveKeys(final String lockKeyPrefix, final String expression, final Object object, final Method method, final Object[] args) {
@@ -74,7 +74,7 @@ protected List<String> convertResultToList(final Object expressionValue) {
7474
} else if (expressionValue.getClass().isArray()) {
7575
list = arrayToList(expressionValue);
7676
} else {
77-
list = List.of(expressionValue.toString());
77+
list = Collections.singletonList(expressionValue.toString());
7878
}
7979

8080
if (CollectionUtils.isEmpty(list)) {
@@ -99,17 +99,17 @@ private Object evaluateExpression(final String expression, final Object object,
9999

100100
private List<String> iterableToList(final Object expressionValue) {
101101
final TypeDescriptor genericCollection = TypeDescriptor.collection(Collection.class, TypeDescriptor.valueOf(Object.class));
102-
final TypeDescriptor stringList = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(String.class));
103-
104-
//noinspection unchecked
105-
return (List<String>) conversionService.convert(expressionValue, genericCollection, stringList);
102+
return toList(expressionValue, genericCollection);
106103
}
107104

108105
private List<String> arrayToList(final Object expressionValue) {
109106
final TypeDescriptor genericArray = TypeDescriptor.array(TypeDescriptor.valueOf(Object.class));
110-
final TypeDescriptor stringList = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(String.class));
107+
return toList(expressionValue, genericArray);
108+
}
111109

110+
private List<String> toList(final Object expressionValue, final TypeDescriptor from) {
111+
final TypeDescriptor listTypeDescriptor = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(String.class));
112112
//noinspection unchecked
113-
return (List<String>) conversionService.convert(expressionValue, genericArray, stringList);
113+
return (List<String>) conversionService.convert(expressionValue, from, listTypeDescriptor);
114114
}
115115
}

distributed-lock-core/src/main/java/com/github/alturkovic/lock/retry/DefaultRetryTemplateConverter.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import com.github.alturkovic.lock.Locked;
2828
import com.github.alturkovic.lock.exception.LockNotAvailableException;
2929
import com.github.alturkovic.lock.interval.IntervalConverter;
30-
import java.util.Map;
30+
import java.util.Collections;
3131
import lombok.Data;
3232
import org.springframework.retry.RetryPolicy;
3333
import org.springframework.retry.backoff.FixedBackOffPolicy;
@@ -42,34 +42,34 @@ public class DefaultRetryTemplateConverter implements RetryTemplateConverter {
4242

4343
@Override
4444
public RetryTemplate construct(final Locked locked) {
45-
final var retryTemplate = new RetryTemplate();
45+
final RetryTemplate retryTemplate = new RetryTemplate();
4646
retryTemplate.setRetryPolicy(resolveLockRetryPolicy(locked));
4747
retryTemplate.setBackOffPolicy(resolveBackOffPolicy(locked));
4848
return retryTemplate;
4949
}
5050

5151
private CompositeRetryPolicy resolveLockRetryPolicy(final Locked locked) {
52-
final var compositeRetryPolicy = new CompositeRetryPolicy();
52+
final CompositeRetryPolicy compositeRetryPolicy = new CompositeRetryPolicy();
5353

54-
final var timeoutRetryPolicy = resolveTimeoutRetryPolicy(locked);
55-
final var exceptionTypeRetryPolicy = resolveExceptionTypeRetryPolicy();
54+
final RetryPolicy timeoutRetryPolicy = resolveTimeoutRetryPolicy(locked);
55+
final RetryPolicy exceptionTypeRetryPolicy = resolveExceptionTypeRetryPolicy();
5656

5757
compositeRetryPolicy.setPolicies(new RetryPolicy[]{timeoutRetryPolicy, exceptionTypeRetryPolicy});
5858
return compositeRetryPolicy;
5959
}
6060

6161
private RetryPolicy resolveTimeoutRetryPolicy(final Locked locked) {
62-
final var timeoutRetryPolicy = new TimeoutRetryPolicy();
62+
final TimeoutRetryPolicy timeoutRetryPolicy = new TimeoutRetryPolicy();
6363
timeoutRetryPolicy.setTimeout(intervalConverter.toMillis(locked.timeout()));
6464
return timeoutRetryPolicy;
6565
}
6666

6767
private RetryPolicy resolveExceptionTypeRetryPolicy() {
68-
return new SimpleRetryPolicy(Integer.MAX_VALUE, Map.of(LockNotAvailableException.class, true));
68+
return new SimpleRetryPolicy(Integer.MAX_VALUE, Collections.singletonMap(LockNotAvailableException.class, true));
6969
}
7070

7171
private FixedBackOffPolicy resolveBackOffPolicy(final Locked locked) {
72-
final var fixedBackOffPolicy = new FixedBackOffPolicy();
72+
final FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
7373
fixedBackOffPolicy.setBackOffPeriod(intervalConverter.toMillis(locked.retry()));
7474
return fixedBackOffPolicy;
7575
}

distributed-lock-core/src/test/java/com/github/alturkovic/lock/advice/LockBeanPostProcessorTest.java

+11-8
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@
2727
import com.github.alturkovic.lock.Interval;
2828
import com.github.alturkovic.lock.Locked;
2929
import com.github.alturkovic.lock.advice.support.SimpleLock;
30+
import com.github.alturkovic.lock.advice.support.SimpleLock.LockedKey;
3031
import com.github.alturkovic.lock.advice.support.SimpleLocked;
3132
import com.github.alturkovic.lock.interval.BeanFactoryAwareIntervalConverter;
33+
import com.github.alturkovic.lock.interval.IntervalConverter;
3234
import com.github.alturkovic.lock.key.SpelKeyGenerator;
3335
import com.github.alturkovic.lock.retry.DefaultRetriableLockFactory;
3436
import com.github.alturkovic.lock.retry.DefaultRetryTemplateConverter;
37+
import com.github.alturkovic.lock.retry.RetriableLockFactory;
3538
import java.util.concurrent.TimeUnit;
3639
import org.assertj.core.data.Offset;
3740
import org.junit.Before;
@@ -51,20 +54,20 @@ public class LockBeanPostProcessorTest {
5154

5255
@Before
5356
public void setUp() {
54-
final var beanFactory = new DefaultListableBeanFactory();
57+
final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
5558
lock = new SimpleLock();
5659

57-
final var lockTypeResolver = Mockito.mock(LockTypeResolver.class);
60+
final LockTypeResolver lockTypeResolver = Mockito.mock(LockTypeResolver.class);
5861
when(lockTypeResolver.get(SimpleLock.class)).thenReturn(lock);
5962

60-
final var scheduler = new ThreadPoolTaskScheduler();
63+
final ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
6164
scheduler.afterPropertiesSet();
6265

63-
final var keyGenerator = new SpelKeyGenerator(new DefaultConversionService());
64-
final var intervalConverter = new BeanFactoryAwareIntervalConverter(beanFactory);
65-
final var retriableLockFactory = new DefaultRetriableLockFactory(new DefaultRetryTemplateConverter(intervalConverter));
66+
final SpelKeyGenerator keyGenerator = new SpelKeyGenerator(new DefaultConversionService());
67+
final IntervalConverter intervalConverter = new BeanFactoryAwareIntervalConverter(beanFactory);
68+
final RetriableLockFactory retriableLockFactory = new DefaultRetriableLockFactory(new DefaultRetryTemplateConverter(intervalConverter));
6669

67-
final var processor = new LockBeanPostProcessor(keyGenerator, lockTypeResolver, intervalConverter, retriableLockFactory, scheduler);
70+
final LockBeanPostProcessor processor = new LockBeanPostProcessor(keyGenerator, lockTypeResolver, intervalConverter, retriableLockFactory, scheduler);
6871
processor.afterPropertiesSet();
6972

7073
beanFactory.addBeanPostProcessor(processor);
@@ -135,7 +138,7 @@ public void shouldLockFromImplementationWithImplementationDetail() {
135138
@Test
136139
public void shouldRefreshLock() throws InterruptedException {
137140
lockedInterface.sleep();
138-
final var lockedKey = this.lock.getLockMap().get("lock").get(0);
141+
final LockedKey lockedKey = this.lock.getLockMap().get("lock").get(0);
139142
assertThat(lockedKey.getUpdatedAt()).withFailMessage(lockedKey.toString()).isCloseTo(System.currentTimeMillis(), Offset.offset(200L));
140143
assertThat(lockedKey.getKey()).isEqualTo("com.github.alturkovic.lock.advice.LockBeanPostProcessorTest.LockedInterfaceImpl.sleep");
141144

distributed-lock-core/src/test/java/com/github/alturkovic/lock/advice/support/SimpleLock.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,18 @@ public class SimpleLock implements Lock {
4545
public String acquire(final List<String> keys, final String storeId, final long expiration) {
4646
log.debug("Acquiring lock for keys {} in store {} with expiration: {}", keys, storeId, expiration);
4747

48-
final var lockedKeysWithExpiration = lockMap.get(storeId);
48+
final List<LockedKey> lockedKeysWithExpiration = lockMap.get(storeId);
4949
if (lockedKeysWithExpiration != null) {
50-
final var locksForStore = lockedKeysWithExpiration.stream().map(LockedKey::getKey).collect(Collectors.toList());
50+
final List<String> locksForStore = lockedKeysWithExpiration.stream()
51+
.map(LockedKey::getKey)
52+
.collect(Collectors.toList());
53+
5154
if (keys.stream().anyMatch(locksForStore::contains)) {
5255
return null;
5356
}
5457
}
5558

56-
final var lockedKeys = lockMap.computeIfAbsent(storeId, s -> new ArrayList<>());
59+
final List<LockedKey> lockedKeys = lockMap.computeIfAbsent(storeId, s -> new ArrayList<>());
5760
keys.forEach(key -> lockedKeys.add(new LockedKey(key, expiration, System.currentTimeMillis(), System.currentTimeMillis(), 0, false)));
5861

5962
return UUID.randomUUID().toString();
@@ -62,7 +65,7 @@ public String acquire(final List<String> keys, final String storeId, final long
6265
@Override
6366
public boolean release(final List<String> keys, final String storeId, final String token) {
6467
log.debug("Releasing keys {} in store {} with token {}", keys, storeId, token);
65-
final var lockedKeys = lockMap.get(storeId);
68+
final List<LockedKey> lockedKeys = lockMap.get(storeId);
6669

6770
if (CollectionUtils.isEmpty(lockedKeys)) {
6871
return false;
@@ -79,7 +82,7 @@ public boolean release(final List<String> keys, final String storeId, final Stri
7982
@Override
8083
public boolean refresh(final List<String> keys, final String storeId, final String token, final long expiration) {
8184
log.debug("Refreshing keys {} in store {} with token {} to expiration: {}", keys, storeId, token, expiration);
82-
final var lockedKeys = lockMap.get(storeId);
85+
final List<LockedKey> lockedKeys = lockMap.get(storeId);
8386

8487
if (lockedKeys == null) {
8588
return false;

distributed-lock-core/src/test/java/com/github/alturkovic/lock/retry/DefaultRetriableLockFactoryTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ public class DefaultRetriableLockFactoryTest {
5555
public void shouldGenerateRetriableLock() {
5656
when(retryTemplateConverter.construct(eq(locked))).thenReturn(retryTemplate);
5757

58-
final var factory = new DefaultRetriableLockFactory(retryTemplateConverter);
59-
final var retriableLock = factory.generate(lock, locked);
58+
final RetriableLockFactory factory = new DefaultRetriableLockFactory(retryTemplateConverter);
59+
final RetriableLock retriableLock = factory.generate(lock, locked);
6060

6161
assertThat(retriableLock.getLock()).isEqualTo(lock);
6262
assertThat(retriableLock.getRetryTemplate()).isEqualTo(retryTemplate);

0 commit comments

Comments
 (0)