A distributed delay scheduling method and device based on TCP long connection
By adopting the TCP long connection communication and time round algorithm of the Netty framework in a distributed environment, combined with the database lock mechanism, the task repetitive triggering and performance overhead problems are solved, real-time task triggering and efficient communication are realized, API registration is supported, and high security is provided. Sex and fault tolerance.
Patent Information
- Application Number
- CN202310085950.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-02-08
- Publication Date
- 2024-11-22
- Estimated Expiration
- 2043-02-08
AI Technical Summary
The existing technology has the problem of repeated task triggering in a distributed environment. As the number of tasks increases, performance overhead increases, and delayed tasks cannot be triggered in real time, and it is impossible to support the application-side simple and efficient API method to register delayed tasks.
TCP long connection communication based on Netty framework is adopted, task registration and execution is carried out through the API interface between the scheduling server and the scheduling executor, combining the time round algorithm and database lock mechanism to realize task orchestration and execution, and support heartbeat detection and API tasks. register.
It solves the problem of repeated task triggering and performance overhead, realizes real-time task triggering, supports API registration, has efficient, secure and scalable communication protocols, and has fault tolerance and high availability in distributed environments.
Smart Images

Figure CN116155970B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a distributed delay scheduling method and device based on TCP long connection. Background Art
[0002] With the wave of digital transformation of commercial banks, banking business is becoming more and more online and automated. In business scenarios such as online transactions and marketing activities of banks, such business demands are often encountered. For example, the operation staff creates a marketing activity for a certain business, which has a start and end time. After the end time of the activity is reached, the activity expiration operation must be triggered on time to avoid the status not being changed in time after the activity expires. For example, in the deposit business, if a user deposits a one-year time deposit, the principal and interest payment operation must be automatically triggered after 365 days to avoid user complaints caused by failure to issue on time. There are many such scenarios. In essence, they are all triggered after a certain operation, and another operation is triggered after a delay, and it must be triggered on time.
[0003] For the above business scenarios, the industry currently has the following solutions:
[0004] Solution 1) Use JDK's own scheduling tool to start a scheduled task locally in the application system and trigger it when the specified time arrives. However, this method has the problem of repeated task triggering when the application is deployed on multiple nodes in a distributed environment. As the number of tasks increases, the pressure on local memory and CPU increases, the performance overhead increases sharply, and even affects other normal businesses.
[0005] Solution 2) Use open source scheduling solutions, such as Quartz and xxl-job, to periodically poll the expired data and trigger the corresponding logic. However, the most obvious disadvantage of the timed polling solution is that there is a certain degree of delay and delayed tasks cannot be triggered in real time. Moreover, the Quartz framework does not have the distributed feature. Although xxl-job supports distribution, it only supports sending scheduling instructions in plain text HTTP. There is room for further improvement in communication real-time, security, and cluster node fault tolerance.
[0006] In addition, neither Solution 1 nor Solution 2 supports the application side (client) to directly register delayed tasks in a concise and efficient API manner, and neither can directly meet the current business needs within the bank. Summary of the invention
[0007] In view of the above problems, the present invention provides a distributed delay scheduling method and device based on TCP long connection, which solves the problem of repeated task triggering in the existing technical solution when multi-node deployment is applied, and the performance overhead increases sharply as the number of tasks increases, and there are delays, and delayed tasks cannot be triggered in real time.
[0008] To solve the above technical problems, the technical solution adopted by the present invention is: a distributed delay scheduling method based on TCP long connection, comprising the following steps: establishing TCP long connection communication between a scheduling server and a scheduling executor based on the Netty framework; registering an API application with the scheduling server according to the local configuration address and the client jar package, and the scheduling server saves the application node information in a relational database; registering an API task according to the task registration interface provided by the scheduling server, executing the task registration logic, and writing the task information into the database; obtaining tasks to be executed from the database, and scheduling tasks based on the time wheel algorithm; scheduling clusters based on the lock mechanism of the relational database; receiving a task execution request pushed by the scheduling server, triggering task execution after decoding, and notifying the scheduling server of the task execution result; receiving the task execution result, and updating the task execution log in the database.
[0009] As a preferred solution, the network packet of the TCP long connection communication is divided into three parts: a fixed header, an extended header and a packet body. The fixed header includes a 2-byte SNB identifier, a 2-byte header length and a 4-byte packet body length. The extended header is used to store metadata. The packet body is the business data of the request or response, which is a serialized JSON string.
[0010] As a preferred solution, the TCP long connection communication also includes: configuring the heartbeat interval, the scheduling executor sends a heartbeat packet to the scheduling server, and if the sending fails, triggering the reconnection mechanism, re-establishing a TCP connection with the scheduling server to achieve network fault tolerance.
[0011] As a preferred solution, the API application is registered with the scheduling server according to the local configuration address and the client jar package, including: the scheduling executor integrates the client jar package, and when the node is started, it automatically registers with the scheduling server according to the local configuration address to establish a TCP long connection; calling the application registration interface appRegister of the scheduling server to register application information; the scheduling server receives the application registration request message from the scheduling executor, decodes the request method name in the extended packet header of the application registration request message, that is, the apiName field value, and if the apiName field value is appRegister, the API application is registered.
[0012] As a preferred solution, the API task registration is performed according to the task registration interface provided by the scheduling server, including: connecting to the task registration interface taskRegister provided by the scheduling server; the scheduling server receives the task registration request message from the scheduling executor, decodes the request method name in the extended packet header of the task registration request message, that is, the apiName field value, and if the apiName field value is taskRegister, API task registration is performed.
[0013] As a preferred solution, the task scheduling based on the time wheel algorithm includes: obtaining the tasks to be executed from the database, and calculating the time scales of three levels: month, week, and day according to the next trigger time of the task; adding the tasks to the three levels of time wheels according to the time wheels to which they belong; the three time wheels run continuously at their own time scales at the same time, and when the monthly time wheel and the weekly time wheel run to the specified scale, the tasks under the scale are taken out and sent to the sky wheel, and the sky wheel takes over the tasks; when the sky wheel runs to this time point, all tasks under this time point are taken out and triggered for execution.
[0014] As a preferred solution, the locking mechanism based on the relational database is used for cluster scheduling and orchestration, including: locking each scheduling server node that queries the relational database for scheduling tasks to be executed; the scheduling server node that obtains the lock obtains the task list; according to the task list, the tasks to be executed are placed in the time wheel and wait for execution; the next execution time of the task is updated and written to the database; the scheduling server commits the database transaction, releases the database lock it holds, and enters the next cycle.
[0015] As a preferred solution, the receiving of the task execution request pushed by the scheduling server and triggering the task execution after decoding include: receiving the task execution request message pushed by the scheduling server, decoding it with a decoder to obtain the task push information, wherein the task push information includes the class method and parameters; triggering the method call by using the local Java reflection mechanism, or obtaining the Bean instance method in the container based on the Spring framework to trigger the task execution.
[0016] The present invention also discloses a distributed delay scheduling device based on TCP long connection, comprising: a long connection module, which establishes TCP long connection communication between a scheduling server and a scheduling executor based on a Netty framework; an application registration module, which is used to register an API application with the scheduling server according to a local configuration address and a client jar package, and the scheduling server stores application node information in a relational database; a task registration module, which is used to register an API task according to a task registration interface provided by the scheduling server, execute task registration logic, and write task information into a database; a time wheel module, which is used to obtain tasks to be executed from a database and perform task scheduling based on a time wheel algorithm; a lock mechanism module, which performs cluster scheduling scheduling based on a lock mechanism of a relational database; a task execution module, which is used to receive a task execution request pushed by a scheduling server, trigger task execution after decoding, and notify the scheduling server of the task execution result; a result update module, which is used to receive the task execution result and update the task execution log in the database.
[0017] Compared with the prior art, the beneficial effects of the present invention include:
[0018] 1. The present invention can not only meet the business scenarios of delayed scheduling, but can also be used as a general distributed scheduling system.
[0019] 2. The TCP full-duplex communication mechanism is adopted between the scheduling server and the scheduling executor of the present invention, which makes the communication more efficient and has better performance than the HTTP communication method of the traditional scheduling system.
[0020] 3. The present invention adopts a self-defined and customizable communication protocol, supports communication message encryption, and has high security and scalability.
[0021] 4. The present invention supports both manual configuration of scheduling tasks and registration of scheduling tasks via API. The business system can complete task registration directly through API calls, which is more real-time than the traditional timed polling method to trigger delayed tasks.
[0022] 5. The present invention supports the heartbeat detection mechanism between the scheduling server and the scheduling executor, removes failed nodes in time, ensures that each scheduling instruction can be sent to a healthy node, and has fault tolerance in a distributed environment.
[0023] 6. The distributed scheduling system of the present invention supports cluster deployment, and the scheduling server can be horizontally expanded, and has high availability in a distributed environment. BRIEF DESCRIPTION OF THE DRAWINGS
[0024] The disclosure of the present invention is described with reference to the accompanying drawings. It should be understood that the drawings are for illustrative purposes only and are not intended to limit the scope of protection of the present invention. In the accompanying drawings, the same reference numerals are used to refer to the same components. Among them:
[0025] Figure 1 This is an architecture diagram of a scheduling system according to an embodiment of the present invention;
[0026] Figure 2 This is a flow chart of a distributed delay scheduling method based on TCP long connection according to an embodiment of the present invention;
[0027] Figure 3 This is a schematic diagram of the data structure of the time wheel algorithm according to an embodiment of the present invention;
[0028] Figure 4 It is a structural schematic diagram of a distributed delay scheduling device based on TCP long connection in an embodiment of the present invention. DETAILED DESCRIPTION
[0029] It is easy to understand that according to the technical solution of the present invention, without changing the essential spirit of the present invention, a person skilled in the art can propose a variety of interchangeable structural modes and implementation modes. Therefore, the following specific implementation modes and drawings are only exemplary descriptions of the technical solution of the present invention, and should not be regarded as the whole of the present invention or as a limitation or restriction to the technical solution of the present invention.
[0030] According to one embodiment of the present invention, Figure 1 A distributed delay scheduling method based on TCP long connection runs on Figure 1 In the scheduling system shown, the scheduling system is mainly composed of a scheduling server and a scheduling executor.
[0031] The scheduling server is the core of the entire scheduling system and can be deployed in clusters. It has the functions of task registration, task scheduling, scheduling instruction issuance, executor node management, etc. The scheduling server is packaged with an API registration interface.
[0032] The scheduling executor is a client that performs specific scheduling tasks. It only needs to introduce the packaged client Jar package and configure relevant parameters to establish a TCP long connection with the scheduling server. It can receive scheduling instructions sent by the scheduling server and send heartbeat packets to the server. At the same time, it packages application registration and task registration interfaces, allowing applications to register scheduling tasks with simple API calls. The scheduling executor is implemented using Netty's Bootstrap. Whether it is a heartbeat message or a business message, the scheduling executor only needs to serialize the request entity into a byte stream, encode it through the Netty encoder, and send it to the scheduling server through the Bootstrap client, and receive the response packet from the scheduling server.
[0033] See also Figure 2 , a distributed delay scheduling method based on TCP long connection, comprising the following steps:
[0034] S101, establish TCP long connection communication between the scheduling server and the scheduling executor based on the Netty framework.
[0035] The network packet protocol of the above-mentioned TCP long connection communication is a customized TCP long connection protocol, and the network packet is divided into three parts: a fixed header, an extended header and a packet body.
[0036] (1) Fixed header meaning:
[0037]
[0038] These 8 bytes are fixed length, of which the SNB identifier occupies 2 bytes, and the character 'SNB' indicates that it is a network packet recognizable by this system; headLen occupies 2 bytes, indicating the length of the extended packet header; bodyLen occupies 4 bytes, indicating the length of the extended packet header + packet body (excluding the 8-byte fixed header).
[0039] (2) Meaning of the extended header:
[0040] The extended header is mainly used to store meta information, and its length is not fixed. It currently contains the following fields.
[0041] int direction; / / Network packet type, 1 = request, 2 = response
[0042] String apiName; / / api interface name, the server encapsulates a series of interfaces for the client to call
[0043] String resCode; / / Response error code
[0044] int seqno; / / Network packet identification
[0045] String clientId; / / Client unique identifier
[0046] String sign; / / Signature
[0047] (3) Meaning of the package:
[0048] The package body is the business data of the request or response. It can be empty and is a serialized JSON string with an unfixed length.
[0049] In an embodiment of the present invention, the above-mentioned TCP long connection communication also includes: configuring the heartbeat interval, for example, setting it to 3 seconds, the scheduling executor sends a heartbeat packet to the scheduling server. If the sending fails, the reconnection mechanism is triggered to establish a TCP connection with the scheduling server again to achieve network fault tolerance.
[0050] The heartbeat packet format is defined as follows:
[0051]
[0052] S102, registering the API application with the scheduling server according to the local configuration address and the client jar package, and the scheduling server saves the application node information in the relational database.
[0053] Specifically, the API application is registered with the scheduling server according to the local configuration address and the client jar package, including: the scheduling executor integrates the client jar package, and when the node starts, it automatically registers with the scheduling server according to the local configuration address to establish a TCP long connection. Call the application registration interface appRegister of the scheduling server to register the application information. The scheduling server receives the application registration request message from the scheduling executor, decodes the request method name in the extended packet header of the application registration request message, that is, the apiName field value, and if the apiName field value is appRegister, the API application is registered.
[0054] Since it is a customized network packet protocol, the present invention uses Netty's LengthFieldBasedFrameDecoder decoder, and the scheduling server is started using Netty's ServerBootstrap.
[0055] The appRegister interface contract is as follows:
[0056]
[0057]
[0058] The database table design is as follows:
[0059]
[0060] S103, according to the task registration interface provided by the scheduling server, perform API task registration, execute task registration logic, and write task information into the database. The scheduling server queries the database for task information and executes task scheduling.
[0061] Specifically, API task registration is performed according to the task registration interface provided by the scheduling server, including: connecting to the task registration interface taskRegister provided by the scheduling server; the scheduling server receives the task registration request message from the scheduling executor, decodes the request method name in the extended packet header of the task registration request message, that is, the apiName field value, and if the apiName field value is taskRegister, API task registration is performed.
[0062] At present, most distributed scheduling systems only support manual configuration of tasks, and do not support API registration of tasks. The present invention provides an API method to register tasks with the scheduling system, and there is no need to manually configure task information on the background management page.
[0063] The taskRegister interface contract is as follows:
[0064]
[0065] The database table design is as follows:
[0066]
[0067]
[0068] S104, obtaining tasks to be executed from the database, and scheduling tasks based on the time wheel algorithm. The present invention uses the time wheel algorithm to implement task scheduling, which can avoid relying on a third-party scheduling framework; at the same time, as the business scale grows, it can be improved at any time to improve performance.
[0069] See also Figure 3 The above-mentioned task scheduling based on the time wheel algorithm includes: obtaining the tasks to be executed from the database, and calculating the time scales of the three levels of month, week and day according to the next trigger time of the task. Add the tasks to the three levels of time wheels according to the time wheels to which they belong. The three time wheels run continuously at their own time scales at the same time. When the monthly time wheel and the weekly time wheel run to the specified scale, the tasks under the scale are taken out and sent to the sky wheel, and the sky wheel takes over the tasks. When the sky wheel runs to this time point, all tasks under this time point are taken out and triggered for execution.
[0070] S105, performing cluster scheduling and orchestration based on the lock mechanism of the relational database.
[0071] As a distributed scheduling system, cluster deployment must be supported to avoid single point failure. The present invention uses a lock mechanism based on a relational database to achieve parallel operation of multiple scheduling servers in a cluster environment, avoid concurrent scheduling problems in a cluster environment, and ensure the normal operation of the scheduling system in a cluster environment.
[0072] Specifically, the above-mentioned lock mechanism based on relational database for cluster scheduling and orchestration includes: locking each scheduling server node that queries the relational database for scheduling tasks to be executed. The scheduling server node that obtains the lock obtains the task list. According to the task list, the tasks to be executed are placed in the time wheel and wait for execution. The next execution time of the task is updated and written to the database. The scheduling server commits the database transaction, releases the database lock it holds, and enters the next cycle.
[0073] Each scheduling server node first locks when querying the relational database for the scheduled tasks to be executed, such as using the for update syntax when querying the database. The scheduling server node that obtains the lock executes the task list acquisition operation, such as querying the database for the tasks that will be triggered within 10 seconds and taking the first 100. The retrieved tasks to be executed are placed in the time wheel and wait for execution. The next execution time of the task is updated and written to the database. After completing this series of operations, the current scheduling server commits the database transaction, releases the database lock it holds, and enters the next cycle. In the next cycle, multiple scheduling servers continue to compete for the database lock. The scheduling server that obtains the lock queries the tasks to be executed from the database, submits the tasks to the time wheel, releases the lock... and so on, to achieve multiple scheduling servers working in parallel in a cluster environment.
[0074] The design of the relational database lock table involved is as follows:
[0075]
[0076]
[0077] S106, receiving the task execution request pushed by the scheduling server, triggering the task execution after decoding, and notifying the scheduling server of the task execution result.
[0078] After the client completes task registration and reaches the registered execution time, the server will push the task execution request to the client. The task push interface is defined as follows:
[0079]
[0080] Specifically, the above-mentioned receiving the task execution request pushed by the scheduling server and triggering the task execution after decoding include: the scheduling executor receives the task execution request message pushed by the scheduling server, and obtains the task push information after decoding by the decoder, and the task push information includes class methods and parameters. At this time, there are two ways to trigger task execution: one is to use the Java reflection mechanism locally to trigger the method call and execute the task logic; the other is to obtain the Bean instance in the container based on the Spring framework and execute the corresponding method. The first method does not need to rely on the Spring framework, and the second method is more convenient to use in the currently popular SpringBoot framework.
[0081] Since the scheduling server and the scheduling executor communicate via a long intranet connection, the communication efficiency is extremely high. The messages from the scheduling server are delivered to the client node in near real time, and there is no HTTP protocol header overhead. The scheduling executor returns asynchronously after receiving the scheduling execution request, and notifies the scheduling server of the execution result through the callback interface after successful execution.
[0082] S107, receiving the task execution result, and updating the task execution log in the database.
[0083] After the task execution is completed, the scheduling executor asynchronously notifies the scheduling server of the execution result and calls the scheduling server's callback interface taskPushCallBack through a long connection. After receiving the callback message, the scheduling server decodes the data and updates the corresponding task execution log in the database.
[0084] The callback interface is defined as follows:
[0085]
[0086] When the scheduling server triggers a task, it first creates a task execution flow with the scheduling status being executed. After receiving the callback notification from the scheduling executor, it updates the task execution status and duration information.
[0087] The database table design involved is as follows:
[0088]
[0089]
[0090] See also Figure 4 The present invention also discloses a distributed delay scheduling device based on TCP long connection, comprising:
[0091] The long connection module 101 establishes TCP long connection communication between the scheduling server and the scheduling executor based on the Netty framework.
[0092] The application registration module 102 is used to register the API application with the scheduling server according to the local configuration address and the client jar package, and the scheduling server saves the application node information in the relational database.
[0093] The task registration module 103 is used to perform API task registration according to the task registration interface provided by the scheduling server, execute task registration logic, and write task information into the database.
[0094] The time wheel module 104 is used to obtain tasks to be executed from the database and schedule the tasks based on the time wheel algorithm.
[0095] The lock mechanism module 105 performs cluster scheduling and arrangement based on the lock mechanism of the relational database.
[0096] The task execution module 106 is used to receive the task execution request pushed by the scheduling server, trigger the task execution after decoding, and notify the scheduling server of the task execution result.
[0097] The result updating module 107 is used to receive the task execution result and update the task execution log in the database.
[0098] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0099] In summary, the beneficial effects of the present invention include:
[0100] 1. The present invention can not only meet the business scenarios of delayed scheduling, but can also be used as a general distributed scheduling system.
[0101] 2. The TCP full-duplex communication mechanism is adopted between the scheduling server and the scheduling executor of the present invention, which makes the communication more efficient and has better performance than the HTTP communication method of the traditional scheduling system.
[0102] 3. The present invention adopts a self-defined and customizable communication protocol, supports communication message encryption, and has high security and scalability.
[0103] 4. The present invention supports both manual configuration of scheduling tasks and registration of scheduling tasks via API. The business system can complete task registration directly through API calls, which is more real-time than the traditional timed polling method to trigger delayed tasks.
[0104] 5. The present invention supports the heartbeat detection mechanism between the scheduling server and the scheduling executor, removes failed nodes in time, ensures that each scheduling instruction can be sent to a healthy node, and has fault tolerance in a distributed environment.
[0105] 6. The distributed scheduling system involved in the present invention supports cluster deployment, and the scheduling server can be horizontally expanded and has high availability in a distributed environment.
[0106] It should be understood that if the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention is essentially or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk and other media that can store program codes.
[0107] The technical scope of the present invention is not limited to the contents in the above description. Those skilled in the art can make various deformations and modifications to the above embodiments without departing from the technical idea of the present invention, and these deformations and modifications should all fall within the protection scope of the present invention.
Claims
1. A distributed delay scheduling method based on TCP long connection, It is characterized in that The steps include: Establish TCP long connection communication between the scheduling server and the scheduling executor based on the Netty framework; According to the local configuration address and the client jar package, the API application is registered with the scheduling server, and the scheduling server stores the application node information in the relational database; According to the task registration interface provided by the scheduling server, perform API task registration, execute task registration logic, and write task information into the database; Obtain tasks to be executed from the database and schedule them based on the time wheel algorithm; Cluster scheduling and orchestration based on the locking mechanism of relational database; Receive the task execution request pushed by the scheduling server, trigger the task execution after decoding, and notify the scheduling server of the task execution result; Receive task execution results and update task execution logs in the database; The network packet of the TCP long connection communication is divided into three parts: a fixed header, an extended header and a body. The fixed header includes a 2-byte SNB identifier, a 2-byte header length and a 4-byte body length. The extended header is used to store meta information. The body is the service data of the request or response, which is a serialized JSON string. The API application is registered with the scheduling server according to the local configuration address and the client jar package, including: the scheduling executor integrates the client jar package, and when the node is started, it automatically registers with the scheduling server according to the local configuration address to establish a TCP long connection; calling the application registration interface appRegister of the scheduling server to register application information; the scheduling server receives the application registration request message of the scheduling executor, decodes the request method name in the extended packet header of the application registration request message, that is, the apiName field value, and if the apiName field value is appRegister, then the API application is registered; The API task registration is performed according to the task registration interface provided by the scheduling server, including: connecting to the task registration interface taskRegister provided by the scheduling server; the scheduling server receives the task registration request message from the scheduling executor, decodes the request method name in the extended packet header of the task registration request message, that is, the apiName field value, and if the apiName field value is taskRegister, then the API task registration is performed.
2. According to claim 1, the distributed delay scheduling method based on TCP long connection, It is characterized in that The TCP long connection communication also includes: configuring the heartbeat interval, the scheduling executor sends a heartbeat packet to the scheduling server, and if the sending fails, a reconnection mechanism is triggered to establish a TCP connection with the scheduling server again.
3. The distributed delay scheduling method based on TCP long connection according to claim 1, It is characterized in that The task scheduling based on the time wheel algorithm includes: Obtain the tasks to be executed from the database, and calculate the time scales at the three levels of month, week, and day according to the next trigger time of the task; Add tasks to the three levels of time wheels according to the time wheels they belong to; The three time wheels run continuously at their own time scales. When the monthly time wheel and weekly time wheel run to the specified scale, the tasks under that scale are taken out and sent to the sky wheel, which then takes over the tasks. When the day wheel runs to this time point, all tasks under this time point are taken out and triggered for execution.
4. The distributed delay scheduling method based on TCP long connection according to claim 1, It is characterized in that The lock mechanism based on the relational database performs cluster scheduling and orchestration, including: Lock each scheduling server node that queries the relational database for scheduling tasks to be executed; The scheduling server node that obtains the lock obtains a task list; According to the task list, the tasks to be executed are placed in the time wheel and wait for execution; Update the next execution time of the task and write it to the database; The scheduling server commits the database transaction, releases the database lock it holds, and enters the next cycle.
5. The distributed delay scheduling method based on TCP long connection according to claim 1, It is characterized in that The receiving of the task execution request pushed by the scheduling server and triggering the task execution after decoding includes: Receive the task execution request message pushed by the scheduling server, and obtain the task push information after decoding by the decoder, wherein the task push information includes class methods and parameters; Use the local Java reflection mechanism to trigger method calls, or obtain the Bean instance method in the container based on the Spring framework to trigger task execution.
6. A distributed delay scheduling device based on TCP long connection, It is characterized in that include: The long connection module establishes TCP long connection communication between the scheduling server and the scheduling executor based on the Netty framework; An application registration module is used to register an API application with a scheduling server according to a local configuration address and a client jar package. The scheduling server stores application node information in a relational database. The task registration module is used to register API tasks according to the task registration interface provided by the scheduling server, execute task registration logic, and write task information into the database; The time wheel module is used to obtain tasks to be executed from the database and schedule tasks based on the time wheel algorithm; The lock mechanism module performs cluster scheduling and orchestration based on the lock mechanism of the relational database; The task execution module is used to receive the task execution request pushed by the scheduling server, trigger the task execution after decoding, and notify the scheduling server of the task execution result; The result update module is used to receive the task execution results and update the task execution log in the database; The network packet of the TCP long connection communication is divided into three parts: a fixed header, an extended header and a body. The fixed header includes a 2-byte SNB identifier, a 2-byte header length and a 4-byte body length. The extended header is used to store meta information. The body is the service data of the request or response, which is a serialized JSON string. The API application is registered with the scheduling server according to the local configuration address and the client jar package, including: the scheduling executor integrates the client jar package, and when the node is started, it automatically registers with the scheduling server according to the local configuration address to establish a TCP long connection; calling the application registration interface appRegister of the scheduling server to register application information; the scheduling server receives the application registration request message of the scheduling executor, decodes the request method name in the extended packet header of the application registration request message, that is, the apiName field value, and if the apiName field value is appRegister, then the API application is registered; The API task registration is performed according to the task registration interface provided by the scheduling server, including: connecting to the task registration interface taskRegister provided by the scheduling server; the scheduling server receives the task registration request message from the scheduling executor, decodes the request method name in the extended packet header of the task registration request message, that is, the apiName field value, and if the apiName field value is taskRegister, then the API task registration is performed.
