三步教你异步调用@Async的使用

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");
    }


}

学习链接1
学习链接2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三横同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值