简体   繁体   English

在 Spring 引导中执行后台进程时返回响应

[英]Returning Response while doing a background process in Spring boot

I am using spring boot to process a huge excel file.我正在使用 spring 引导来处理一个巨大的 excel 文件。 As it`sa time-consuming process, I want to return total number of records (as response) before my file is processed (after the file is processed I return the records as response).由于这是一个耗时的过程,我想在处理我的文件之前返回记录总数(作为响应)(在处理文件之后我返回记录作为响应)。 How can I do it?我该怎么做?

You can get total rows using Apache POI.您可以使用 Apache POI 获取总行数。


            File file = new File(filePath);
            XSSFWorkbook myExcelBook = new XSSFWorkbook(new FileInputStream(file));
            XSSFSheet myExcelSheet = myExcelBook.getSheetAt(0);
            XSSFRow headerRow = myExcelSheet.getRow(0);
            
            int totalRows = myExcelSheet.getPhysicalNumberOfRows();


totalRows returns total number of rows in the excel sheet. totalRows返回 excel 表中的总行数。

Edited已编辑

You can use two method calls for doing the same.您可以使用两个方法调用来做同样的事情。

String file="/path/to/file";
int count=getExcelRecordCount(file);
Records records= getActualExcelRecords(file); 

Sounds like spring batch would be a good idea.听起来像 spring 批处理是个好主意。 Your first step would check the # of rows in the file by importing the data & counting the number of instances of a unique identifier.您的第一步将通过导入数据并计算唯一标识符的实例数来检查文件中的行数。 Or if you know the delimiter (pipe |? comma,? ) & the # of expected columns, you could just count the # of delimeters in the file and divide that by the number of feilds (or columns) and you have the # of rows.或者,如果您知道分隔符(管道 |?逗号,?)和预期列的数量,您可以只计算文件中分隔符的数量,然后将其除以字段(或列)的数量,您就有了行。

In your next spring batch step you could do the rest of the processing.在您的下一个 spring 批处理步骤中,您可以执行 rest 的处理。

Hope this is helpful, Pioneer!希望这有帮助,先锋!

Have fun with Boot, its very opinionated imo.玩得开心 Boot,它非常固执己见。 but useful if you and spring both want the same things haha.但如果你和 spring 都想要同样的东西哈哈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM