How to implement delay queue in Java
The editor will share with you how to implement the delay queue in Java. I hope you will get something after reading this article. Let's discuss it together.
The common implementation methods are: scheduled task scanning, RocketMQ delay queue, Java automatic delay queue, monitoring Redis Key expiration and so on.
1. DelayQueue
First, define a deferred task
Package com.cjs.example;import lombok.Data;import java.util.concurrent.Delayed;import java.util.concurrent.TimeUnit;/** * @ author ChengJianSheng * @ since 2021-3-18 * / @ Datapublic class DelayTask implements Delayed {private Long orderId; private long expireTime; public DelayTask (Long orderId, long expireTime) {this.orderId = orderId; this.expireTime = expireTime;} @ Override public long getDelay (TimeUnit unit) {return expireTime-System.currentTimeMillis () @ Override public int compareTo (Delayed o) {return (int) (getDelay (TimeUnit.MILLISECONDS)-o.getDelay (TimeUnit.MILLISECONDS));}}
Then, define a management class
Package com.cjs.example;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.CommandLineRunner;import org.springframework.stereotype.Component;import java.util.concurrent.DelayQueue;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;/** * @ author ChengJianSheng * @ since 2021-3-19 * / @ Slf4j@Componentpublic class DelayQueueManager implements CommandLineRunner {private DelayQueue queue = new DelayQueue (); @ Autowired private ParkOrderQueryHandler handler; @ Override public void run (String...) Strings) throws Exception {ExecutorService executorService = Executors.newSingleThreadExecutor (); executorService.execute (new Runnable () {@ Override public void run () {while (true) {try {DelayTask task = queue.take (); handler.handle (task);} catch (InterruptedException e) {e.printStackTrace ();});} public void put (DelayTask task) {queue.put (task);}
Insert task
@ Slf4j@Servicepublic class PayServiceImpl implements PayService {@ Autowired private DelayQueueManager delayQueueManager; @ Override public void pay () {delayQueueManager.put (new DelayTask (1,15)); delayQueueManager.put (new DelayTask (2,30)); delayQueueManager.put (new DelayTask (3,60));} 2. Redis Key expiration callback
Modify the redis.conf file
# bind 127.0.0.1 -:: 1
Protected-mode no
Notify-keyspace-events Ex
[root@localhost redis-6.2.1] $src/redis-server redis.conf 4.0.0 org.springframework.boot spring-boot-starter-parent 2.4.4 com.example demo0401 0.0.1-SNAPSHOT demo0401 Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-data-redis org.springframework.boot spring-boot-maven-plugin
RedisConfig.java
Package com.example.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.listener.RedisMessageListenerContainer;/** * @ author ChengJianSheng * @ since 2021-4-2 * / @ Configurationpublic class RedisConfig {@ Bean public RedisMessageListenerContainer container (RedisConnectionFactory connectionFactory) {RedisMessageListenerContainer container = new RedisMessageListenerContainer (); container.setConnectionFactory (connectionFactory); return container;}}
Create a listener class
Package com.example.listener;import org.springframework.data.redis.connection.Message;import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;import org.springframework.data.redis.listener.RedisMessageListenerContainer;import org.springframework.stereotype.Component;/** * @ author ChengJianSheng * @ since 2021-4-2 * / @ Componentpublic class MyRedisKeyExpirationListener extends KeyExpirationEventMessageListener {public MyRedisKeyExpirationListener (RedisMessageListenerContainer listenerContainer) {super (listenerContainer);} @ Override public void onMessage (Message message, byte [] pattern) {String expiredKey = message.toString () System.out.println ("listening to Key:" + expiredKey + "expired");}} after reading this article, I believe you have some understanding of "how to implement delay queue in Java". If you want to know more about it, welcome to follow the industry information channel. Thank you for reading!