A method and system for automatic reconnection of message queues
By building custom notification rules and using the LockSupport tool, the message queue can be automatically reconnected, which solves the connection interruption problem caused by network outages or maintenance, and ensures that downstream systems continue to consume messages and refresh the cache.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- BAIRONG ZHIXIN (BEIJING) TECH CO LTD
- Filing Date
- 2021-11-11
- Publication Date
- 2026-06-30
Smart Images

Figure CN113934558B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of artificial intelligence, and in particular to a method and system for automatic reconnection of message queues. Background Technology
[0002] Message queues are widely used in the development of various systems. They are used when one system needs to notify another system. They interact by sending messages, thereby decoupling upstream and downstream systems. The upstream system does not need to know the specific interface and protocol of the downstream system, nor does it need to know the specific network address, topology, etc. of the downstream system. It only needs to send notification messages to the message queue. The downstream system also does not need to know the specific information of the upstream system. It only needs to consume the messages in the message queue.
[0003] After the upstream system starts up, it establishes a connection with the message queue server. Similarly, after the downstream system starts up, it also establishes a connection with the message queue server. In existing technology, when the upstream system sends a message, the message queue selects one or more downstream consumers based on its existing routing strategy and then sends the message.
[0004] In the process of implementing the inventive technical solutions in the embodiments of this application, the inventors of this application discovered that the above-mentioned technology has at least the following technical problems:
[0005] Existing technologies present a technical problem where downstream system consumers and message queues may experience prolonged or permanent connection interruptions due to rare network outages or maintenance. Summary of the Invention
[0006] In view of this, this application provides a method and system for automatic reconnection of a message queue. The method includes: obtaining a first connection state of a first message queue; constructing a custom notification rule based on the first connection state; obtaining a first notification instruction based on the custom notification rule; notifying a daemon thread to start looping execution using the LockSupport tool according to the first notification instruction; creating message queue connections cyclically through the daemon thread to obtain a first creation result, wherein the first creation result includes a first result and a second result, the first result being a successful connection and the second result being a failed connection; if the first creation result is the first result, monitoring the daemon thread using the LockSupport tool to obtain a second connection state. This solves the technical problem in the prior art where downstream system consumers and message queues experience prolonged or permanent connection interruptions due to rare network outages or maintenance. It achieves the technical effect that when a message queue experiences a disconnection due to network instability or maintenance, the system issues a notification and automatically attempts to re-establish the connection, enabling downstream system consumers to automatically reconnect to the message queue and continue consuming messages and processing business logic.
[0007] In view of the above problems, embodiments of this application provide a method and system for automatic reconnection of message queues.
[0008] In a first aspect, this application provides a method for automatic reconnection of a message queue. The method is implemented through an automatic reconnection system for a message queue, comprising: obtaining a first connection state of a first message queue; constructing a custom notification rule based on the first connection state; obtaining a first notification instruction based on the custom notification rule; notifying a daemon thread to start looping execution using the LockSupport tool according to the first notification instruction; creating message queue connections cyclically through the daemon thread to obtain a first creation result, wherein the first creation result includes a first result and a second result, the first result being a successful connection and the second result being a failed connection; if the first creation result is the first result, monitoring the daemon thread using the LockSupport tool to obtain a second connection state.
[0009] On the other hand, this application also provides a message queue automatic reconnection system for executing a message queue automatic reconnection method as described in the first aspect, wherein the system includes: a first obtaining unit, the first obtaining unit being used to obtain a first connection state of a first message queue; a first building unit, the first building unit being used to build a custom notification rule based on the first connection state; a second obtaining unit, the second obtaining unit being used to obtain a first notification instruction based on the custom notification rule; a first running unit, the first running unit being used to use the LockSupport tool to notify a daemon thread to start looping execution according to the first notification instruction; a third obtaining unit, the third obtaining unit being used to create message queue connections cyclically through the daemon thread to obtain a first creation result, wherein the first creation result includes a first result and a second result, the first result being a successful connection and the second result being a failed connection; and a fourth obtaining unit, the fourth obtaining unit being used to use the LockSupport tool to monitor the daemon thread and obtain a second connection state if the first creation result is the first result.
[0010] Thirdly, embodiments of this application also provide an automatic message queue reconnection system, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to implement the steps of the method described in the first aspect above.
[0011] One or more technical solutions provided in the embodiments of this application have at least the following technical effects or advantages:
[0012] 1. By obtaining the first connection state of the first message queue; constructing a custom notification rule based on the first connection state; obtaining a first notification instruction based on the custom notification rule; using the LockSupport tool to notify the daemon thread to start looping according to the first notification instruction; and using the daemon thread to create message queue connections in a loop to obtain a first creation result, wherein the first creation result includes a first result and a second result, where the first result is a successful connection and the second result is a failed connection; if the first creation result is the first result, the LockSupport tool is used to monitor the daemon thread to obtain a second connection state. This achieves the technical effect that when the message queue is disconnected due to network instability or maintenance, the system issues a notification and automatically attempts to re-establish the connection, enabling downstream system consumers to automatically reconnect to the message queue and continue consuming messages and processing business logic.
[0013] 2. Through message queues and the automatic reconnection mechanism of message queues, the local memory cache is automatically refreshed when the message queue connection is interrupted.
[0014] The above description is only an overview of the technical solution of this application. In order to better understand the technical means of this application and to implement it in accordance with the contents of the specification, and to make the above and other objects, features and advantages of this application more obvious and understandable, the following are specific embodiments of this application. Attached Figure Description
[0015] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are merely exemplary. For those skilled in the art, other drawings can be obtained based on the provided drawings without creative effort.
[0016] Figure 1 This is a flowchart illustrating an automatic message queue reconnection method according to an embodiment of this application;
[0017] Figure 2 This is a flowchart illustrating the method for constructing custom notification rules in an automatic message queue reconnection method according to an embodiment of this application.
[0018] Figure 3 This is a flowchart illustrating the invocation of a custom notification rule method in an automatic message queue reconnection method according to an embodiment of this application.
[0019] Figure 4 This is a flowchart illustrating a method for obtaining a second connection state in an automatic message queue reconnection method according to an embodiment of this application.
[0020] Figure 5 This is a schematic diagram of the structure of an automatic message queue reconnection system according to an embodiment of this application;
[0021] Figure 6 This is a schematic diagram of the structure of an exemplary electronic device according to an embodiment of this application.
[0022] Explanation of reference numerals in the attached figures:
[0023] First acquisition unit 11, first construction unit 12, second acquisition unit 13, first running unit 14, third acquisition unit 15, fourth acquisition unit 16, bus 300, receiver 301, processor 302, transmitter 303, memory 304, bus interface 305. Detailed Implementation
[0024] This application provides a method and system for automatic reconnection of message queues, solving the technical problem in the prior art where downstream system consumers and message queues experience prolonged or permanent connection interruptions due to rare network outages or maintenance. It achieves the technical effect of automatically reconnecting the downstream system consumer to the message queue after a disconnection caused by network instability or maintenance, issuing a notification, and automatically attempting to re-establish the connection, allowing the downstream system consumer to continue consuming messages and processing business logic.
[0025] The technical solutions in the embodiments of this application will now be clearly and completely described with reference to the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of this application, and not all of them. It should be understood that this application is not limited to the exemplary embodiments described herein. All other embodiments obtained by those skilled in the art based on the embodiments of this application without creative effort are within the scope of protection of this application. It should also be noted that, for ease of description, only the parts related to this application are shown in the accompanying drawings, not all of them.
[0026] Application Overview
[0027] Message queues are widely used in the development of various systems. They are used when one system needs to notify another system. They interact by sending messages, thereby decoupling upstream and downstream systems. The upstream system does not need to know the specific interface and protocol of the downstream system, nor does it need to know the specific network address, topology, etc. of the downstream system. It only needs to send notification messages to the message queue. The downstream system also does not need to know the specific information of the upstream system. It only needs to consume the messages in the message queue.
[0028] After the upstream system starts up, it establishes a connection with the message queue server. Similarly, after the downstream system starts up, it also establishes a connection with the message queue server. In existing technology, when the upstream system sends a message, the message queue selects one or more downstream consumers based on its existing routing strategy and then sends the message.
[0029] Existing technologies present a technical problem where downstream system consumers and message queues may experience prolonged or permanent connection interruptions due to rare network outages or maintenance.
[0030] To address the aforementioned technical problems, the overall approach of the technical solution provided in this application is as follows:
[0031] This application provides a method for automatic message queue reconnection, applied to a message queue automatic reconnection system. The method includes: obtaining a first connection state of a first message queue; constructing a custom notification rule based on the first connection state; obtaining a first notification instruction based on the custom notification rule; notifying a daemon thread to start looping execution using the LockSupport tool according to the first notification instruction; creating message queue connections cyclically through the daemon thread to obtain a first creation result, wherein the first creation result includes a first result and a second result, where the first result indicates a successful connection and the second result indicates a failed connection; if the first creation result is the first result, monitoring the daemon thread using the LockSupport tool to obtain a second connection state.
[0032] After introducing the basic principles of this application, various non-limiting embodiments of this application will be described in detail below with reference to the accompanying drawings.
[0033] Example 1
[0034] Please see the appendix Figure 1 This application provides a method for automatic message queue reconnection, wherein the method is applied to a message queue automatic reconnection system, and the method specifically includes the following steps:
[0035] Step S100: Obtain the first connection status of the first message queue;
[0036] Step S200: Based on the first connection state, construct a custom notification rule;
[0037] Specifically, the message queue refers to a container that stores messages during transmission, used for communication between different systems. Its main function is to provide routing and ensure message delivery. If the receiver is unavailable when a message is sent, the message queue will retain the message until it can be successfully delivered. The automatic reconnection method for the message queue can automatically re-establish a connection when the connection between the downstream system consumer and the message queue is broken. The first message queue refers to any message queue issued by the upstream system. The first connection state refers to the connection state between the first message queue and the downstream system consumer. Based on the first connection state between the first message queue and the downstream system consumer, the system issues a corresponding custom notification, which is the custom notification rule. By constructing notification rules for the system under different connection states through the connection state of the message queue, the technical effect of automatically notifying the message queue of its real-time connection status is achieved.
[0038] Step S300: Obtain the first notification instruction based on the custom notification rule;
[0039] Step S400: According to the first notification instruction, use the LockSupport tool to notify the daemon thread to start running in a loop;
[0040] Specifically, based on the connection status between the first message queue and the downstream system consumer, and in conjunction with the custom notification rules, the system automatically issues a first notification instruction. This first notification instruction is the notification information corresponding to the current connection status between the first message queue and the downstream system consumer. The `LockSupport` tool is a flexible thread utility class that allows threads to block and be woken up at any point, primarily including two methods: `park` and `unpark`. A daemon thread refers to a thread that provides a general service in the background during program execution. This thread is not an indispensable part of the program; all daemon threads terminate when all non-daemon threads end. An example is a garbage collection thread. By cyclically running daemon threads, the technical effect of re-creating the connection between the message queue and the downstream system consumer is achieved.
[0041] Step S500: The daemon thread creates message queue connections in a loop to obtain a first creation result, wherein the first creation result includes a first result and a second result, the first result being a successful connection and the second result being a failed connection;
[0042] Specifically, the daemon thread cyclically creates connections between the message queue and the downstream system consumer. The result of the daemon thread's execution is the first creation result. This first creation result includes a first result and a second result: the first result indicates a successful connection between the message queue and the downstream system consumer, while the second result indicates an unsuccessful connection. By running the daemon thread, the technical effect of re-creating the connection between the message queue and the downstream system consumer is achieved.
[0043] Step S600: If the first creation result is the first result, use the LockSupport tool to monitor the daemon thread and obtain the second connection status.
[0044] Specifically, if the creation of the daemon thread results in a successful connection between the message queue and the downstream system consumer, the LockSupport tool is used to monitor the daemon thread until the connection between the message queue and the downstream system consumer is broken again. At this point, the connection status between the message queue and the downstream system consumer is the second connection status. By using the LockSupport tool to monitor the daemon thread, the technical effect of obtaining the corresponding connection break status immediately when the connection between the message queue and the downstream system consumer is broken is achieved.
[0045] Further details are attached. Figure 2 As shown, step S200 in this embodiment further includes:
[0046] Step S210: Construct the first notification framework and the first notification rules using the ApplicationListener technology of the SPRING framework;
[0047] Step S220: Determine whether the first message queue is in a connection interruption state based on the first connection status;
[0048] Step S230: When the first message queue is in a connection interruption state, construct the custom notification rule according to the first notification framework and the first notification rule.
[0049] Specifically, the SPRING framework is an open-source framework created to address the complexity of enterprise application development. The ApplicationListener technology is used to listen for specific events. Through this interface, a generic event is passed in, and the `run` method can listen for this event and perform further logic. In this embodiment, the ApplicationListener technology of the SPRING framework is used to monitor the connection status between the message queue client and the downstream system consumer, and corresponding logic is executed according to a pre-defined first notification framework and rules based on different listening results.
[0050] Based on the first connection status, it is determined whether the connection between the first message queue and the downstream system consumer is interrupted. When the connection between the first message queue and the downstream system consumer is interrupted, according to the first notification framework and the first notification rule, the ApplicationListener uses the unpark method in the LockSupport tool to notify the daemon thread to start running. Here, the unpark method refers to starting and waking up the thread.
[0051] By using ApplicationListener technology to monitor the connection status of the message queue, when the connection is interrupted, the unpark method in the LockSupport tool is used to notify the daemon thread to start running in a timely manner, and re-establish the connection between the message queue client and the downstream system consumer, thus achieving the technical effect of automatically reconnecting the message queue.
[0052] Furthermore, step S230 in this embodiment of the application also includes:
[0053] Step S231: When the first message queue is in a connection interruption state, obtain the first accumulation instruction;
[0054] Step S232: Obtain the first connection interruption time of the first message queue according to the first accumulation instruction;
[0055] Step S233: Determine whether the first connection interruption time exceeds a preset time limit threshold;
[0056] Step S234: When the first connection interruption time exceeds the preset time limit threshold, construct the custom notification rule.
[0057] Specifically, when the first message queue is in a connection interruption state, the system automatically starts calculating the interruption time. When the interruption time exceeds the preset time limit threshold, the system notifies the daemon thread to start running using the `unpark` method in the `LockSupport` tool, according to a custom notification rule. The custom notification rule means that when the first message queue is in a connection interruption state, the system automatically accumulates the interruption time, and once the interruption time exceeds the preset time threshold, the daemon thread is started running in a loop. The preset time limit threshold refers to the maximum time the message queue can be interrupted due to some unexpected event. By calculating the message queue interruption time, the system only starts the daemon thread when the interruption time reaches a certain value.
[0058] Furthermore, step S400 in this embodiment of the application also includes:
[0059] Step S410: The notification to the daemon thread to start running in a loop using the LockSupport tool is performed by the ApplicationListener using the unpark function of the LockSupport tool to resume the thread.
[0060] Step S420: The step of using the LockSupport tool to monitor the daemon thread is to use the LockSupport tool's park function to pause the thread for monitoring the ApplicationListener.
[0061] Specifically, the notification to start the daemon thread to run in a loop using the LockSupport tool is achieved by the ApplicationListener using the `unpark` function of the LockSupport tool to resume the thread. The monitoring of the daemon thread using the LockSupport tool is achieved by the ApplicationListener using the `park` function of the LockSupport tool to pause the thread. Here, `unpark` starts the thread to wake it up, and `park` stops the blocked thread. By using the `park` and `unpark` functions in the LockSupport tool, the technical effect of monitoring the daemon thread's running status and automatically restarting the daemon thread to re-establish the connection when the message queue connection is interrupted is achieved.
[0062] Furthermore, step S220 in this embodiment of the application also includes:
[0063] Step S221: Obtain the first routing policy based on business communication requirements;
[0064] Step S222: Obtain the connection consumption port of the first message queue according to the first routing strategy;
[0065] Step S223: Determine whether the first message queue is in a connection interruption state based on the connection status of the connection consumption port.
[0066] Specifically, a first routing strategy is obtained based on business communication requirements. This routing strategy is a technique that modifies routing information to change the path taken by network traffic. It is a more flexible packet routing and forwarding mechanism than routing based on the target network, mainly achieved by changing routing attributes. Based on the first routing strategy, the connection consumption port of the first message queue is obtained. Furthermore, the connection status of the connection consumption port is used to determine whether the first message queue is in a connection interruption state. This achieves the technical effect of determining the connection status of the message queue through the status of its connection consumption port.
[0067] Further details are attached. Figure 3 As shown, step S700 in this embodiment further includes:
[0068] Step S710: Obtain the first local cache data of the virtual machine;
[0069] Step S720: Determine whether the first local cache data needs to be refreshed;
[0070] Step S730: If the first local cache data needs to be refreshed, obtain the first call instruction;
[0071] Step S740: Invoke the custom notification rule according to the first invocation instruction to complete the refresh.
[0072] Specifically, the system first obtains the virtual machine's local cache data (the first local cache data) when the message queue is interrupted. It then determines whether the first local cache data needs to be refreshed. If it does, the system automatically obtains a first invocation instruction and invokes the custom notification rule according to this instruction to refresh the virtual machine's local cache data. This refresh of the virtual machine's local cache data is automatically achieved when the message queue is interrupted through the message queue and automatic reconnection mechanism.
[0073] Further details are attached. Figure 4 As shown, step S600 in this embodiment further includes:
[0074] Step S610: Based on the second connection status, determine whether the second message queue is in a connection interruption state;
[0075] Step S620: When the second message queue is in a connection interruption state, complete the autonomous reconnection and obtain the second accumulation instruction, wherein the second accumulation instruction is used to accumulate the number of interruptions within the period;
[0076] Step S630: Obtain the first interrupt index according to the second accumulation instruction;
[0077] Step S640: Obtain the first reminder information based on the first interruption index.
[0078] Specifically, the second message queue refers to the message queue after the thread is resumed using the `unpark` method in the LockSupport tool. The second connection state refers to the connection status between the message queue monitored by the LockSupport tool's daemon thread and the downstream system consumer. If the second message queue is in a connection interruption state at this time, it will automatically reconnect and obtain a second accumulation instruction, which is used to accumulate the number of interruptions within the period. Based on the second accumulation instruction, a first interruption index is obtained, and further based on the first interruption index, a first reminder message is obtained. This achieves the technical effect of automatically judging the connection status of the message queue, accumulating the interruption index, and issuing reminder messages.
[0079] In summary, the embodiments of this application provide the following technical effects:
[0080] 1. By obtaining the first connection state of the first message queue; constructing a custom notification rule based on the first connection state; obtaining a first notification instruction based on the custom notification rule; using the LockSupport tool to notify the daemon thread to start looping according to the first notification instruction; and using the daemon thread to create message queue connections in a loop to obtain a first creation result, wherein the first creation result includes a first result and a second result, where the first result is a successful connection and the second result is a failed connection; if the first creation result is the first result, the LockSupport tool is used to monitor the daemon thread to obtain a second connection state. This achieves the technical effect that when the message queue is disconnected due to network instability or maintenance, the system issues a notification and automatically attempts to re-establish the connection, enabling downstream system consumers to automatically reconnect to the message queue and continue consuming messages and processing business logic.
[0081] 2. Through message queues and the automatic reconnection mechanism of message queues, the local memory cache is automatically refreshed when the message queue connection is interrupted.
[0082] Example 2
[0083] Based on the same inventive concept as the message queue automatic reconnection method described in the foregoing embodiments, this invention also provides a message queue automatic reconnection system. Please refer to the appendix. Figure 5 The system includes:
[0084] First obtaining unit 11, the first obtaining unit 11 is used to obtain the first connection state of the first message queue;
[0085] The first construction unit 12 is configured to construct a custom notification rule based on the first connection state.
[0086] The second obtaining unit 13 is used to obtain the first notification instruction based on the custom notification rule;
[0087] The first running unit 14 is configured to use the LockSupport tool to notify the daemon thread to start running in a loop according to the first notification instruction.
[0088] The third obtaining unit 15 is used to create message queue connections in a loop through the daemon thread and obtain a first creation result. The first creation result includes a first result and a second result, where the first result is a successful connection and the second result is an unsuccessful connection.
[0089] The fourth obtaining unit 16 is used to monitor the daemon thread using the LockSupport tool and obtain the second connection state if the first creation result is the first result.
[0090] Furthermore, the system also includes:
[0091] The second building unit is used to build the first notification framework and the first notification rules using the ApplicationListener technology of the SPRING framework;
[0092] The first judgment unit is used to determine whether the first message queue is in a connection interruption state based on the first connection state.
[0093] The third construction unit is used to construct the custom notification rule according to the first notification framework and the first notification rule when the first message queue is in a connection interruption state.
[0094] Furthermore, the system also includes:
[0095] The fifth obtaining unit is used to obtain the first accumulation instruction when the first message queue is in a connection interruption state;
[0096] The sixth obtaining unit is used to obtain the first connection interruption time of the first message queue according to the first accumulation instruction;
[0097] The second judgment unit is used to determine whether the first connection interruption time exceeds a preset time limit threshold.
[0098] The fourth building unit is used to build the custom notification rule when the first connection interruption time exceeds the preset time limit threshold.
[0099] Furthermore, the system also includes:
[0100] The first setting unit is used to notify the daemon thread to start running in a loop using the LockSupport tool, and to notify the ApplicationListener to use the unpark recovery thread method of the LockSupport tool.
[0101] The second setting unit is used to monitor the daemon thread using the LockSupport tool by using the LockSupport tool's park function to pause the thread for the ApplicationListener.
[0102] Furthermore, the system also includes:
[0103] The seventh obtaining unit is used to obtain the first routing strategy according to the business communication requirements;
[0104] The eighth obtaining unit is used to obtain the connection consumption port of the first message queue according to the first routing strategy;
[0105] The third judgment unit is used to determine whether the first message queue is in a connection interruption state based on the connection status of the connection consumption port.
[0106] Furthermore, the system also includes:
[0107] The ninth obtaining unit is used to obtain the first local cache data of the virtual machine;
[0108] The fourth determination unit is used to determine whether the first local cache data needs to be refreshed;
[0109] The tenth obtaining unit is used to obtain a first calling instruction if the first local cache data needs to be refreshed;
[0110] The first invocation unit is used to invoke the custom notification rule to complete the refresh according to the first invocation instruction.
[0111] Furthermore, the system also includes:
[0112] The fifth judgment unit is used to determine whether the second message queue is in a connection interruption state based on the second connection state.
[0113] The eleventh obtaining unit is used to complete autonomous reconnection when the second message queue is in a connection interruption state and obtain a second accumulation instruction, wherein the second accumulation instruction is used to accumulate the number of interruptions within the period;
[0114] The twelfth obtaining unit is used to obtain the first interrupt index according to the second accumulation instruction;
[0115] The thirteenth obtaining unit is used to obtain the first reminder information based on the first interruption index.
[0116] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on its differences from other embodiments. Figure 1 The specific examples and instances in Embodiment 1 are also applicable to this embodiment. Through the foregoing detailed description, those skilled in the art can clearly understand this embodiment; therefore, for the sake of brevity, further details are omitted here. As for the apparatus disclosed in the embodiments, since it corresponds to the method disclosed in the embodiments, the description is relatively simple; relevant details can be found in the method section.
[0117] The above description of the disclosed embodiments enables those skilled in the art to make or use this application. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of this application. Therefore, this application is not to be limited to the embodiments shown herein, but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.
[0118] Exemplary electronic devices
[0119] The following is for reference. Figure 6 The present application describes the electronic device according to its embodiments.
[0120] Figure 6 The figure shows a schematic diagram of the structure of an electronic device according to an embodiment of the present application.
[0121] Based on the inventive concept of the automatic message queue reconnection method in the foregoing embodiments, the present invention also provides an automatic message queue reconnection system, on which a computer program is stored, which, when executed by a processor, implements the steps of any of the methods of the automatic message queue reconnection method described above.
[0122] Among them, Figure 6 In this document, a bus architecture (represented by bus 300) is used. Bus 300 may include any number of interconnected buses and bridges, linking various circuits including one or more processors represented by processor 302 and memory represented by memory 304. Bus 300 may also link various other circuits such as peripheral devices, voltage regulators, and power management circuits, which are well known in the art and therefore will not be described further herein. Bus interface 305 provides an interface between bus 300 and receiver 301 and transmitter 303. Receiver 301 and transmitter 303 may be the same element, i.e., a transceiver, providing a unit for communicating with various other devices over a transmission medium.
[0123] The processor 302 is responsible for managing the bus 300 and general processing, while the memory 304 can be used to store the data used by the processor 302 when performing operations.
[0124] This application provides a method for automatic message queue reconnection, applied to a message queue automatic reconnection system. The method includes: obtaining a first connection state of a first message queue; constructing a custom notification rule based on the first connection state; obtaining a first notification instruction based on the custom notification rule; notifying a daemon thread to start looping execution using the LockSupport tool according to the first notification instruction; creating message queue connections cyclically through the daemon thread to obtain a first creation result, wherein the first creation result includes a first result and a second result, where the first result indicates a successful connection and the second result indicates a failed connection; if the first creation result is the first result, monitoring the daemon thread using the LockSupport tool to obtain a second connection state. This solves the technical problem in the prior art where downstream system consumers and message queues experience prolonged or permanent connection interruptions due to rare network outages or maintenance. It achieves the technical effect that when a message queue experiences a disconnection due to network instability or maintenance, the system issues a notification and automatically attempts to re-establish the connection, enabling downstream system consumers to automatically reconnect to the message queue and continue consuming messages and processing business logic.
[0125] Those skilled in the art will understand that embodiments of this application can be provided as methods, apparatus, or computer program products. Therefore, this application can take the form of a completely software embodiment, a completely hardware embodiment, or an embodiment combining software and hardware aspects. Furthermore, this application is in the form of a computer program product that can be implemented on one or more computer-usable storage media containing computer-usable program code. Such computer-usable storage media include, but are not limited to, various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), disk storage, compact disc read-only memory (CD-ROM), and optical storage.
[0126] This invention is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the invention. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart illustrations and / or block diagrams. Figure 1 One or more processes and / or boxes Figure 1 A system that specifies functions in one or more boxes.
[0127] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including an instruction set implemented in a process. Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.
[0128] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1The steps of the functions specified in one or more boxes. Although preferred embodiments of the invention have been described, those skilled in the art, upon learning the basic inventive concept, can make other changes and modifications to these embodiments. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments as well as all changes and modifications falling within the scope of the invention.
[0129] Obviously, those skilled in the art can make various modifications and variations to this invention without departing from its spirit and scope. Therefore, if these modifications and variations fall within the scope of the claims of this invention and their equivalents, this invention also intends to include these modifications and variations.
Claims
1. A message queue automatic reconnection method, wherein, The method includes: Obtain the first connection status of the first message queue; Based on the first connection state, construct a custom notification rule corresponding to the first connection state; Based on the custom notification rules, a first notification instruction is obtained; According to the first notification instruction, the LockSupport tool is used to notify the daemon thread to start running in a loop. The notification of the daemon thread to start running in a loop using the LockSupport tool means that the ApplicationListener uses the unpark method of the LockSupport tool to notify the daemon thread to start running in a loop. The daemon thread creates message queue connections in a loop to obtain a first creation result, wherein the first creation result includes a first result and a second result, the first result being a successful connection and the second result being a failed connection. If the first creation result is the first result, use the LockSupport tool to monitor the daemon thread and obtain the second connection state. The monitoring of the daemon thread using the LockSupport tool is to use the Park method of the LockSupport tool to monitor the ApplicationListener. The custom notification rule is also used to detect local cached data of the virtual machine when the message queue connection is interrupted, and when it is determined that it needs to be refreshed, to trigger the established custom notification rule with a first call instruction so as to realize the cached data refresh operation.
2. The method of claim 1, wherein, The method of constructing a custom notification rule based on the first connection state further includes: The first notification framework and first notification rules are constructed using the ApplicationListener technology of the SPRING framework. Determine whether the first message queue is in a connection interruption state based on the first connection status; When the first message queue is in a connection interruption state, the custom notification rule is constructed according to the first notification framework and the first notification rule.
3. The method as described in claim 2, wherein, The method for determining whether the first message queue is in a connection interruption state based on the first connection state further includes: When the first message queue is in a connection interruption state, the first accumulation instruction is obtained; According to the first cumulative instruction, the first connection interruption time of the first message queue is obtained; Determine whether the first connection interruption time exceeds a preset time limit threshold; When the first connection interruption time exceeds the preset time limit threshold, the custom notification rule is constructed.
4. The method of claim 2, wherein, The method for determining whether the first message queue is in a connection interruption state based on the first connection state further includes: The first routing strategy is obtained based on business communication requirements; According to the first routing strategy, obtain the connection consumption port of the first message queue; The connection status of the consumer port is used to determine whether the first message queue is in a connection interruption state.
5. The method of claim 1, further comprising: Obtain the first local cached data of the virtual machine; Determine whether the first local cache data needs to be refreshed; If the first local cache data needs to be refreshed, obtain the first invocation instruction; The refresh is completed by invoking the custom notification rule according to the first invocation instruction.
6. The method of claim 1, wherein, If the first creation result is the first result, the daemon thread is monitored according to the park's thread pausing method to obtain the second connection state. The method further includes: Based on the second connection status, determine whether the second message queue is in a connection interruption state; When the second message queue is in a connection interruption state, it completes autonomous reconnection and obtains a second accumulation instruction, wherein the second accumulation instruction is used to accumulate the number of interruptions within the period; According to the second cumulative instruction, the first interrupt index is obtained; Based on the first interruption index, a first reminder message is obtained.
7. A message queue automatic reconnection system, wherein, The system includes: The first obtaining unit is used to obtain the first connection state of the first message queue; The first construction unit is configured to construct a custom notification rule corresponding to the first connection state based on the first connection state. The second obtaining unit is used to obtain the first notification instruction based on the custom notification rule; The first running unit is configured to notify the daemon thread to start running in a loop using the LockSupport tool according to the first notification instruction. The notification to the daemon thread to start running in a loop using the LockSupport tool is performed by the ApplicationListener using the unpark method of the LockSupport tool to resume the thread. The third obtaining unit is used to create message queue connections in a loop through the daemon thread and obtain a first creation result. The first creation result includes a first result and a second result, where the first result is a successful connection and the second result is an unsuccessful connection. The fourth obtaining unit is used to monitor the daemon thread using the LockSupport tool and obtain the second connection state if the first creation result is the first result. The monitoring of the daemon thread using the LockSupport tool is to use the park function of the LockSupport tool to monitor the ApplicationListener. The custom notification rule is also used to detect local cached data of the virtual machine when the message queue connection is interrupted, and when it is determined that it needs to be refreshed, to trigger the established custom notification rule with a first call instruction so as to realize the cached data refresh operation.
8. A message queue automatic reconnection electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein, When the processor executes the program, it implements the steps of the method according to any one of claims 1 to 6.