com.github.lontime.shaded.org.redisson.RedissonPriorityBlockingQueue Maven / Gradle / Ivy
/**
* Copyright (c) 2013-2021 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 com.github.lontime.shaded.org.redisson;
import com.github.lontime.shaded.org.redisson.api.RFuture;
import com.github.lontime.shaded.org.redisson.api.RPriorityBlockingQueue;
import com.github.lontime.shaded.org.redisson.api.RedissonClient;
import com.github.lontime.shaded.org.redisson.client.RedisConnectionException;
import com.github.lontime.shaded.org.redisson.client.codec.Codec;
import com.github.lontime.shaded.org.redisson.client.protocol.RedisCommand;
import com.github.lontime.shaded.org.redisson.client.protocol.RedisCommands;
import com.github.lontime.shaded.org.redisson.command.CommandAsyncExecutor;
import com.github.lontime.shaded.org.redisson.connection.decoder.ListDrainToDecoder;
import com.github.lontime.shaded.org.redisson.misc.CompletableFutureWrapper;
import java.time.Duration;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
/**
* Distributed and concurrent implementation of {@link java.util.concurrent.PriorityBlockingQueue}.
*
*
Queue size limited by Redis server memory amount. This is why {@link #remainingCapacity()} always
* returns Integer.MAX_VALUE
*
* @author Nikita Koksharov
*/
public class RedissonPriorityBlockingQueue extends RedissonPriorityQueue implements RPriorityBlockingQueue {
protected RedissonPriorityBlockingQueue(CommandAsyncExecutor commandExecutor, String name, RedissonClient redisson) {
super(commandExecutor, name, redisson);
}
protected RedissonPriorityBlockingQueue(Codec codec, CommandAsyncExecutor commandExecutor, String name, RedissonClient redisson) {
super(codec, commandExecutor, name, redisson);
}
@Override
public void put(V e) throws InterruptedException {
add(e);
}
@Override
public boolean offer(V e, long timeout, TimeUnit unit) throws InterruptedException {
return offer(e);
}
@Override
public RFuture takeAsync() {
CompletableFuture result = new CompletableFuture();
takeAsync(result, 0, 0, RedisCommands.LPOP, getRawName());
return new CompletableFutureWrapper<>(result);
}
protected void takeAsync(CompletableFuture result, long delay, long timeoutInMicro, RedisCommand command, Object... params) {
long start = System.currentTimeMillis();
commandExecutor.getConnectionManager().getGroup().schedule(() -> {
RFuture future = wrapLockedAsync(command, params);
future.whenComplete((res, e) -> {
if (e != null && !(e instanceof RedisConnectionException)) {
result.completeExceptionally(e);
return;
}
if (res != null) {
result.complete(res);
return;
}
if (result.isCancelled()) {
return;
}
long remain = 0;
if (timeoutInMicro > 0) {
remain = timeoutInMicro - ((System.currentTimeMillis() - start))*1000;
if (remain <= 0) {
result.complete(null);
return;
}
}
long del = ThreadLocalRandom.current().nextInt(2000000);
if (timeoutInMicro > 0 && remain < 2000000) {
del = 0;
}
takeAsync(result, del, remain, command, params);
});
}, delay, TimeUnit.MICROSECONDS);
}
@Override
public V take() throws InterruptedException {
return commandExecutor.getInterrupted(takeAsync());
}
public RFuture pollAsync(long timeout, TimeUnit unit) {
CompletableFuture result = new CompletableFuture();
takeAsync(result, 0, unit.toMicros(timeout), RedisCommands.LPOP, getRawName());
return new CompletableFutureWrapper<>(result);
}
@Override
public V poll(long timeout, TimeUnit unit) throws InterruptedException {
return commandExecutor.getInterrupted(pollAsync(timeout, unit));
}
@Override
public V pollFromAny(long timeout, TimeUnit unit, String... queueNames) throws InterruptedException {
throw new UnsupportedOperationException("use poll method");
}
@Override
public Map> pollFirstFromAny(Duration duration, int count, String... queueNames) throws InterruptedException {
throw new UnsupportedOperationException("use poll method");
}
@Override
public Map> pollLastFromAny(Duration duration, int count, String... queueNames) throws InterruptedException {
throw new UnsupportedOperationException("use poll method");
}
@Override
public RFuture