Asynchronous excel export processing method and system, electronic equipment and storage medium

Through the asynchronous Excel export processing method, using MQ message queue and easyExcel framework to monitor CPU and memory resources, the unified payment back-end management system can export Excel with high efficiency and controllability, and solve the system unavailability and slow progress caused by the large amount of data. Clarity question.

CN118760715APending Publication Date: 2024-10-11WUHAN ZBANK CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410848136.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-06-27
Publication Date
2024-10-11

AI Technical Summary

Technical Problem

When the existing unified payment back-end management system exports Excel, the large amount of data causes the front page to wait for a long time, consumes a lot of CPU and memory, makes the system unavailable, and the export task progress is unclear and the process is uncontrollable.

Method used

The asynchronous Excel export processing method is used to implement the one-producer-multi-consumer model through the MQ message queue. Combined with the easyExcel framework, the thread pool and blocking queue are used to monitor CPU and memory resources to achieve task progress display and process control.

Benefits of technology

It reduces CPU and memory resource overhead, prevents system crashes, realizes task intelligence and process controllability, supports multi-node deployment, and provides an efficient and visual export solution.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118760715A_ABST
    Figure CN118760715A_ABST
Patent Text Reader

Abstract

The invention provides an asynchronous excel export processing method and system, electronic equipment and a storage medium. The method comprises the steps that 1, export operation is initiated, and export information is stored in a database; 2, exporting a task management page by newly adding excel in the database; 3, accessing the web service to an MQ consumption end, and obtaining a task id message in the queue; 4, querying the total record number of data to be exported according to a task'query condition ', constructing a sheetObject object according to the sheet page number, and putting the sheetObject object into a linkedBlockingQueue queue; 5, executing a sub-thread of the thread pool; judging whether execution of all the threads is abnormal or not, if yes, quitting the program and updating the task state to be'export abnormity '; and step 6, when all the threads are executed without abnormity, exporting the exclWrite object into the excel file, and updating a database task state. The method aims at solving the problems that a java web system exports excel server cpu and memory resource alarm causes service unavailability, unclear task export progress and uncontrollable process.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to an asynchronous Excel export processing method, system, electronic device and storage medium. Background Art

[0002] The existing unified payment back-end management system exports Excel based on the POI framework. After selecting the search conditions on the page, the synchronous export is submitted to the backend, and SQL queries the data that meets the conditions, and then exports it through POI loop. In this process, when the amount of data is large, the following problems will occur: 1. The front page waits for a long time and the current export progress is unknown.

[0003] 2. High CPU and memory consumption causes macros and system unavailability.

[0004] 3. The export task process is uncontrollable (for example, if a problem is found and it cannot be stopped in time, the service will be unavailable). Summary of the invention

[0005] Aiming at the technical problems existing in the prior art, the present invention provides an asynchronous Excel export processing method, system, electronic device and storage medium, which are used to solve the problems of service unavailability, unclear export task progress and uncontrollable process caused by CPU and memory resource alarms of Excel server when exporting Excel from a Java web system.

[0006] According to a first aspect of the present invention, the present invention provides an asynchronous Excel export processing method, comprising the following steps: Step 1: The export operation is triggered in the foreground to save the export information to the database and store the task id in the MQ consumer; Step 2: The MQ consumer obtains the task ID message and adds an Excel export task management page; Step 3: The web service accesses the MQ consumer end to obtain the task message in the queue; Step 4: Query the total number of records to be exported according to the task "query condition", build a page object according to the number of sheet pages and put it into the blocking queue based on the link node; Step 5: Execute the thread pool sub-threads; determine whether all threads are abnormal, if so, exit the program and update the task status to "export exception"; Step 6: After all threads have executed without exception, export the data frame to an Excel file and update the database task status.

[0007] Based on the above technical solution, the present invention can also make the following improvements.

[0008] Optionally, the foreground triggers the export operation to save the export information into the database, and makes the following judgment when exporting the Excel table: Determine whether the number of tasks in the current "to be exported" and "exporting" states is greater than the maximum number of tasks. If so, the operation fails and the task ID is saved to the MQ consumer end. Determine whether the exported data is empty; Determine whether the number of exported items is greater than the maximum number limit; If the above judgment is not satisfied, the response prompt information is returned; if it is satisfied, the task information is saved to the database and the task id is saved to the MQ consumer.

