View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Provider of {@link RedissonSemaphoreNamedLockFactory} using Redisson and {@link org.redisson.api.RSemaphore}.
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          /* Threre is no reasonable way to destroy the semaphore in Redis because we cannot know
64           * when the last process has stopped using it.
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  }