View Javadoc
1   package org.eclipse.aether.named.redisson;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Provider of {@link RedissonSemaphoreNamedLockFactory} using Redisson and {@link org.redisson.api.RSemaphore}.
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 final ConcurrentMap<String, RSemaphore> semaphores;
42  
43      public RedissonSemaphoreNamedLockFactory()
44      {
45          this.semaphores = new ConcurrentHashMap<>();
46      }
47  
48      @Override
49      protected AdaptedSemaphoreNamedLock createLock( final String name )
50      {
51          RSemaphore semaphore = semaphores.computeIfAbsent( name, k ->
52          {
53              RSemaphore result = redissonClient.getSemaphore( NAME_PREFIX + k );
54              result.trySetPermits( Integer.MAX_VALUE );
55              return result;
56          } );
57          return new AdaptedSemaphoreNamedLock( name, this, new RedissonSemaphore( semaphore ) );
58      }
59  
60      @Override
61      protected void destroyLock( final String name )
62      {
63          RSemaphore semaphore = semaphores.remove( name );
64          if ( semaphore == null )
65          {
66              throw new IllegalStateException( "Semaphore expected but does not exist: " + name );
67          }
68          semaphore.delete();
69      }
70  
71      private static final class RedissonSemaphore implements AdaptedSemaphoreNamedLock.AdaptedSemaphore
72      {
73          private final RSemaphore semaphore;
74  
75          private RedissonSemaphore( final RSemaphore semaphore )
76          {
77              this.semaphore = semaphore;
78          }
79  
80          @Override
81          public boolean tryAcquire( final int perms, final long time, final TimeUnit unit ) throws InterruptedException
82          {
83              return semaphore.tryAcquire( perms, time, unit );
84          }
85  
86          @Override
87          public void release( final int perms )
88          {
89              semaphore.release( perms );
90          }
91      }
92  }