All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.redisson.reactive.RedissonSemaphoreReactive Maven / Gradle / Ivy

/**
 * Copyright 2016 Nikita Koksharov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.redisson.reactive;

import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

import org.reactivestreams.Publisher;
import org.redisson.RedissonLock;
import org.redisson.RedissonSemaphore;
import org.redisson.api.RFuture;
import org.redisson.api.RLockAsync;
import org.redisson.api.RSemaphoreAsync;
import org.redisson.api.RSemaphoreReactive;
import org.redisson.command.CommandAsyncExecutor;
import org.redisson.command.CommandReactiveExecutor;
import org.redisson.pubsub.SemaphorePubSub;

/**
 * 
 * @author Nikita Koksharov
 *
 */
public class RedissonSemaphoreReactive extends RedissonExpirableReactive implements RSemaphoreReactive {

    private final RSemaphoreAsync instance;
    
    public RedissonSemaphoreReactive(CommandReactiveExecutor connectionManager, String name, SemaphorePubSub semaphorePubSub) {
        super(connectionManager, name);
        instance = new RedissonSemaphore(commandExecutor, name, semaphorePubSub);
    }

    protected RLockAsync createLock(CommandAsyncExecutor connectionManager, String name, UUID id) {
        return new RedissonLock(commandExecutor, name, id);
    }

    @Override
    public Publisher tryAcquire() {
        return reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.tryAcquireAsync();
            }
        });
    }

    @Override
    public Publisher tryAcquire(final int permits) {
        return reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.tryAcquireAsync(permits);
            }
        });
    }

    @Override
    public Publisher acquire() {
        return reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.acquireAsync();
            }
        });
    }

    @Override
    public Publisher acquire(final int permits) {
        return reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.acquireAsync(permits);
            }
        });
    }

    @Override
    public Publisher release() {
        return reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.releaseAsync();
            }
        });
    }

    @Override
    public Publisher release(final int permits) {
        return reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.releaseAsync(permits);
            }
        });
    }

    @Override
    public Publisher trySetPermits(final int permits) {
        return reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.trySetPermitsAsync(permits);
            }
        });
    }

    @Override
    public Publisher tryAcquire(final long waitTime, final TimeUnit unit) {
        return reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.tryAcquireAsync(waitTime, unit);
            }
        });
    }

    @Override
    public Publisher tryAcquire(final int permits, final long waitTime, final TimeUnit unit) {
        return reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.tryAcquireAsync(permits, waitTime, unit);
            }
        });
    }

    @Override
    public Publisher reducePermits(final int permits) {
        return reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.reducePermitsAsync(permits);
            }
        });
    }
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy