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 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          /* Threre is no reasonable way to destroy the semaphore in Redis because we cannot know
71           * when the last process has stopped using it.
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  }