A more elegant implementation of delayed execution---circular queue

When doing business, we often encounter some one-time delay requirements, such as the regular release of news, after a time threshold has passed, the status of a task is set to timeout or a certain interface is triggered.

There are basically three ways to realize this one-time delay requirement before.

(1) Thread polling scan library

(2) Timer class

(3) Timing frame Quartz

All three options have their own problems

For "thread polling and scanning library", the performance overhead is huge and the real-time performance is poor

For the "Timer class", if the project is restarted, the previously set timing will be gone.

For "timed framework Quartz", although DBstore is supported, the framework is too heavyweight.

How to elegantly and efficiently implement a delayed task? I saw an article  https://mp.weixin.qq.com/s/eDMV25YqCPYjxQG-dvqSqQ not long ago , which seems to provide a good solution.

 

 

According to the idea, I implemented a ring queue by myself, https://git.oschina.net/spjich/RingQueue.git

Quoting an image from the article:

 

 

 

This article only proposes a solution, but there are several problems that are not explained

 

1. This timing is similar to the state machine in the memory, and the memory will inevitably be restarted and cleared. Solution: You need to enter the task into the DB, restart the project to construct the circular queue, implement the initData method of AbstractRingQueue, and reload the unfinished tasks in the DB to run

 

2. How to ensure normal operation in a large-scale cluster environment? Solution: This is very critical. Under cluster conditions, if the project is restarted, a task will be executed n times. The solution I think of at this time is to initialize data according to certain rules. The numbers of the three machines are 1, 2, and 3, respectively, so each machine loads the task of taskId%3-machine number=0 when initData. Of course, the solution I proposed is not the best. It also has hot issues, which will cause more tasks to run on a certain machine than on other machines. For general solutions, you can refer to some load balancing strategies.

 

 

In addition, correct the sentence that "open source MQ does not seem to support delayed messages" at the end of the article. As far as I know, Ali's RocketMQ supports delayed messages.

 

 

For original article reprint, please declare the source: http://spjich.iteye.com/blog

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326395645&siteId=291194637