Integrated solution during RabbitMQ exception handling
Patent Information
- Application Number
- CN202410712778.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-03
- Publication Date
- 2024-09-24
AI Technical Summary
[0011]本发明的目的在于提供一种RabbitMQ异常处理时的集成解决方法,从而解决现有技术中存在的前述问题
[0037] The beneficial effects of the present invention are: 1. The present invention abstracts and encapsulates SpringBoot integration and RabbitMq integration, and solves the problem of message sending failure and message consumption failure encountered in the use of RabbitMQ. 2. The present invention can ensure 100% delivery of messages and supports automatic asynchronous retry after monitoring failure. It can also guarantee 100% message consumption and support automatic asynchronous retry after consumption failure. 3. The present invention provides an API to control the processing of queues with failed transmissions and message failures, such as: querying message content, modifying the message body, re-consuming, re-sending, etc. 4. The present invention encapsulates the re-entry of scheduled task processing messages. Solve the problem of repeated consumption on the consumer side.
Smart Images

Figure CN118689667A
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to an integrated solution for RabbitMQ exception processing. Background Art
[0002] MQ plays an important role in business logic. It can play an important role in business decoupling, asynchronous communication, peak buffering, scalability, recoverability, etc. RabbitMQ follows the AMQP protocol, has good commercial and community support, and has significant performance in Erlang development. It supports message persistence, message confirmation mechanism, high reliability, and good web management and monitoring performance.
[0003] RabbitMQ is mainly used in conjunction with the Spring framework. The Spring framework provides a series of configurations and methods for using RabbitMQ, enabling Java services to use RabbitMQ in a more unified and convenient manner. Spring encapsulates the RabbitMQ client to a certain extent, mainly through XML or annotations to configure the message queue, and use RabbitMQ by configuring the message queue name, listener, exchange, and binding the message queue. However, the following problems exist during use:
[0004] (1) When a message fails to be sent, the system either discards the message in an unfriendly manner or records the exception information. The system needs to wait for manual re-posting of the failed message and cannot automatically retry.
[0005] (2) When consuming a message fails, either the exception information is recorded and the failed message is manually republished, which cannot be automatically retried, or the message is written back to the queue and waits for consumption again. If the exception is due to the business logic on the consumer side, the consumption will be retried in an infinite loop, which cannot solve the problem and will affect the consumption of normal messages and business performance due to the failed message.
[0006] (3) Due to network or program abnormalities, duplicate consumption may occur on the consumer side.
[0007] (4) Unable to dynamically manage abnormal messages during operation.
[0008] (5) The message content of the message that failed to produce or failed to consume cannot be modified to meet the consumption conditions.
[0009] For example, there is a logic of sending SMS after a successful order. The query assembly and query of SMS is a time-consuming process, and there is a need to retry after the sending fails. Sending SMS is not a logic that requires high real-time performance. In this way, a message can be sent to RabbitMQ after the order is placed, and the listening queue on the other side will process the logic of sending SMS after receiving the message. However, there are a series of problems in the process of sending messages from the production end to RabbitMQ and the consumer end processing this message task. Messages are lost when sending and consuming messages, and it is impossible to guarantee 100% delivery and 100% consumption of messages; there are problems such as duplicate consumption on the consumer end and a user sending multiple identical messages.
[0010] Therefore, an integrated solution for RabbitMQ exception handling is urgently needed to solve the above problems. Summary of the invention
[0011] The object of the present invention is to provide an integrated solution for RabbitMQ exception handling, thereby solving the above-mentioned problems existing in the prior art.
[0012] In order to achieve the above object, the technical solution adopted by the present invention is as follows:
[0013] An integrated solution for RabbitMQ exception handling, including production-side processing and consumer-side processing;
[0014] The production end processing process includes the following steps:
[0015] S11. When the production service starts, the encapsulated RabbitMQ exception handling component is called to create a client instance connected to the RabbitMQ server;
[0016] S12. When creating an instance, set the RabbitMQ message interface and register a listening method for asynchronous listening, which is used to receive a confirmation notification sent by the RabbitMQ server after receiving a message sent by the production end;
[0017] S13. When there is a message to be sent, the producer calls the provider() production method to send the message to the RabbitMQ server and wait for the consumer to consume it.
[0018] The provider() production method generates a unique message ID when it is called, and registers the unique message ID and the message body content in the redis queue in the form of a key-value pair to cache the message body content in the Redis service; the provider() production method then sends the unique message ID and the message body content to the RabbitMQ server, waiting for consumption by the consumer;
[0019] S14, determine whether the RabbitMQ server has received the message. If the message is received, a confirmation notification with the message unique ID is sent to the production end. After the monitoring method registered by the production end receives the confirmation notification, it deletes the message body content registered in the redis queue according to the message unique ID, indicating that the message is successfully sent to the RabbitMQ server; if the message is not received, the monitoring method registered by the generator end will not receive the confirmation notification, and the message body content registered in the redis queue will not be deleted. If the message body content is still not deleted after the first preset time, it means that the message is not successfully sent to the RabbitMQ server;
[0020] The consumer-side processing process includes the following steps:
[0021] S21. Start the consumer service and register the monitoring method to monitor the message queue of the RabbitMQ server;
[0022] S22, after the RabbitMQ server successfully receives the message from the producer, it sends the message to the consumer;
[0023] S23, after the monitoring method registered by the consumer listens to the message sent by the RabbitMQ server, it calls the consume() consumption method of the AbstractMsgQueueServi ce class to consume the message;
[0024] The consume() method uses the unique ID of the received message to try to obtain a distributed lock in redis. If the distributed lock cannot be obtained, it means that the message has been consumed by another consumer. In order to avoid repeated consumption of messages, it directly exits abnormally to ensure that the same message can only be consumed by one consumer in the same time period. If the distributed lock is obtained successfully, it means that the current consumer has obtained the qualification to consume the message in the current time period, and the custom consumption method will be called to consume the message. After the message consumption is successful, it ends normally.
[0025] Preferably, the production end processing process also includes:
[0026] The background scheduled task periodically queries the message body content registered in the redis queue that has survived for more than the second preset time, and determines that the sending of these message body contents has failed and needs to be retried;
[0027] The message body content of the failed message is saved in the database table message_error_log, and the message is marked as failed to send and not retried. At the same time, the message body content of the message registered in the redis queue is deleted, and the scheduled task is waited for to retry sending the message.
[0028] The background scheduled task jobcenter2 regularly queries the database message_error_log for all messages that failed to be sent and were not retried, calls the provider() production method, and sends the message to the consumer again.
[0029] Preferably, the production end processing process also includes:
[0030] The system retries three times by default. If it still fails after three retries, it is assumed that no retries will be made on the same day. At the same time, DingTalk reminds manual intervention and waits for retry the next day. If the retry is successful, the message status in the message_error_log table is marked as retried. After receiving the failure reminder, the manual login management page can view the relevant information of the failure message and manually trigger a retry.
[0031] Preferably, the consumer-side processing process further includes:
[0032] The content of the message body that failed to be consumed is saved in the database table message_error_log, and the message is marked as failed to be consumed and not retried, waiting for the scheduled task to retry sending the message;
[0033] If consumption fails due to network or other business exceptions, the global exception is captured, the content of the message body of the failed consumption is saved in the database table message_error_log, and the message is marked as failed consumption and not retried, waiting for the scheduled task to retry sending the message;
[0034] The background scheduled task jobcenter3 regularly queries the database message_error_log for all messages that have failed to be consumed and have not been retried, and calls the consume() method to consume the message again.
[0035] Preferably, the consumer-side processing process further includes:
[0036] The system retries three times by default. If it still fails after three retries, it is assumed that no retries will be made on the same day. At the same time, DingTalk reminds manual intervention and waits for another retry the next day. If the retry is successful, the message status in the message_error_log table is marked as retried. After receiving the failure reminder, the manual login management page can view the relevant information of the failure message and manually trigger a retry.
[0037] The beneficial effects of the present invention are: 1. The present invention abstracts and encapsulates the SpringBoot integration with RabbitMQ, solving the problems encountered in the use of RabbitMQ when sending messages and consuming messages. 2. The present invention can ensure 100% message delivery and support automatic asynchronous retry after monitoring of sending failure. It can also ensure 100% message consumption and support automatic asynchronous retry after consumption failure. 3. The present invention provides an API to control the processing of queues with sending failures and message failures, such as querying message content, modifying message body, re-consuming, resending, etc. 4. The present invention encapsulates the re-queuing of scheduled tasks to process messages. The problem of repeated consumption on the consumer side is solved. BRIEF DESCRIPTION OF THE DRAWINGS
[0038] Figure 1 is a diagram of the production end processing process in an embodiment of the present invention;
[0039] Figure 2 is a diagram of the processing process at the consumer end in an embodiment of the present invention;
[0040] Figure 3 is a schematic diagram of RabbitMQ configuration in an embodiment of the present invention;
[0041] Figure 4 It is a functional schematic diagram of the management interface in an embodiment of the present invention. DETAILED DESCRIPTION
[0042] In order to make the purpose, technical solution and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the accompanying drawings. It should be understood that the specific implementation methods described herein are only used to explain the present invention and are not used to limit the present invention.
[0043] like Figure 1 and Figure 2 As shown, in this embodiment, an integrated solution for RabbitMQ exception handling is provided, including a production-side processing process and a consumer-side processing process;
[0044] 1. Production-side processing (see Figure 1 )
[0045] 1. When the production service starts, the encapsulated RabbitMQ exception handling component is called to create a client instance connected to the RabbitMQ server.
[0046] 2. When creating the instance in step 1, set the RabbitMQ message interface confirmation asynchronous listener to receive the confirmation message sent by the RabbitMQ service after receiving the message sent by the producer.
[0047] 3. When there is a message to be sent on the production side, the provider() production method is called to send the message.
[0048] 4. The provider() production method will generate a [message unique id] when called in step 3, and register the [message unique id] and [message body content] in the redis queue as a key-value pair, and cache the message content in the Redis service.
[0049] 5. The provide() production method then sends the [unique id of the message] and [the content of the message body] to the RabbitMQ server, waiting for consumption by the consumer.
[0050] 6. The RabbitMQ service confirms whether the message has been received.
[0051] 7. If the RabbitMQ service receives the message in step 6, it will send a confirmation notification with the [unique message id] to the producer.
[0052] 8. After the monitoring method registered in step 2 of the production end receives the confirmation notification with the [unique message id] issued by the RabbitMQ service in step 7, it deletes the message body content registered in the redis queue in step 4 according to the [unique message id], indicating that the message is successfully sent to the RabbitMQ service.
[0053] 9. If the RabbitMQ service does not receive the message in step 6, the listening method registered by the generator in step 2 will not receive the confirmation notification, and the message body content registered in step 4 will not be deleted. If the message is still not deleted after five minutes (the first preset time), it means that the message has not been successfully sent to the RabbitMQ service.
[0054] 10. The background scheduled task [jobcenter1] regularly queries the message body content registered in the redis queue in step 4 for a survival time of more than five minutes (the second preset time), and then determines that these sending fails and needs to be retried.
[0055] 11. Save the message body content that failed to be sent to the database table [message_error_log], and mark the message as [failed to send] and [not retried] status. At the same time, delete the message body content of the message registered in the redis queue in step 4, and wait for the scheduled task to retry sending the message.
[0056] 12. The background scheduled task [jobcenter2] regularly queries the database [message_error_log] for all messages with [failed to send] and [not retried] status, calls the provider() production method in step 3, and sends the message.
[0057] 13. Repeat steps 1-12.
[0058] 14. The system retries three times by default. If the message still fails after three retries, it is assumed that no retries will be made on the same day. At the same time, DingTalk will remind you to intervene manually and wait for another try the next day. If the retry is successful, the message status in the [message_error_log] table is marked as [Retried].
[0059] 15. After receiving the failure reminder issued in step 14, log in to the management page to view the relevant information of the failure message and manually trigger a retry.
[0060] 2. Consumer-side processing (see Figure 2 )
[0061] 16. Start the consumer service to listen to the RabbitMQ message queue, and the message will be received.
[0062] 17. After RabbitMQ successfully receives the message from the producer in step 8, it sends the message to the consumer.
[0063] 18. The monitoring method in step 16 monitors the message sent in step 17. The monitoring method will call the consume() method of the AbstractMsgQueueServi ce class to consume the message.
[0064] 19. The consume() method uses the received [unique message id] to try to acquire the distributed lock in redis.
[0065] 20. If the acquisition of the distributed lock in step 19 fails, it means that the message has been consumed by another consumer. In order to avoid repeated consumption of messages, the process directly exits abnormally to ensure that the same message can only be consumed by one consumer in the same time period.
[0066] 21. Save the message body content of the failed consumption in step 20 to the database table [message_error_log], and mark the message as [Consumption failed] and [Not retried] status, waiting for the scheduled task to retry step 25 to send the message.
[0067] 22. If the distributed lock is successfully acquired in step 19, it means that the current consumer has obtained the qualification to consume the message in the current time period, and the custom consumption method will be called to consume the message.
[0068] 23. After the message in step 22 is consumed successfully, it ends normally.
[0069] 24. If the consumption fails due to network or other business abnormalities in step 22, the global exception is captured, and the message body content of the consumption failure in step 22 is saved in the database table [message_error_log], and the message is marked as [Consumption Failed] and [Not Retried] status, waiting for the scheduled task to retry step 25 to send the message.
[0070] 25. The background scheduled task [jobcenter3] regularly queries the database [message_error_log] for all messages in the [consumption failed] and [not retried] states, and calls the consume() consumption method in step 18 to consume the message.
[0071] 26. Repeat steps 16-25.
[0072] 27. The system retries three times by default. If the message still fails after three retries, it is assumed that no retries will be made on the same day. At the same time, DingTalk will remind you to intervene manually and wait for another try the next day. If the retry is successful, the message status in the [message_error_log] table will be marked as [Retried].
[0073] 28. After receiving the failure reminder issued in step 27, log in to the management page to view the relevant information of the failure message and manually trigger a retry.
[0074] In this embodiment, see Figure 3 , RabbitMQ includes the following configurations: queue name configuration, switch configuration, routing key configuration, and client monitoring. The switch configuration includes Di rect mode configuration, Topic configuration, and Fanout configuration.
[0075] In this embodiment, a management interface is set up to manage consumption failure messages, such as Figure 4 As shown, the management interface supports querying messages that failed to be sent or consumed; supports modifying the content of abnormal message bodies; supports deleting erroneous or discarded messages, and deleted messages will not be processed again; supports adding new messages; supports manually triggering the sending and consuming of messages; the management interface displays a visual display of all different types of production and consumption failure messages for manual intervention in messages.
[0076] By adopting the above technical solution disclosed in the present invention, the following beneficial effects are obtained:
[0077] The present invention provides an integrated solution for RabbitMQ exception handling. The present invention abstracts and encapsulates SpringBoot integration with RabbitMQ, and solves the problems encountered in the use of RabbitMQ when sending messages and consuming messages fail. The present invention can ensure 100% message delivery and support automatic asynchronous retry after monitoring of sending failure. It can also ensure 100% message consumption and support automatic asynchronous retry after consumption failure. The present invention provides an API to control the processing of queues of failed sending and failed messages, such as querying message content, modifying message body, re-consuming, resending, etc. The present invention encapsulates the re-queuing of scheduled tasks to process messages. The problem of repeated consumption on the consumer side is solved.
[0078] The above is only a preferred embodiment of the present invention. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principle of the present invention. These improvements and modifications should also be considered as the scope of protection of the present invention.
Claims
1. An integrated solution for RabbitMQ exception handling, characterized by: Includes production-side processing and consumer-side processing; The production end processing process includes the following steps: S11. When the production service starts, the encapsulated RabbitMQ exception handling component is called to create a client instance connected to the RabbitMQ server; S12. When creating an instance, set the RabbitMQ message interface and register a listening method for asynchronous listening, which is used to receive a confirmation notification sent by the RabbitMQ server after receiving a message sent by the production end; S13. When there is a message to be sent, the producer calls the provider() production method to send the message to the RabbitMQ server and wait for the consumer to consume it. The provider() production method generates a message unique id when called, and registers the message unique id and message body content in the redis queue in the form of a key-value pair to cache the message body content in the Redis service; the provider() production method then sends the message unique id and message body content to the RabbitMQ server, waiting for consumption by the consumer end; S14. Determine whether the RabbitMQ server has received the message. If it has, send a confirmation notification with the message unique ID to the producer. After receiving the confirmation notification, the monitoring method registered by the producer deletes the message body content registered in the redis queue according to the message unique ID, indicating that the message has been successfully sent to the RabbitMQ server. If the message is not received, the listening method registered by the generator will not receive the confirmation notification, and the message body content registered in the redis queue will not be deleted. If the message body content is still not deleted after the first preset time, it means that it has not been successfully sent to the RabbitMQ server; The consumer-side processing process includes the following steps: S21. Start the consumer service and register the monitoring method to monitor the message queue of the RabbitMQ server; S22, after the RabbitMQ server successfully receives the message from the producer, it sends the message to the consumer; S23, after the monitoring method registered by the consumer listens to the message sent by the RabbitMQ server, it calls the consume() consumption method of the AbstractMsgQueueService class to consume the message; The consume() method uses the unique ID of the received message to try to obtain a distributed lock in redis; if the distributed lock cannot be obtained, it means that the message has been consumed by another consumer. In order to avoid repeated consumption of messages, it directly exits abnormally to ensure that the same message can only be consumed by one consumer in the same time period; if the distributed lock is obtained successfully, it means that the current consumer has obtained the qualification to consume the message in the current time period, and the custom consumption method will be called to consume the message. After the message consumption is successful, it ends normally.
2. The integrated solution for RabbitMQ exception handling according to claim 1, characterized in that: The production side processing also includes, The background scheduled task periodically queries the message body content registered in the redis queue whose survival time exceeds the second preset time, and determines that the sending of these message body contents fails and needs to be retried; The message body content that failed to be sent is saved in the database table message_error_log, and the message is marked as failed to be sent and not retried. At the same time, the message body content of the message registered in the redis queue is deleted, and the scheduled task is waited for to retry sending the message; The background scheduled task jobcenter2 regularly queries the database message_error_log for all messages that failed to be sent and were not retried, calls the provider() production method, and sends the message to the consumer again.
3. The integrated solution for RabbitMQ exception handling according to claim 2, characterized in that: The production side processing also includes, The system retries three times by default. If it still fails after three retries, it is assumed that no retries will be made on the same day. At the same time, DingTalk reminds manual intervention and waits for another retry the next day. If the retry is successful, the message status in the message_error_log table is marked as retried. After receiving the failure reminder, the manual login management page can view the relevant information of the failure message and manually trigger a retry.
4. The integrated solution for RabbitMQ exception handling according to claim 1, characterized in that: The consumer-side processing process also includes: Save the content of the message body that failed to be consumed to the database table message_error_log, and mark the message as failed to be consumed and not retried, waiting for the scheduled task to retry sending the message; If consumption fails due to network or other business exceptions, the global exception is captured, the message body content of the failed consumption is saved in the database table message_error_log, and the message is marked as failed consumption and not retried, waiting for the scheduled task to retry sending the message; The background scheduled task jobcenter3 regularly queries the database message_error_log for all messages that have failed to be consumed and have not been retried, and calls the consume() consumption method to consume the message again.
5. The integrated solution for RabbitMQ exception handling according to claim 4, characterized in that: The consumer-side processing process also includes: The system retries three times by default. If it still fails after three retries, it is assumed that no retries will be made on the same day. At the same time, DingTalk reminds manual intervention and waits for another retry the next day. If the retry is successful, the message status in the message_error_log table is marked as retried. After receiving the failure reminder, the manual login management page can view the relevant information of the failure message and manually trigger a retry.
Citation Information
Cited By
Message queue distribution management system and method based on data monitoring
CN120017604A
