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

org.redisson.RedissonBlockingQueue 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.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.redisson.api.RBlockingQueue;
import org.redisson.api.RFuture;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.Codec;
import org.redisson.client.protocol.RedisCommand;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.command.CommandAsyncExecutor;
import org.redisson.connection.decoder.ListDrainToDecoder;

/**
 * 

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 { protected RedissonBlockingQueue(CommandAsyncExecutor commandExecutor, String name, RedissonClient redisson) { super(commandExecutor, name, redisson); } protected RedissonBlockingQueue(Codec codec, CommandAsyncExecutor commandExecutor, String name, RedissonClient redisson) { super(codec, commandExecutor, name, redisson); } @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(getName(), codec, RedisCommands.BLPOP_VALUE, getName(), 0); } /* * (non-Javadoc) * @see java.util.concurrent.BlockingQueue#take() */ @Override public V take() throws InterruptedException { return get(takeAsync()); } @Override public RFuture pollAsync(long timeout, TimeUnit unit) { return commandExecutor.writeAsync(getName(), codec, RedisCommands.BLPOP_VALUE, getName(), 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 get(pollAsync(timeout, unit)); } /* * (non-Javadoc) * @see 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 get(pollFromAnyAsync(timeout, unit, queueNames)); } /* * (non-Javadoc) * @see org.redisson.core.RBlockingQueueAsync#pollFromAnyAsync(long, java.util.concurrent.TimeUnit, java.lang.String[]) */ @Override public RFuture pollFromAnyAsync(long timeout, TimeUnit unit, String ... queueNames) { List params = new ArrayList(queueNames.length + 1); params.add(getName()); for (Object name : queueNames) { params.add(name); } params.add(toSeconds(timeout, unit)); return commandExecutor.writeAsync(getName(), codec, RedisCommands.BLPOP_VALUE, params.toArray()); } @Override public RFuture pollLastAndOfferFirstToAsync(String queueName, long timeout, TimeUnit unit) { return commandExecutor.writeAsync(getName(), codec, RedisCommands.BRPOPLPUSH, getName(), queueName, toSeconds(timeout, unit)); } @Override public V pollLastAndOfferFirstTo(String queueName, long timeout, TimeUnit unit) throws InterruptedException { return get(pollLastAndOfferFirstToAsync(queueName, timeout, unit)); } @Override public V takeLastAndOfferFirstTo(String queueName) throws InterruptedException { return get(takeLastAndOfferFirstToAsync(queueName)); } @Override public RFuture takeLastAndOfferFirstToAsync(String queueName) { return pollLastAndOfferFirstToAsync(queueName, 0, TimeUnit.SECONDS); } @Override public int remainingCapacity() { return Integer.MAX_VALUE; } @Override public int drainTo(Collection c) { return get(drainToAsync(c)); } @Override public RFuture drainToAsync(Collection c) { if (c == null) { throw new NullPointerException(); } return commandExecutor.evalWriteAsync(getName(), codec, new RedisCommand("EVAL", new ListDrainToDecoder(c)), "local vals = redis.call('lrange', KEYS[1], 0, -1); " + "redis.call('ltrim', KEYS[1], -1, 0); " + "return vals", Collections.singletonList(getName())); } @Override public int drainTo(Collection c, int maxElements) { if (maxElements <= 0) { return 0; } return get(drainToAsync(c, maxElements)); } @Override public RFuture drainToAsync(Collection c, int maxElements) { if (c == null) { throw new NullPointerException(); } return commandExecutor.evalWriteAsync(getName(), codec, new RedisCommand("EVAL", new ListDrainToDecoder(c)), "local elemNum = math.min(ARGV[1], redis.call('llen', KEYS[1])) - 1;" + "local vals = redis.call('lrange', KEYS[1], 0, elemNum); " + "redis.call('ltrim', KEYS[1], elemNum + 1, -1); " + "return vals", Collections.singletonList(getName()), maxElements); } }