org.redisson.RedissonBoundedBlockingQueue Maven / Gradle / Ivy
/**
* Copyright 2018 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;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.redisson.api.RBoundedBlockingQueue;
import org.redisson.api.RFuture;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.Codec;
import org.redisson.client.codec.LongCodec;
import org.redisson.client.protocol.RedisCommand;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.command.CommandExecutor;
import org.redisson.connection.decoder.ListDrainToDecoder;
import org.redisson.misc.RPromise;
import org.redisson.misc.RedissonPromise;
import org.redisson.pubsub.SemaphorePubSub;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener;
/**
* Distributed and concurrent implementation of bounded {@link java.util.concurrent.BlockingQueue}.
*
* @author Nikita Koksharov
*/
public class RedissonBoundedBlockingQueue extends RedissonQueue implements RBoundedBlockingQueue {
private final CommandExecutor commandExecutor;
private final SemaphorePubSub semaphorePubSub;
protected RedissonBoundedBlockingQueue(SemaphorePubSub semaphorePubSub, CommandExecutor commandExecutor, String name, RedissonClient redisson) {
super(commandExecutor, name, redisson);
this.semaphorePubSub = semaphorePubSub;
this.commandExecutor = commandExecutor;
}
protected RedissonBoundedBlockingQueue(SemaphorePubSub semaphorePubSub, Codec codec, CommandExecutor commandExecutor, String name, RedissonClient redisson) {
super(codec, commandExecutor, name, redisson);
this.semaphorePubSub = semaphorePubSub;
this.commandExecutor = commandExecutor;
}
private String getSemaphoreName() {
if (getName().contains("{")) {
return "redisson_bqs:" + getName();
}
return "redisson_bqs:{" + getName() + "}";
}
@Override
public RFuture addAsync(V e) {
final RPromise result = new RedissonPromise();
RFuture future = offerAsync(e);
future.addListener(new FutureListener() {
@Override
public void operationComplete(Future future) throws Exception {
if (!future.isSuccess()) {
result.tryFailure(future.cause());
return;
}
if (!future.getNow()) {
result.tryFailure(new IllegalStateException("Queue is full"));
return;
}
result.trySuccess(future.getNow());
}
});
return result;
}
@Override
public RFuture putAsync(V e) {
RedissonQueueSemaphore semaphore = createSemaphore(e);
return semaphore.acquireAsync();
}
private RedissonQueueSemaphore createSemaphore(V e) {
RedissonQueueSemaphore semaphore = new RedissonQueueSemaphore(commandExecutor, getSemaphoreName(), semaphorePubSub);
semaphore.setQueueName(getName());
semaphore.setValue(e);
return semaphore;
}
@Override
public void put(V e) throws InterruptedException {
RedissonQueueSemaphore semaphore = createSemaphore(e);
semaphore.acquire();
}
@Override
public RFuture offerAsync(V e) {
RedissonQueueSemaphore semaphore = createSemaphore(e);
return semaphore.tryAcquireAsync();
}
@Override
public boolean offer(V e, long timeout, TimeUnit unit) throws InterruptedException {
RedissonQueueSemaphore semaphore = createSemaphore(e);
return semaphore.tryAcquire(timeout, unit);
}
@Override
public RFuture offerAsync(V e, long timeout, TimeUnit unit) {
RedissonQueueSemaphore semaphore = createSemaphore(e);
return semaphore.tryAcquireAsync(timeout, unit);
}
@Override
public RFuture takeAsync() {
RFuture takeFuture = commandExecutor.writeAsync(getName(), codec, RedisCommands.BLPOP_VALUE, getName(), 0);
return wrapTakeFuture(takeFuture);
}
private RPromise wrapTakeFuture(final RFuture takeFuture) {
final RPromise result = new RedissonPromise() {
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
super.cancel(mayInterruptIfRunning);
return takeFuture.cancel(mayInterruptIfRunning);
};
};
takeFuture.addListener(new FutureListener() {
@Override
public void operationComplete(Future future) throws Exception {
if (!future.isSuccess()) {
result.tryFailure(future.cause());
return;
}
if (future.getNow() == null) {
result.trySuccess(takeFuture.getNow());
return;
}
createSemaphore(null).releaseAsync().addListener(new FutureListener() {
@Override
public void operationComplete(Future future) throws Exception {
result.trySuccess(takeFuture.getNow());
}
});
}
});
return result;
}
@Override
public RFuture removeAsync(Object o) {
return removeAllAsync(Collections.singleton(o));
}
@Override
public RFuture removeAllAsync(Collection> c) {
if (c.isEmpty()) {
return RedissonPromise.newSucceededFuture(false);
}
String channelName = RedissonSemaphore.getChannelName(getSemaphoreName());
return commandExecutor.evalWriteAsync(getName(), codec, RedisCommands.EVAL_BOOLEAN,
"local count = 0; " +
"for i = 1, #ARGV, 1 do "
+ "if redis.call('lrem', KEYS[1], 0, ARGV[i]) == 1 then "
+ "count = count + 1; "
+ "end; "
+"end; "
+ "if count > 0 then "
+ "local value = redis.call('incrby', KEYS[2], count); "
+ "redis.call('publish', KEYS[3], value); "
+ "return 1;"
+ "end;"
+ "return 0 ",
Arrays.