Preface:

Message queues are well understood and are implicitly queued for execution. Redisson queues are similar to implementing Java interfaces such as Queue and BlockingQueue to handle complex business logic based on the Redisson Api.

1.Queue

The RQueue object in Redisson implements the java.util.Queue interface. Queues are used in cases where elements are processed first from the oldest element (also known as FIFO or FIFO). As with normal Java, the first element of the RQueue can be examined using the peek() method, or examined and deleted using the poll() method.

RQueue<SomeObject> queue = redisson.getQueue("anyQueue");// Define a queue
queue.add(new SomeObject());
SomeObject obj = queue.peek(); / / check
SomeObject someObj = queue.poll();/ / valueCopy the code

2.BlockingQueue

The RBlockingQueue object in Redisson implements the java.util.BlockingQueue interface. BlockingQueues are queues that block threads trying to poll from empty queues or trying to insert elements in queues that are already full. The thread is blocked until another thread inserts an element into an empty queue or polls the first element from the full queue.

You can call the poll () method with an argument that specifies how long the thread waits for the element to become available.

RBlockingQueue<SomeObject> queue = redisson.getBlockingQueue("anyQueue");
queue.offer(new SomeObject());
SomeObject obj = queue.peek();
SomeObject someObj = queue.poll();
SomeObject ob = queue.poll(10, TimeUnit.MINUTES); // Call the poll() method with an argument that specifies the thread wait time:Note: The poll(), pollFromAny(), pollLastAndOfferFirstTo(), and take() methods are automatically re-subscribed during failover or reconnection to a Redis server.Copy the code

3.BoundedBlockingQueue

The RBoundedBlockingQueue object in Redisson implements the bounded blocking queue structure. Bounded blocking queues have limited capacity.

The following code demonstrates how to instantiate and use the RBoundedBlockingQueue in Redisson. The trySetCapacity () method is used. :

RBoundedBlockingQueue<SomeObject> queue = redisson.getBoundedBlockingQueue("anyQueue");
queue.trySetCapacity(2); TrySetCapacity () returns: Boolean
queue.offer(new SomeObject(1));
queue.offer(new SomeObject(2));
// will be blocked until there is free space in the queue
queue.put(new SomeObject());
SomeObject obj = queue.peek();
SomeObject someObj = queue.poll();
SomeObject ob = queue.poll(10, TimeUnit.MINUTES);

//Copy the code

4.DelayedQueue

The RDelayedQueue object in Redisson allows you to implement delay queuing in Redis. Using strategies such as exponential fallback to deliver messages to consumers, the time between retries increases exponentially after each failed attempt to deliver the message.

RQueue<String> destinationQueue = redisson.getQueue("anyQueue");
RDelayedQueue<String> delayedQueue = getDelayedQueue(destinationQueue);
// Move the object to DestinationQueue within 10 seconds
delayedQueue.offer("msg1".10, TimeUnit.SECONDS);
// Move the object to the DestinationQueue within 1 minute
delayedQueue.offer("msg2".1, TimeUnit.MINUTES);

// Each element in the delay queue will be transmitted to the destination queue after the delay specified with the element. This target queue can be any queue that implements the RQueue interface, such as RBlockingQueue or RBoundedBlockingQueue.Copy the code

5.PriorityQueue

The RPriorityQueue object in Redisson implements the java.util.Queue interface. Priority queues are queues that are not sorted by the age of an element, but by the priority associated with each element.

RPriorityQueue<Integer> queue = redisson.getPriorityQueue("anyQueue");
queue.trySetComparator(new MyComparator()); // Use the Comparator to sort the elements in the queue:
queue.add(3);
queue.add(1);
queue.add(2);
queue.removeAsync(0);
queue.addAsync(5);
queue.poll();Copy the code

6.PriorityBlockingQueue

The RPriorityBlockingQueue object in Redisson combines the functionality of RPriorityQueue and RBlockingQueue. Like RPriorityQueue, RPriorityBlockingQueue uses comparators to sort the elements in the queue.

RPriorityBlockingQueue<Integer> queue = redisson.getPriorityBlockingQueue("anyQueue");
queue.trySetComparator(new MyComparator()); // Set the comparison object
queue.add(3);
queue.add(1);
queue.add(2);
queue.removeAsync(0);
queue.addAsync(5);
queue.take();

// Automatically re-subscribe to POL (), pollLastAndOfferFirstTo() and take() during failover or reconnection to Redis serverCopy the code

Select the corresponding message queue according to your business scenario.

For more redis, follow the column:Blog.csdn.net/u011663149/…