001package org.eclipse.aether.named.redisson; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.eclipse.aether.named.support.AdaptedSemaphoreNamedLock; 023import org.redisson.api.RSemaphore; 024 025import javax.inject.Named; 026import javax.inject.Singleton; 027import java.util.concurrent.ConcurrentHashMap; 028import java.util.concurrent.ConcurrentMap; 029import java.util.concurrent.TimeUnit; 030 031/** 032 * Provider of {@link RedissonSemaphoreNamedLockFactory} using Redisson and {@link org.redisson.api.RSemaphore}. 033 */ 034@Singleton 035@Named( RedissonSemaphoreNamedLockFactory.NAME ) 036public class RedissonSemaphoreNamedLockFactory 037 extends RedissonNamedLockFactorySupport 038{ 039 public static final String NAME = "semaphore-redisson"; 040 041 private static final String TYPED_NAME_PREFIX = NAME_PREFIX + NAME + ":"; 042 043 private final ConcurrentMap<String, RSemaphore> semaphores; 044 045 public RedissonSemaphoreNamedLockFactory() 046 { 047 this.semaphores = new ConcurrentHashMap<>(); 048 } 049 050 @Override 051 protected AdaptedSemaphoreNamedLock createLock( final String name ) 052 { 053 RSemaphore semaphore = semaphores.computeIfAbsent( name, k -> 054 { 055 RSemaphore result = redissonClient.getSemaphore( TYPED_NAME_PREFIX + k ); 056 result.trySetPermits( Integer.MAX_VALUE ); 057 return result; 058 } ); 059 return new AdaptedSemaphoreNamedLock( name, this, new RedissonSemaphore( semaphore ) ); 060 } 061 062 @Override 063 protected void destroyLock( final String name ) 064 { 065 RSemaphore semaphore = semaphores.remove( name ); 066 if ( semaphore == null ) 067 { 068 throw new IllegalStateException( "Semaphore expected but does not exist: " + name ); 069 } 070 /* Threre is no reasonable way to destroy the semaphore in Redis because we cannot know 071 * when the last process has stopped using it. 072 */ 073 } 074 075 private static final class RedissonSemaphore implements AdaptedSemaphoreNamedLock.AdaptedSemaphore 076 { 077 private final RSemaphore semaphore; 078 079 private RedissonSemaphore( final RSemaphore semaphore ) 080 { 081 this.semaphore = semaphore; 082 } 083 084 @Override 085 public boolean tryAcquire( final int perms, final long time, final TimeUnit unit ) throws InterruptedException 086 { 087 return semaphore.tryAcquire( perms, time, unit ); 088 } 089 090 @Override 091 public void release( final int perms ) 092 { 093 semaphore.release( perms ); 094 } 095 } 096}