Simulate delay queues with Redis
In fact, it is more convenient to implement delay queues through MQ, but in the actual business, it is because of various reasons that ultimately choose to use redis as the middleware for the business implementation. Let's record it by the way.
This service is used for schedule SMS reminders. After the user adds the schedule, he will be placed in the redis queue and wait for the execution of sending SMS reminders.
This article describes how to use Redis to implement a simple delay task queue. This example can help you understand how to use Redis's ordered collection features to manage and execute delay tasks.
Design ideas
Redis Ordered Sets can be used well to implement the function of delay queues. Execution is triggered by storing the execution time of the task as a score into an ordered set, and regularly checking tasks less than or equal to the current time in the set.
Code implementation
JedisCluster connection initialization
First, we need to initialize the JedisCluster connection to interact with the Redis cluster.
private static final String ZSET_KEY = "sms_delayed_tasks"; private JedisCluster jedisCluster; public void RedisClusterScheduler() { Set<HostAndPort> nodes = new HashSet<>(); //Read the redis cluster configuration from the configuration file for (String node : ("").split(",")) { String[] hostPort = (":"); (new HostAndPort(hostPort[0], (hostPort[1]))); } GenericObjectPoolConfig<Jedis> poolConfig = new GenericObjectPoolConfig<>(); (128); (128); (16); jedisCluster = new JedisCluster(nodes, 2000, 2000, 5, (""), poolConfig); if (!isCalled) { isCalled = true; startTaskChecker(); } }
Add delay tasks
We can add delayed tasks by specifying tasks and execution time. This method converts execution time to timestamps and stores tasks in Redis ordered collection.
public void addDelayedTask(String task, String time) { long executeTime = convertToTimestamp(time); if (executeTime > () / 1000) { (ZSET_KEY, executeTime, task); ("Add Task: " + task + "Execution time: " + executeTime); } else { ("The task time must be after the current time: " + task); } } private long convertToTimestamp(String time) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { return (time).getTime() / 1000; } catch (ParseException e) { (); return () / 1000; } }
Check and execute tasks
Continuously check the tasks before the current time and execute them through a timed task.
private void startTaskChecker() { (() -> { while (!().isInterrupted()) { try { checkAndExecuteTasks(); (1000); } catch (Exception e) { (new Date() + "An exception occurs but does not interrupt, the exception is:" + e); } } }); } private void checkAndExecuteTasks() { long currentTime = () / 1000; Set<String> tasks = (ZSET_KEY, 0, currentTime); for (String task : tasks) { (ZSET_KEY, task); executeTask(task); } }
Logic of executing tasks
Assuming that the task content is a JSON object, the execution logic can be any operation here, such as calling an external service, sending a message, etc.
private void executeTask(String taskJson) { JSONObject task = (taskJson); // Add specific business logic here ("Execute Task: " + task); }
Summarize
Through Redis's ordered collection and simple timer, a simple and effective delay task queue can be implemented.
Of course, this example is a simplified model. In a production environment, you need to consider issues such as idempotence of tasks, recovery strategies after system crashes, and priority of tasks. I hope this article can provide you with ideas and references to implement delay queues.
This is the article about Redis simulation delay queue implementation schedule reminders. For more related Redis delay queue content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!