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