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.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   * Provider of {@link RedissonSemaphoreNamedLockFactory} using Redisson and {@link org.redisson.api.RSemaphore}.
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              /* There is no reasonable way to destroy the semaphore in Redis because we cannot know
68               * when the last process has stopped using it.
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  }