[0009] Optionally, the MQ consumer obtains the task id message and makes the following judgment: According to the message, query the task information in the database to determine whether the task status is "pending export", otherwise delete the message; determine whether the current machine CPU and memory usage are lower than the preset percentage, if so, do not process this message and let the less busy machine process it.

[0010] Optionally, obtaining task messages in the queue includes: Get the task ID message, query the database task ID details, and get the current server CPU and memory information.

[0011] Optionally, the querying of the total number of records of data to be exported according to the task "query condition" includes: Calculate the total number of sheet pages according to the data of each sheet page, calculate and set the field value of the sheetObject object at the corresponding position, build a blocking queue based on the link node of the sheetObject object type, with a length of the total number of sheet pages, and set the field value of the sheetObject object at the corresponding position according to the order.

[0012] Optionally, constructing a sheetObject object according to the number of sheet pages and putting it into the linkedBlockingQueue queue includes: Create a countDownLatch object with the counter number as min(number of sheets, number of CPU cores of the current machine); Create a threadPoolExecutor thread pool with a size of min(number of sheet pages, number of CPU cores of the current machine).

[0013] Optionally, determining whether all threads execute abnormally includes: If a thread sets the flag, it means someone has manually clicked the "Stop" operation, changing the task status to "Stopped".

[0014] According to a second aspect of the present invention, an asynchronous Excel export processing system is provided, comprising: The first processing module is used to trigger the export operation in the foreground to save the export information to the database and store the task id in the MQ consumer end; The second processing module is used to enable the MQ consumer to obtain the task ID message, add an Excel export task management page, and enable the web service to access the MQ consumer to obtain the task message in the queue; The third processing module is used to query the total number of records to be exported according to the task "query condition", build a page object according to the number of sheet pages and put it into the blocking queue based on the link node; The fourth processing module is used to execute the thread pool sub-threads; determine whether all threads are executed abnormally, if so, exit the program and update the task status to "export exception"; The fifth processing module is used to wait until all threads are executed without exception, export the data frame into an Excel file, and update the database task status.

[0015] The present invention also provides an 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, the asynchronous Excel export processing method as described above is implemented.

[0016] The present invention also provides a non-transitory computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the asynchronous Excel export processing method as described above is implemented.

[0017] Technical effects and advantages of the present invention: The present invention provides an asynchronous excel export processing method, system, electronic device and storage medium, which reduces memory and CPU resource overhead in large quantities by combining the easyExcel framework, and prevents system macros from causing service unavailability. The task of the present invention adopts an mq message queue to implement a one-producer-multiple-consumer mode, and supports multi-node deployment to achieve horizontal business expansion. The multi-threaded mode is adopted to make full use of CPU resources. Before the task is executed, the local CPU and memory resources are monitored to exceed the threshold, and the local machine discards the task to let the machine with better resources execute it. The real-time progress of the task can be known on the task management page, and the task can be stopped or executed according to the situation. Intelligentization and process controllability are achieved.

[0018] Other features and advantages of the present invention will be described in the following description, and partly become apparent from the description, or understood by practicing the present invention. The purpose and other advantages of the present invention can be realized and obtained by the structures pointed out in the description, claims and drawings. BRIEF DESCRIPTION OF THE DRAWINGS

