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