A timer implementation method

The timer implementation method simplifies timer management by packaging tasks into a jar file with annotations and database queries, decoupling from the Spring framework, reducing complexity and learning costs for developers.

CN111813390BActive Publication Date: 2024-12-20INSPUR SOFTWARE CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202010700561.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-07-20
Publication Date
2024-12-20
Estimated Expiration
2040-07-20

AI Technical Summary

Technical Problem

Existing timer implementations, such as the Quartz tool integrated with the Spring framework, require reliance on the Spring framework for configuration and management, posing high learning costs and complexity for developers who do not want to use the framework.

Method used

A timer implementation method that packages scheduled tasks into a jar file, allowing configuration through annotations and database queries, decoupled from the Spring framework, enabling easy integration and management without complex XML configurations.

Benefits of technology

Simplifies timer management, reducing learning costs and complexity, making it easier for developers to focus on business logic development by eliminating the need for Spring framework reliance and complex configurations.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN111813390B_ABST
    Figure CN111813390B_ABST
Patent Text Reader

Abstract

The present invention discloses a timer implementation method, the method comprising the following steps: S1, packaging the source code of the configured timed task to obtain a timed task jar package; S2, referencing the timed task jar package to add a timer in the project during the project development process. The present invention solves the trouble that the general framework needs to rely on other components when using timed tasks, and the project is more lightweight, easier to use and easier to get started for developers, saving learning costs, and focusing on business layer logic development instead of studying how to use it.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention relates to the field of computer technology, in particular to a timer implementation method. Background Art

[0002] In the process of computer project development, especially when facing business scenarios, timers are often used. For example, when you need to start a scheduled task to process business needs at a fixed time and point, perhaps data ETL, order statistics, delivery settlement, etc., timers play a very critical role. In the entire business processing process, timers play a good assistant role, saving time and facilitating programmers' operations.

[0003] Currently, the more popular timer processing method is to use the quarzt tool integrated with the open source spring framework, which provides a relatively complete timing processing mechanism. However, in the process of using it, you must rely on the spring framework to provide services, and need to configure a lot of xml, properties and other files to load more beans and manage bean instances in a containerized way. However, for those who do not want to use the spring framework to build projects, it is more troublesome to handle timing tasks.

[0004] At the same time, while quarzt brings powerful functions to programmers, it still brings great cost consumption in terms of learning cost and ease of use. Although quarzt provides many powerful functions, for beginners, it requires higher learning costs, which increases the difficulty of project development and affects the overall development progress of the project. Summary of the invention

[0005] The purpose of the present invention is to provide a timer implementation method to address the above shortcomings, so that the timer is completely decoupled from the spring framework, so that developers do not need to spend too much energy studying the use of timers, but more technical solutions adopted by the present invention are:

[0006] A timer implementation method, the method comprising the following steps:

[0007] S1. Package the source code of the configured scheduled task to obtain the scheduled task jar package;

[0008] S2. During the project development process, reference the scheduled task jar package to add a timer to the project.

[0009] As a further optimization, in step S1 of the present invention, the source code configuration process of the scheduled task includes the following steps:

[0010] S101. Customize a target class to execute a scheduled task.

[0011] S102, specify the directory where the target class is located;

[0012] S103, starting the target classes for executing the scheduled tasks in batches through the class loader.

[0013] As a further optimization, the source code configuration process of the scheduled task described in the present invention is based on the open source quarzt framework, and no complicated XML configuration is required.

[0014] As a further optimization, the present invention defines that all classes that implement the MyJob annotation and inherit BaseJob are target classes to be loaded.

[0015] As a further optimization, the present invention includes the following aspects in the annotation of MyJob:

[0016] Write methods that need to be executed regularly in the task class;

[0017] Define the timer configuration class. The configuration class needs to have the @Configuration annotation to mark the configuration class.

[0018] Configure scheduled task properties, including whether to execute scheduled tasks concurrently, set the name of the scheduled tasks, group the scheduled tasks, and the method to be used to execute the scheduled tasks.

[0019] Configure the scheduled task trigger to decide when to trigger the scheduled task;