[0019] Figure 1 This is a schematic diagram of saving the foreground export information to the MQ consumer end provided by an embodiment of the present invention; Figure 2 It is a schematic diagram of saving the background export information to a database provided by an embodiment of the present invention; Figure 3 This is a diagram of a newly added Excel export task management page provided in an embodiment of the present invention; Figure 4 It is a schematic diagram of the background sub-thread execution process provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0020] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0021] The embodiment of the present invention aims to solve the problems of the Java web system exporting Excel server CPU, memory resource alarm leading to service unavailability, unclear export task progress and uncontrollable process, and thus provide an efficient, visual and process-controllable intelligent export solution. It is used for export task progress display and process control. In this embodiment, the existing professional terms are explained as follows: sheetObject: a custom Java class used to store the start and end record numbers of the current sheet page and the current task information; Sheet page: sheet worksheet in Excel file; MQ: open source message queue framework; Redis - open source in-memory database. linkedBlockingQueue: a blocking queue based on linked nodes; countDownLatch: concurrency control tool class; threadPoolExecutor: java thread pool; easyExcel: open source framework for exporting large amounts of data to Excel; excelWrite: easyExcel framework tool class, used to write objects into excel files; writeSheet: sheet object in easyExcel framework; Based on the defects in the background technology, an embodiment of the present invention proposes an asynchronous Excel export processing method, including the following steps: Step 1: The export operation is triggered in the foreground to save the export information to the database and store the task id in the MQ consumer; In this embodiment, the export task information is saved to the database, including: querying the database for detailed task ID information, and obtaining the current server CPU and memory information. Figure 1 The figure shows the schematic diagram of exporting information from the front desk to the database, where the following judgment is made when exporting the Excel table: 1. Determine whether the number of tasks in the current "pending export" and "exporting" states in the system is greater than the maximum number of tasks. If so, the operation fails; 2. Determine whether the exported data is empty; 3. Determine whether the number of exported items is greater than the maximum limit; If the above judgment is not satisfied, the response prompt information is returned; if it is satisfied, the task information is saved to the database, and then the task id is saved to MQ.

[0022] Step 2: The MQ consumer obtains the task ID message and adds an Excel export task management page; Figure 2 Schematic diagram of backend export information saved to a database provided by an embodiment of the present invention, such as Figure 2 As shown, the MQ consumer obtains the task id message and makes the following judgments: query the task information in the database according to the message to determine whether the task status is "pending export", otherwise delete the message; determine whether the current machine CPU and memory usage are less than 70%, if so, do not process this message and let the less busy machine process it.

[0023] Added excel export task management page such as Figure 3 As shown, description: As shown in the figure, a. "Refresh" refreshes the current table and queries the latest information of the current export task; b. Click "Stop" to stop the tasks of "To be exported" and "Exporting". Change the status to "Stopped" c. "Execute" can only operate tasks in the "Stopped" state. Change the state to "Waiting for Export" and determine whether the number of tasks in the "Waiting for Export" and "Exporting" states is greater than the maximum number of tasks. If so, the operation fails. d. "Download" can only operate tasks in the "Exported" state. Download files according to the "Full file path"; e. Every 10 seconds, the page will asynchronously request the background to query the latest task progress of the task with the task ID whose status is "exporting" on the page, so as to display the task progress in real time.

[0024] Step 3: The web service accesses the MQ consumer end to obtain the task message in the queue; In this embodiment, the acquisition of task messages in the queue specifically includes: acquiring task ID messages, querying the database for detailed information on the task ID, and acquiring the current server CPU and memory information.

[0025] Step 4: Query the total number of records to be exported based on the task "Query Conditions", and calculate the total number of sheet pages based on 1 million records per sheet page; construct a sheetObject object based on the number of sheet pages and put it into the linkedBlockingQueue queue; The details are as follows: In the sheetObject class: the length is the "total number of sheet pages", and the following field values ​​of the sheetObject object at the corresponding position are calculated and set: startNum. Starting record number (calculation rule: ((subscript + 1-1) × 100) + 1) endNum End record number (calculation rule: (subscript + 1) × 100) sheetNumsheet page name number (calculation rule: ((subscript + 1)) taskID task ID (assignment rule: current task id) count total number of records (assignment rule: total number of records to be exported) According to the number of sheets, a linkedBlockingQueue of sheetObject type is constructed with a length of "total number of sheets", and the sheetObject object fields at the corresponding positions are set in order as follows: a. Start recording the number b. End record number c.sheet page name number d.Task ID e.Total number of records Create a countDownLatch object with the number of counters as min(sheet pages, current machine CPU cores); create a threadPoolExecutor thread pool with the size as min(sheet pages, current machine CPU cores); Step 5: The thread pool subthread executes; determine whether all threads execute abnormally, if so, exit the program and update the task status to "export exception"; Determining whether all threads are abnormally executed includes: determining that the thread setting flag indicates that someone manually clicks the "stop" operation, and changes the task status to "stopped"; The thread pool subthread execution is as follows Figure 4 As shown, the following steps are included: 1) Query the task information according to the "task ID" set in sheetObject, calculate how many pages the current sheet page data needs to be queried according to the "starting number of records" and "page query page size" set in sheetObject, and create a writeSheet object; 2) Loop to determine the total number of pages in the current sheet, and execute the following process to increase the total number of pages in the loop: a. Determine whether the task status is set to "Stopped". If so, set the flag to exit the loop; b. Otherwise, calculate the starting and ending record numbers of the paging query based on the "starting record number", "page query page size", and "ending record number"; c. Query the data to be exported based on the "query condition" set in sheetObject and the number of records calculated in b; d. Set the query collection and writeSheet object to the excelWriter object; e. Calculate the percentage of the current number of exports to the total number of exports, and set it to redis according to the "task id" in sheetObject (the formula is: current percentage + existing percentage in redis).

