需求分析
我们在下载报表时,有时候数据较多接口会超时。此时我们可以采用后台进程异步生成文件,生成后前端再下载。
流程图:
模拟代码:
@RequestMapping("/downExcel")
public String downExcel(String req){
String fileName= UUID.randomUUID().toString();
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
//异步生成文件
createExcel(req,fileName);
}
});
thread.start();
return fileName;
}
@RequestMapping("/downExcelStatus")
public String downExcelStatus(String fileName){
//下载文件
String status="";//获取文件生成状态
return status;
}
@RequestMapping("/downExcelReal")
public String downExcelReal(String fileName){
//下载文件
return fileName;
}
private void createExcel(String req, String fileName) {
//异步生成文件
//状态可以存入redis 供downExcelStatus查询
}