1.在启动类上添加异步注解
@EnableAsync
2.编写异步方法
异步方法建议单独写在一个独立的异步类中,如果和调用方法一同写到了一个类中,那么(使用注解的)该异步方法就变成了串行执行,异步失效!!!
package demo
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.concurrent.TimeUnit;
@Component
@Slf4j
public class AsyncInterface {
@Resource
private EpiExportRecordMapper epiSurveyReportRecordMapper;
//疫情报备导出的文件类型
private String TYPE = "YQBB";
/**
* 异步下载报备导出excel
*
* @param list
* @param currentUser
*/
@Async
public void asyncGetReportExcel(List<EpiSurveyReportEntity> list,UserVO currentUser) {
long startTime=System.nanoTime(); //获取开始时间
String fileName = "疫情报备";
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
//使用这个方法把Excel数据write到流中
EasyExcel.write(outputStream)
.head(EpiSurveyReportEntity.class)
.sheet(fileName)
.doWrite(list);
//将文件流转为byte[]
byte[] bytes = outputStream.toByteArray();
IAttachment api = EBaiZeApplication.findAPI(IAttachment.class);
AttachmentInfoEntity attachmentInfoEntity = api.saveFile(bytes, "xlsx", "");
//得到获取的文件id
String attachmentId = attachmentInfoEntity.getAttachmentId();
//操作用户信息
EpiExportRecordEntity epiReportRecord = new EpiExportRecordEntity();
epiReportRecord.setId(UUID.randomUUID().toString());
//设置文件下载类型
epiReportRecord.setType(TYPE);
epiReportRecord.setFileName(fileName+attachmentId);
epiReportRecord.setFileId(attachmentId);
epiReportRecord.setCreateUser(currentUser.getUserUuid());
epiSurveyReportRecordMapper.saveOrUpdateByEBaiZe(epiReportRecord);
long endTime=System.nanoTime(); //获取结束时间
log.info("异步任务程序运行时间:[{}] s",(endTime-startTime)/1000000000);
} catch (Exception e) {
e.printStackTrace();
}
}
3.编写调用方法
package demo
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@Api(tags = "")
@Slf4j
@RestController
@RequestMapping(value = "epi/admin/report", name = "接口")
public class EpiReportController {
@Resource
private EpiSurveyReportService epiSurveyReportService;
@Resource
private AsyncInterface asyncInterface;
@Resource
private EpiSurveyReportRecordService epiSurveyReportRecordService;
@ApiOperation(value = "导出报备数据(EasyExcel导出)")
@ResponseBody
@PostMapping(value = "exportReport", name = "导出报备数据")
public ResultBody exportReport(@RequestBody ReportSelectParam param) throws IOException{
//在这里执行异步方法,执行下载任务,并系统留痕
//获取开始时间
long startTime=System.nanoTime();
UserVO currentUser = EBaiZeApplication.findAPI(IUser.class).getCurrentUser();
List<EpiSurveyReportEntity> list = epiSurveyReportService.exportReportList(param);
asyncInterface.asyncGetReportExcel(list,currentUser);
//获取结束时间
long endTime=System.nanoTime();
log.info("导出报备数据任务程序运行时间:[{}] s",(endTime-startTime)/1000000000);
return ResultBody.succeed("ok");
}
}