[0026] 3) Query the task details according to the "Task ID" set in sheetObject, calculate the paging query condition information required for the current sheet page according to the "Starting number of records" and "Paging query page size" set in sheetObject, and create a writeSheet object; The process of loop query and exporting data is as follows: a. Determine whether the task status is set to "Stopped". If so, set the flag to exit the loop.

[0027] b. Calculate the starting and ending records of the paging query according to the "starting record number", "page query page size", and "ending record number" and the "query condition" set in sheetObject to query the data to be exported; 4). Set the query collection and writeSheet object to the excelWriter object; 5). Calculate the percentage of the current number of exports to the total number of exports, and add and set it to redis according to the "task id" in sheetObject to build an excelWriter object.

[0028] Step 6: After all threads have executed without exception, export the excelWrite object to an excel file and update the database task status. At this time, the database task status is: "Exported".

[0029] In summary, the asynchronous excel export processing method provided by the present invention reduces the resource overhead of memory and CPU in large quantities by combining the easyExcel framework, and prevents the system macro from causing service unavailability. The task of the present invention adopts the mq message queue to realize the one-producer-multiple-consumer mode, and supports multi-node deployment to realize horizontal business expansion. The multi-threaded mode is adopted to make full use of CPU resources. Before the task is executed, the local CPU and memory resources will be monitored to exceed the threshold, and the local machine will discard the task to let the machine with better resources execute it. The real-time progress of the task can be known on the task management page, and the task can be stopped and executed according to the situation. Realize intelligence and process controllability.

[0030] According to a second aspect of the present invention, an asynchronous Excel export processing system is provided, comprising: The first processing module is used to trigger the export operation in the foreground to save the export information to the database and store the task id in the MQ consumer end; The second processing module is used to enable the MQ consumer to obtain the task ID message, add an Excel export task management page, and enable the web service to access the MQ consumer to obtain the task message in the queue; The third processing module is used to query the total number of records to be exported according to the task "query condition", build a page object according to the number of sheet pages and put it into the blocking queue based on the link node; The fourth processing module is used to execute the thread pool sub-threads; determine whether all threads are executed abnormally, if so, exit the program and update the task status to "export exception"; The fifth processing module is used to wait until all threads are executed without exception, export the data frame into an Excel file, and update the database task status.

[0031] It can be understood that the asynchronous Excel export processing system provided by the present invention corresponds to the asynchronous Excel export processing method provided by the aforementioned embodiments. The relevant technical features of the asynchronous Excel export processing system can refer to the relevant technical features of the asynchronous Excel export processing method, which will not be repeated here.

[0032] The present invention illustrates a physical structure diagram of an electronic device, which may include: a processor, a communications interface, a memory, and a communication bus, wherein the processor, the communications interface, and the memory communicate with each other through the communication bus. The processor may call the logic instructions in the memory to execute the steps of an asynchronous Excel export processing method.

[0033] In addition, the logic instructions in the above-mentioned memory can be implemented in the form of software functional units and can be stored in a computer-readable storage medium when sold or used as an independent product. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, which is stored in a storage medium and includes several 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, etc., which can store program code.

