1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.named.redisson;
20
21 import javax.inject.Named;
22 import javax.inject.Singleton;
23
24 import java.util.concurrent.ConcurrentHashMap;
25 import java.util.concurrent.ConcurrentMap;
26 import java.util.concurrent.TimeUnit;
27
28 import org.eclipse.aether.named.NamedLock;
29 import org.eclipse.aether.named.NamedLockKey;
30 import org.eclipse.aether.named.support.AdaptedSemaphoreNamedLock;
31 import org.redisson.api.RSemaphore;
32
33
34
35
36 @Singleton
37 @Named(RedissonSemaphoreNamedLockFactory.NAME)
38 public class RedissonSemaphoreNamedLockFactory extends RedissonNamedLockFactorySupport {
39 public static final String NAME = "semaphore-redisson";
40
41 private static final String TYPED_NAME_PREFIX = NAME_PREFIX + NAME + ":";
42
43 private final ConcurrentMap<NamedLockKey, RSemaphore> semaphores;
44
45 public RedissonSemaphoreNamedLockFactory() {
46 this.semaphores = new ConcurrentHashMap<>();
47 }
48
49 @Override
50 protected AdaptedSemaphoreNamedLock createLock(final NamedLockKey key) {
51 RSemaphore semaphore = semaphores.computeIfAbsent(key, k -> {
52 RSemaphore result = redissonClient.getSemaphore(TYPED_NAME_PREFIX + k.name());
53 result.trySetPermits(Integer.MAX_VALUE);
54 return result;
55 });
56 return new AdaptedSemaphoreNamedLock(key, this, new RedissonSemaphore(semaphore));
57 }
58
59 @Override
60 protected void destroyLock(final NamedLock namedLock) {
61 if (namedLock instanceof AdaptedSemaphoreNamedLock) {
62 final NamedLockKey key = namedLock.key();
63 RSemaphore semaphore = semaphores.remove(key);
64 if (semaphore == null) {
65 throw new IllegalStateException("Semaphore expected, but does not exist: " + key);
66 }
67
68
69
70 }
71 }
72
73 private static final class RedissonSemaphore implements AdaptedSemaphoreNamedLock.AdaptedSemaphore {
74 private final RSemaphore semaphore;
75
76 private RedissonSemaphore(final RSemaphore semaphore) {
77 this.semaphore = semaphore;
78 }
79
80 @Override
81 public boolean tryAcquire(final int perms, final long time, final TimeUnit unit) throws InterruptedException {
82 return semaphore.tryAcquire(perms, time, unit);
83 }
84
85 @Override
86 public void release(final int perms) {
87 semaphore.release(perms);
88 }
89 }
90 }