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 final ConcurrentMap<String, RSemaphore> semaphores; 042 043 public RedissonSemaphoreNamedLockFactory() 044 { 045 this.semaphores = new ConcurrentHashMap<>(); 046 } 047 048 @Override 049 protected AdaptedSemaphoreNamedLock createLock( final String name ) 050 { 051 RSemaphore semaphore = semaphores.computeIfAbsent( name, k -> 052 { 053 RSemaphore result = redissonClient.getSemaphore( NAME_PREFIX + k ); 054 result.trySetPermits( Integer.MAX_VALUE ); 055 return result; 056 } ); 057 return new AdaptedSemaphoreNamedLock( name, this, new RedissonSemaphore( semaphore ) ); 058 } 059 060 @Override 061 protected void destroyLock( final String name ) 062 { 063 RSemaphore semaphore = semaphores.remove( name ); 064 if ( semaphore == null ) 065 { 066 throw new IllegalStateException( "Semaphore expected but does not exist: " + name ); 067 } 068 semaphore.delete(); 069 } 070 071 private static final class RedissonSemaphore implements AdaptedSemaphoreNamedLock.AdaptedSemaphore 072 { 073 private final RSemaphore semaphore; 074 075 private RedissonSemaphore( final RSemaphore semaphore ) 076 { 077 this.semaphore = semaphore; 078 } 079 080 @Override 081 public boolean tryAcquire( final int perms, final long time, final TimeUnit unit ) throws InterruptedException 082 { 083 return semaphore.tryAcquire( perms, time, unit ); 084 } 085 086 @Override 087 public void release( final int perms ) 088 { 089 semaphore.release( perms ); 090 } 091 } 092}