java查询百万数据,通过异步的方式写入excel,没查询1000条数据,通过异步线程写入
时间: 2024-02-18 16:01:39 浏览: 26
Java查询百万数据并通过异步的方式写入Excel可以通过使用多线程或异步任务来实现。以下是使用异步任务的示例代码:
1. 首先,你需要定义一个实现了Callable接口的任务类,用来执行Excel写入操作。例如:
```
public class ExcelWriteTask implements Callable<File> {
private List<Data> dataList;
public ExcelWriteTask(List<Data> dataList) {
this.dataList = dataList;
}
@Override
public File call() throws Exception {
// 执行Excel写入操作,返回写入的文件
return ExcelUtils.write(dataList);
}
}
```
2. 在Controller中,定义一个异步查询并写入Excel的方法。例如:
```
@GetMapping("/export")
public Callable<ResponseEntity<byte[]>> exportExcel() {
Callable<List<Data>> task = () -> {
// 执行查询操作,返回需要导出的数据列表
return dataService.getDataList();
};
return () -> {
try {
List<Data> dataList = task.call();
int pageSize = 1000;
int total = dataList.size();
int pageCount = (total + pageSize - 1) / pageSize;
ExecutorService executorService = Executors.newFixedThreadPool(pageCount);
List<Future<File>> futureList = new ArrayList<>();
for (int i = 0; i < pageCount; i++) {
int fromIndex = i * pageSize;
int toIndex = Math.min((i + 1) * pageSize, total);
List<Data> subList = dataList.subList(fromIndex, toIndex);
ExcelWriteTask writeTask = new ExcelWriteTask(subList);
Future<File> future = executorService.submit(writeTask);
futureList.add(future);
}
List<File> fileList = new ArrayList<>();
for (Future<File> future : futureList) {
fileList.add(future.get());
}
File mergedFile = ExcelUtils.merge(fileList);
byte[] bytes = FileUtils.readFileToByteArray(mergedFile);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", mergedFile.getName());
return new ResponseEntity<>(bytes, headers, HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
return null;
}
};
}
```
3. 在异步查询并写入Excel的方法中,我们首先创建一个Callable对象,用来执行查询操作并返回需要导出的数据列表。然后,将查询操作和写入操作分离,将写入操作放在一个异步任务中执行。在异步任务中,我们将数据列表按照每1000条数据分成若干份,并创建一个线程池来执行写入操作。将每次写入操作的Future对象保存在一个List中,最后将所有的写入结果合并成一个Excel文件。最后,将导出的文件转换成byte数组,并返回一个ResponseEntity对象,以供前端下载。
这样,当用户请求异步查询并写入Excel时,不会阻塞主线程,而是异步执行查询和Excel写入操作,从而提高了系统的并发能力和响应速度。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)