[0020] Configure the scheduled task scheduling factory and set the processing for the next scheduled task when a scheduled task is executed;

[0021] Define the time configuration class for scheduled tasks and operate the database layer. It must contain the @Component annotation that instantiates Myjob into the spring container and the @EnableScheduling annotation that starts the timer task.

[0022] Set the scheduled task method. The method content is the scheduled task execution time checked from the database. The @Scheduled annotation is used to set the database to be checked every few seconds, and decide whether to reset the scheduled task based on the query results.

[0023] As a further optimization, the source code of the scheduled task described in the present invention is managed by Maven, and a jar package is generated through a pom file.

[0024] As a further optimization, in step S2 of the present invention, the execution process of the jar package includes the following steps:

[0025] S201, determine whether there is a Myjob annotation, and if so, execute the next step;

[0026] S202, determine whether to inherit Basejob, if it is determined to inherit BaseJob, execute the next step;

[0027] S203, load the target class through the class loader and store it in the Map, and finally start the timer in batches.

[0028] As a further optimization, the startup method of the source code of the scheduled task described in the present invention can be customized, and can be configured and started through web.xml, or defined and started using @WebServlet.

[0029] The present invention has the following advantages:

[0030] 1. The present invention solves the trouble that the general framework needs to rely on other components when using scheduled tasks. The project is more lightweight, easier to use and easier to get started for developers, saving learning costs and focusing on business layer logic development instead of studying how to use it.

[0031] 2. The present invention can be completely decoupled from the spring framework, saving a lot of configuration and no longer relying on spring container loading.

[0032] 3. The developers of the present invention do not need to pay attention to the underlying code, saving a lot of learning costs, providing a simple and easy-to-develop entry, only needing one annotation to complete the configuration of the scheduled task, and supporting custom timing rule configuration. BRIEF DESCRIPTION OF THE DRAWINGS

[0033] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.

[0034] The present invention is further described below in conjunction with the accompanying drawings:

[0035] Figure 1 It is a schematic diagram of the process of the present invention. DETAILED DESCRIPTION

[0036] The present invention is further described below in conjunction with the accompanying drawings and specific embodiments so that those skilled in the art can better understand the present invention and implement it. However, the embodiments are not intended to limit the present invention. In the absence of conflict, the embodiments of the present invention and the technical features in the embodiments may be combined with each other.

[0037] It should be understood that in the description of the embodiments of the present invention, the words "first", "second", etc. are only used for the purpose of distinguishing the description, and cannot be understood as indicating or implying relative importance, nor can they be understood as indicating or implying an order. The "plurality" in the embodiments of the present invention refers to two or more.

[0038] The "and / or" in the embodiments of the present invention is only a description of the association relationship of the associated objects, indicating that there may be three relationships. For example, A and / or B can represent: A exists alone, B exists alone, and A and B exist at the same time. In addition, the character " / " in this document generally indicates that the associated objects before and after are in an "or" relationship.

[0039] This embodiment provides a timer implementation method, such as Figure 1 As shown, the method comprises the following steps:

[0040] S1. Package the source code of the configured scheduled task to obtain the scheduled task jar package;

[0041] S101, customize a target class for executing a scheduled task, by defining all classes that implement the MyJob annotation and inherit BaseJob as target classes to be loaded. The source code configuration process of the scheduled task is based on the open source quarzt framework, and no complex xml configuration is required.

[0042] The annotations for MyJob include the following aspects:

[0043] A1. Write the method that needs to be executed regularly in the task class;

[0044] A2. Define a timer configuration class. The configuration class needs to have the @Configuration annotation to mark the configuration class.

[0045] A3. Configure scheduled task properties, including whether to execute scheduled tasks concurrently, setting the name of the scheduled tasks, grouping scheduled tasks, and the method to be used for scheduled tasks.

[0046] A4. Configure the scheduled task trigger to determine when to trigger the scheduled task.

[0047] A5. Configure the scheduled task scheduling factory and set the processing for the next scheduled task when a scheduled task is executed;

