A method for implementing timeout retry execution based on Rocket MQ
The method leverages RocketMQ to manage timeout retries in financial business systems, improving efficiency and reducing CPU usage while providing a generic solution for asynchronous tasks, addressing inefficiencies in existing methods.
Patent Information
- Application Number
- CN202111538304.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-15
- Publication Date
- 2025-03-14
- Estimated Expiration
- 2041-12-15
AI Technical Summary
Existing methods for handling timeouts in asynchronous tasks in financial business systems are inefficient, consuming excessive CPU resources and lacking generality, as they rely on frequent polling and specific code modifications for retry logic.
Implementing a method using RocketMQ to manage timeout retries by binding task trigger methods with strategies, sending messages with parameters and retry settings, and leveraging RocketMQ's delayed messaging for efficient and generic retry operations.
This approach enhances query efficiency, reduces CPU usage, and provides a generic solution for handling timeouts across various asynchronous tasks by decoupling retry logic from the main code, ensuring high performance and adaptability.
Smart Images

Figure CN114328194B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer software, and in particular to a method for implementing a timeout retry execution based on Rocket MQ. Background Art
[0002] In the financial business system, it is necessary to obtain the relevant business information rules of the enterprise to make risk control decisions. It is necessary to execute the business information rules and display the execution results. Since the logic of executing the business rules is relatively complex and time-consuming, asynchronous processing is required, that is, the task is triggered to the rule engine system, and the rule engine system notifies the business system after execution.
[0003] Because the rule engine system sometimes times out when executing rules and fails to return results (due to high concurrency or a long time for mining some enterprise information or other abnormal situations), the business system needs to retry and trigger the task to the rule engine system again, and a timeout retry mechanism needs to be implemented.
[0004] Of course, there are other similar tasks that require asynchronous processing and may time out.
[0005] The existing technology mainly uses scheduled task polling to query the task that times out and does not return a result, and performs retry trigger processing.
[0006] Disadvantages of existing technology:
[0007] 1. Low efficiency. Tasks that need to query the database for timeouts and do not return results will have low query efficiency when the amount of data is large. 2. It takes up a lot of CPU resources. In order to retry as accurately as possible, the interval of scheduled tasks is short, resulting in frequent execution of scheduled tasks. 3. It is not versatile enough. If other asynchronous tasks also need to retry after timeout, the entire retry logic code needs to be changed.
[0008] Therefore, the prior art has defects and needs to be improved. Summary of the invention
[0009] The technical problem to be solved by the present invention is to provide a method for implementing timeout retry execution based on Rocket MQ, which has high query efficiency, fast execution speed, less CPU resource occupation and versatility.
[0010] The technical solution of the present invention is as follows: a method for implementing a timeout retry execution method based on Rocket MQ, characterized in that it includes the following steps: Step S1: when the application is started, the task triggering methods of all asynchronous tasks that need timeout retries are bound to a corresponding task triggering strategy, and saved in the application cache; Step S2: the parameters of the task triggering method, the task triggering strategy, the time of the timeout retry, and the maximum number of retries are assembled into a task message, and sent to Rocket MQ; and the delay time of the task message is set; Step S3: according to the delay time, Rocket MQ pushes the task message to the application, and the application obtains the corresponding task triggering method saved in the application cache according to the task triggering strategy, matches the parameters of the task triggering method according to the task triggering method, and completes the timeout retry.
[0011] Applied to the above technical solution, in the method for implementing timeout retry execution based on Rocket MQ, in step S3, before matching the parameters of the task triggering method, it is also queried from the database whether the asynchronous task has returned a result. If so, no processing is performed; otherwise, the number of retries is increased by 1; after accumulation, when the current number of retries is equal to or greater than the maximum number of retries, no processing is performed; when the current number of retries is less than the maximum number of retries, the parameters of the task triggering method are matched to complete the timeout retry.
[0012] Applied to the above-mentioned technical solutions, in the method for implementing timeout retry execution based on Rocket MQ, in step S1, the task triggering method is a task triggering method based on reflection acquisition; and in step S3, the reflection mechanism is used to complete the call of the task triggering method to complete the timeout retry.
[0013] Applied to the above-mentioned technical solutions, in the method for implementing timeout retry execution based on Rocket MQ, in step S2, the delay time is the time of the timeout retry.
[0014] Applied to the above-mentioned technical solutions, in the method for implementing timeout retry execution based on Rocket MQ, in step S2, the task triggering strategy, time for timeout retry, and maximum number of retries are all set on the timeout retry annotation; and in step S1, all asynchronous tasks that need timeout retry are asynchronous tasks with timeout retry annotation.
[0015] Applied to the above-mentioned technical solutions, in the method for implementing timeout retry execution based on rocket mq, in step S2, a custom spring aop aspect is written to intercept the timeout retry annotation of asynchronous task timeout retry, and the task triggering strategy, timeout retry time, maximum number of retries, and parameters of the task triggering method on the intercepted timeout retry annotation are assembled into a task message and sent to rocket mq.
[0016] The beneficial effects of the present invention are:
[0017] 1. The problem of low efficiency mentioned above is solved. It only needs to query whether the current timeout task has been completed and the result has been returned. The query efficiency is high and the execution speed is naturally fast.
[0018] 2. Solve the problem of excessive CPU usage mentioned above. There is no need to poll the task status during non-timeout period, thus reducing CPU usage.
[0019] 3. It is universal. For the same type of asynchronous tasks that can be retried with a timeout, you only need to customize a trigger strategy and a corresponding trigger method, and then annotate the corresponding trigger strategy in the method that needs to be retried with a timeout, which follows the opening and closing principle of the design pattern. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] Figure 1 It is an execution flow chart of the present invention. DETAILED DESCRIPTION
[0021] The following is combined with the accompanying drawings and specific implementation example , the present invention is described in detail.
[0022] This embodiment provides a method for implementing a timeout retry execution based on rocket mq. The execution method of this embodiment mainly implements a timeout retry mechanism based on rocket mq, and also uses spring aop and jdk's reflection mechanism.
[0023] The execution method comprises the following steps: Step S1: when the application is started, the task triggering methods of all asynchronous tasks that need timeout and retry are bound to a corresponding task triggering strategy and saved in the application cache.
[0024] Among them, key is the task trigger strategy, value is the method obtained based on reflection, that is, the task trigger method; at startup, all methods with timeout retry annotations are loaded, and the methods and task trigger strategies on the annotations are bound together.
[0025] Then, step S2: assemble the parameters of the task triggering method, the task triggering strategy, the time of the timeout retry, and the maximum number of retries into a task message, and send it to Rocket MQ; and set the delay time of the task message.
[0026] Among them, the delay time is the time of the timeout retry, and the task triggering strategy, time of the timeout retry, and the maximum number of retries are all set on the timeout retry annotation; and, a custom spring aop aspect is written to intercept the timeout retry annotation of the asynchronous task timeout retry, and the task triggering strategy, time of the timeout retry, maximum number of retries, and parameters of the task triggering method on the intercepted timeout retry annotation are assembled into a task message and sent to rocket mq; the parameters of the task triggering method are written and obtained from spring aop; that is, sending the task message to rocketmq is processed in the spring aop aspect and coupled with the business method.
[0027] Finally, step S3: according to the delay time, Rocket MQ pushes the task message to the application. The application obtains the corresponding task trigger method saved in the application cache according to the task trigger strategy, matches the parameters of the task trigger method according to the task trigger method, and completes the timeout retry.
[0028] Among them, before matching the parameters of the task triggering method, it is also necessary to query from the database whether the asynchronous task has returned a result. If so, no processing is performed. Otherwise, the number of retries is increased by 1. After accumulation, when the current number of retries is equal to or greater than the maximum number of retries, no processing is performed. When the current number of retries is less than the maximum number of retries, the parameters of the task triggering method are matched to complete the timeout retry.
[0029] Since in step S1, the task triggering method is a task triggering method based on reflection acquisition; therefore, in step S3, the reflection mechanism is used to complete the call of the task triggering method to complete the timeout retry.
[0030] In this way, if the number of retries is less than the maximum number, the corresponding task will be re-triggered. The message obtained from Rocket MQ contains the parameters of the task triggering method and the task triggering strategy. According to the task triggering strategy, the corresponding triggering method can be obtained, that is, the method in the cache in the first step. After obtaining the reflected method method, the reflection mechanism is used to complete the method call, that is, the retry after the timeout is completed. The method parameters are obtained from Rocket MQ.
[0031] The present invention solves the above-mentioned problem of low efficiency by querying from the database whether the asynchronous task has returned a result before matching the parameters of the task triggering method. It only needs to query whether the current timeout task has been completed and returned a result. The query efficiency is high and the execution speed is naturally fast.
[0032] In addition, the present invention binds a task triggering method to a corresponding task triggering strategy and saves it in the application cache, and assembles the parameters of the task triggering method, the task triggering strategy, the time of the timeout retry, and the maximum number of retries into a task message and sends it to Rocket MQ, thereby solving the above-mentioned problem of excessive CPU usage. There is no need to poll the task status during non-timeout time, thereby reducing CPU usage.
[0033] Moreover, the present invention is universal. For the same type of asynchronous tasks that can be timed out and retried, only a trigger strategy and a corresponding trigger method need to be customized, and then the corresponding trigger strategy can be annotated in the method that needs to be timed out and retried, which follows the opening and closing principle of the design pattern.
[0034] The above are only preferred embodiments of the present invention and are not intended to limit the present invention. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention should be included in the protection scope of the present invention.
Claims
1. A method for implementing timeout retry execution based on Rocket MQ, characterized in that: The steps include: Step S1: When the application is started, the task triggering methods of all asynchronous tasks that need to time out and retry are bound to a corresponding task triggering strategy and saved in the application cache; Step S2: Assemble the parameters of the task trigger method, the task trigger strategy, the time of the timeout retry, and the maximum number of retries into a task message, and send it to Rocket MQ; and set the delay time of the task message; Step S3: Rocket MQ pushes the task message to the application according to the delay time. The application obtains the corresponding task trigger method stored in the application cache according to the task trigger strategy, matches the parameters of the task trigger method according to the task trigger method, and completes the timeout retry; In step S3, before matching the parameters of the task triggering method, it is also checked from the database whether the asynchronous task has returned a result. If so, no processing is performed. If not, the number of retries is increased by 1. After accumulation, when the current number of retries is equal to or greater than the maximum number of retries, no processing is performed. When the current number of retries is less than the maximum number of retries, the parameters of the task triggering method are matched to complete the timeout retry. In step S1, the task triggering method is a task triggering method based on reflection acquisition; and in step S3, the reflection mechanism is used to complete the call of the task triggering method to complete the timeout retry; In step S2, the task triggering strategy, time of timeout retry, and maximum number of retries are all set on the timeout retry annotation; and in step S1, all asynchronous tasks that need timeout retry are asynchronous tasks with timeout retry annotation.
2. According to claim 1, the method for implementing timeout retry execution based on Rocket MQ is characterized in that: In step S2, the delay time is set to the time of the timeout retry.
3. The method for implementing timeout retry execution based on Rocket MQ according to claim 1, characterized in that: In step S2, a custom spring aop aspect is written to intercept the timeout retry annotation of asynchronous task timeout retry, and the task triggering strategy, timeout retry time, maximum number of retries, and parameters of the task triggering method on the intercepted timeout retry annotation are assembled into a task message and sent to rocket mq.
Citation Information
Patent Citations
Message queue system-based delay task triggering method and apparatus
CN107391271A
Automatic retry device and method based on reflection mechanism
CN111367723A
Timeout processing optimization method and storage medium
CN111600738A