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.support.AdaptedSemaphoreNamedLock;
29 import org.redisson.api.RSemaphore;
30
31
32
33
34 @Singleton
35 @Named(RedissonSemaphoreNamedLockFactory.NAME)
36 public class RedissonSemaphoreNamedLockFactory extends RedissonNamedLockFactorySupport {
37 public static final String NAME = "semaphore-redisson";
38
39 private static final String TYPED_NAME_PREFIX = NAME_PREFIX + NAME + ":";
40
41 private final ConcurrentMap<String, RSemaphore> semaphores;
42
43 public RedissonSemaphoreNamedLockFactory() {
44 this.semaphores = new ConcurrentHashMap<>();
45 }
46
47 @Override
48 protected AdaptedSemaphoreNamedLock createLock(final String name) {
49 RSemaphore semaphore = semaphores.computeIfAbsent(name, k -> {
50 RSemaphore result = redissonClient.getSemaphore(TYPED_NAME_PREFIX + k);
51 result.trySetPermits(Integer.MAX_VALUE);
52 return result;
53 });
54 return new AdaptedSemaphoreNamedLock(name, this, new RedissonSemaphore(semaphore));
55 }
56
57 @Override
58 protected void destroyLock(final String name) {
59 RSemaphore semaphore = semaphores.remove(name);
60 if (semaphore == null) {
61 throw new IllegalStateException("Semaphore expected, but does not exist: " + name);
62 }
63
64
65
66 }
67
68 private static final class RedissonSemaphore implements AdaptedSemaphoreNamedLock.AdaptedSemaphore {
69 private final RSemaphore semaphore;
70
71 private RedissonSemaphore(final RSemaphore semaphore) {
72 this.semaphore = semaphore;
73 }
74
75 @Override
76 public boolean tryAcquire(final int perms, final long time, final TimeUnit unit) throws InterruptedException {
77 return semaphore.tryAcquire(perms, time, unit);
78 }
79
80 @Override
81 public void release(final int perms) {
82 semaphore.release(perms);
83 }
84 }
85 }