[0048] A6. Define the time configuration class for scheduled tasks and operate the database layer. It must contain the @Component annotation that instantiates Myjob into the spring container and the @EnableScheduling annotation that starts the timer task.

[0049] A7. Set the scheduled task method. The method content is to query the scheduled task execution time from the database. Use the @Scheduled annotation to set the database query every few seconds and decide whether to reset the scheduled task based on the query results.

[0050] S102, specify the directory where the target class is located;

[0051] S103, starting the target classes for executing the scheduled tasks in batches through the class loader.

[0052] S2. During the project development process, reference the scheduled task jar package to add a timer to the project.

[0053] S201, determine whether there is a Myjob annotation, and if so, execute the next step;

[0054] S202, determine whether to inherit Basejob, if it is determined to inherit BaseJob, execute the next step;

[0055] S203, load the target class through the class loader and store it in the Map, and finally start the timer in batches.

[0056] The time selected by the judgment step of whether Myjob is annotated and whether it inherits BaseJob is a custom start event. For example, it can be started manually or triggered by associated parameters.

[0057] As a preferred embodiment, the source code of the scheduled task is managed through Maven, and a jar package is generated through a pom file.

[0058] As a preferred embodiment, the startup mode of the source code of the scheduled task can be customized, and can be started by configuration through web.xml, or defined and started using @WebServlet.

[0059] In the embodiment of the present invention, the custom target class, that is, all classes that implement the MyJob annotation and inherit the BaseJob are the target classes to be loaded. By specifying the package name of the target class, when the application starts, the class loader actively loads these target classes to achieve the start and stop control of the scheduled task, thereby realizing the loading of the timer.

[0060] The above-described embodiments are only preferred embodiments for fully illustrating the present invention, and the protection scope of the present invention is not limited thereto. Equivalent substitutions or changes made by those skilled in the art based on the present invention are within the protection scope of the present invention. The protection scope of the present invention shall be subject to the claims.

Claims

1. A timer implementation method, characterized in that: The method comprises the following steps: S1. Package the source code of the configured scheduled task to obtain the scheduled task jar package; Specifically, the source code configuration process of the scheduled task includes the following steps: S101, customize a target class for executing a scheduled task, by defining all classes that implement the MyJob annotation and inherit BaseJob as target classes to be loaded; the source code configuration process of the scheduled task is based on the open source quarzt framework, and no complex xml configuration is required; The annotations for MyJob include the following aspects: Write methods that need to be executed regularly in the task class; Define the timer configuration class. The configuration class needs to have the @Configuration annotation to mark the configuration class. Configure scheduled task properties, including whether to execute scheduled tasks concurrently, set the name of the scheduled tasks, group the scheduled tasks, and the method to be used to execute the scheduled tasks. Configure the scheduled task trigger to decide when to trigger the scheduled task; Configure the scheduled task scheduling factory and set the processing for the next scheduled task when a scheduled task is executed; Define the time configuration class for scheduled tasks and operate the database layer. It must contain the @Component annotation that instantiates Myjob into the spring container and the @EnableScheduling annotation that starts the timer task. Set the scheduled task method. The method content is the scheduled task execution time checked from the database. The @Scheduled annotation is used to set the database to be checked every few seconds, and decide whether to reset the scheduled task based on the query results. S102, specify the directory where the target class is located; S103, starting the target class for executing the scheduled task in batches through the class loader; S2. During the project development process, reference the scheduled task jar package to add a timer in the project; Specifically, the execution process of the jar package includes the following steps: S201, determine whether there is a Myjob annotation, and if so, execute the next step; S202, determine whether to inherit Basejob, if it is determined to inherit BaseJob, execute the next step; S203, load the target class through the class loader and store it in the Map, and finally start the timer in batches.

2. The timer implementation method according to claim 1, characterized in that: The source code of the scheduled task is managed through Maven, and a jar package is generated through the pom file.

3. The timer implementation method according to claim 1, characterized in that: The startup method of the source code of the scheduled task can be customized. It can be configured and started through web.xml, or defined and started using @WebServlet.