[0034] On the other hand, the present invention further provides a non-transitory computer-readable storage medium having a computer program stored thereon, which is implemented to execute the steps of the asynchronous Excel export processing method provided above when the computer program is executed by a processor.

[0035] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.

[0036] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.

[0037] Although the preferred embodiments of the present invention have been described, those skilled in the art may make other changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the present invention.

[0038] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. An asynchronous Excel export processing method, characterized in that: The following steps are involved: Step 1: The export operation is triggered in the foreground to save the export information to the database and store the task id in the MQ consumer; Step 2: The MQ consumer obtains the task ID message and adds an Excel export task management page; Step 3: The web service accesses the MQ consumer end to obtain the task message in the queue; Step 4: Query the total number of records to be exported based on the task "query condition", build a page object based on the number of sheet pages and put it into the blocking queue based on the link node; Step 5: Execute the thread pool sub-threads to determine whether all threads have executed abnormally. If so, exit the program and update the task status to "export exception"; Step 6: After all threads have executed without exception, export the data frame to an Excel file and update the database task status.

2. The asynchronous excel export processing method according to claim 1, characterized in that: The front-end triggers the export operation to save the export information into the database, and makes the following judgments when exporting the Excel table: Determine whether the number of tasks in the current "to be exported" and "exporting" states is greater than the maximum number of tasks. If so, the operation fails and the task id is saved to the MQ consumer end. Determine whether the exported data is empty; Determine whether the number of exported items is greater than the maximum number limit; If the above judgment is not satisfied, the response prompt information is returned; If satisfied, save the task information to the database and save the task ID to the MQ consumer.

3. The asynchronous excel export processing method according to claim 1, characterized in that: The MQ consumer obtains the task id message and makes the following judgment: According to the message, query the task information in the database to determine whether the task status is "pending export", otherwise delete the message; determine whether the current machine CPU and memory usage are lower than the preset percentage, if so, do not process this message and let the less busy machine process it.

4. The asynchronous excel export processing method according to claim 1, characterized in that: Getting task messages in the queue includes: Get the task ID message, query the database task ID details, and get the current server CPU and memory information.

5. The asynchronous excel export processing method according to claim 1, characterized in that: Said According to the task "Query Condition", the total number of records to be exported includes: Calculate the total number of sheet pages based on the data of each sheet page, calculate and set the object field values ​​at the corresponding positions, build a blocking queue based on link nodes of the object type, with a length equal to the total number of sheet pages, and set the object field values ​​at the corresponding positions according to the order.

6. The asynchronous excel export processing method according to claim 1, characterized in that: The step of constructing a sheetObject object according to the number of sheets and placing it into a blocking queue based on a link node includes: Create a countDownLatch object with the counter number as min(number of sheets, number of CPU cores of the current machine); Create a threadPoolExecutor thread pool with a size of min(number of sheet pages, number of CPU cores of the current machine).

7. The asynchronous excel export processing method according to claim 1, characterized in that: The determining whether all threads are abnormally executed includes: if a thread sets a flag, it means that someone manually clicks the "stop" operation, and the task status is changed to "stopped".

8. An asynchronous Excel export processing system, characterized in that: include: The first processing module is used to trigger the export operation in the foreground to save the export information to the database and store the task id in the MQ consumer end; The second processing module is used to enable the MQ consumer to obtain the task ID message, add an Excel export task management page, and enable the web service to access the MQ consumer to obtain the task message in the queue; The third processing module is used to query the total number of records to be exported according to the task "query condition", build a page object according to the number of sheet pages and put it into the blocking queue based on the link node; The fourth processing module is used to execute the thread pool sub-threads; determine whether all threads are executed abnormally, if abnormal, exit the program and update the task status to "export exception"; The fifth processing module is used to wait until all threads are executed without exception, export the data frame into an Excel file, and update the database task status.

9. An 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, an asynchronous Excel export processing method as described in any one of claims 1 to 8 is implemented.

10. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, an asynchronous Excel export processing method as described in any one of claims 1 to 8 is implemented.