com.github.lontime.shaded.org.redisson.RedissonBlockingQueue 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.RBlockingQueue;
import com.github.lontime.shaded.org.redisson.api.RFuture;
import com.github.lontime.shaded.org.redisson.api.RedissonClient;
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 java.time.Duration;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
/**
* Distributed and concurrent implementation of {@link java.util.concurrent.BlockingQueue}.
*
*
Queue size limited by Redis server memory amount. This is why {@link #remainingCapacity()} always
* returns Integer.MAX_VALUE
*
* @author [email protected]
* @author Nikita Koksharov
*/
public class RedissonBlockingQueue extends RedissonQueue implements RBlockingQueue {
public RedissonBlockingQueue(CommandAsyncExecutor commandExecutor, String name, RedissonClient redisson) {
super(commandExecutor, name, redisson);
}
public RedissonBlockingQueue(Codec codec, CommandAsyncExecutor commandExecutor, String name, RedissonClient redisson) {
super(codec, commandExecutor, name, redisson);
}
public RedissonBlockingQueue(Codec codec, CommandAsyncExecutor commandExecutor, String name) {
super(codec, commandExecutor, name, null);
this.name = name;
}
@Override
public RFuture putAsync(V e) {
return addAsync(e, RedisCommands.RPUSH_VOID);
}
/*
* (non-Javadoc)
* @see java.util.concurrent.BlockingQueue#put(java.lang.Object)
*/
@Override
public void put(V e) throws InterruptedException {
get(putAsync(e));
}
@Override
public boolean offer(V e, long timeout, TimeUnit unit) throws InterruptedException {
return offer(e);
}
@Override
public RFuture takeAsync() {
return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.BLPOP_VALUE, getRawName(), 0);
}
/*
* (non-Javadoc)
* @see java.util.concurrent.BlockingQueue#take()
*/
@Override
public V take() throws InterruptedException {
return commandExecutor.getInterrupted(takeAsync());
}
@Override
public RFuture pollAsync(long timeout, TimeUnit unit) {
return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.BLPOP_VALUE, getRawName(), toSeconds(timeout, unit));
}
/*
* (non-Javadoc)
* @see java.util.concurrent.BlockingQueue#poll(long, java.util.concurrent.TimeUnit)
*/
@Override
public V poll(long timeout, TimeUnit unit) throws InterruptedException {
return commandExecutor.getInterrupted(pollAsync(timeout, unit));
}
/*
* (non-Javadoc)
* @see com.github.lontime.shaded.org.redisson.core.RBlockingQueue#pollFromAny(long, java.util.concurrent.TimeUnit, java.lang.String[])
*/
@Override
public V pollFromAny(long timeout, TimeUnit unit, String... queueNames) throws InterruptedException {
return commandExecutor.getInterrupted(pollFromAnyAsync(timeout, unit, queueNames));
}
/*
* (non-Javadoc)
* @see com.github.lontime.shaded.org.redisson.core.RBlockingQueueAsync#pollFromAnyAsync(long, java.util.concurrent.TimeUnit, java.lang.String[])
*/
@Override
public RFuture pollFromAnyAsync(long timeout, TimeUnit unit, String... queueNames) {
return commandExecutor.pollFromAnyAsync(getRawName(), codec, RedisCommands.BLPOP_VALUE,
toSeconds(timeout, unit), queueNames);
}
@Override
public Map> pollFirstFromAny(Duration duration, int count, String... queueNames) throws InterruptedException {
return commandExecutor.getInterrupted(pollFirstFromAnyAsync(duration, count, queueNames));
}
@Override
public RFuture