多线程异步导出excel

先新建一个执行类

@Service
public class MultiService {
     private static final Logger logger = LoggerFactory.getLogger(MultiService .class);
     //创建一个线程池
     private ExecutorService executorService = Executors.newFixedThreadPool(10);    

    //导出结果列表的service,不是必须
    @Autowired
    private OemAsnyReportResultService oemAsnyReportResultService;

    // 数据来源的service
    @Autowired
    private CouponChannelStatisticService couponChannelStatisticService;

    public void couponStatisticExport(Map params) throws Exception {
        if (!params.containsKey("path") ) {
            //do something
            return;
        }

        //文件上传的路径
        String path = params.get("path").toString()
        // 异步导出
        executorService.execute(new KQChannelStatisticExport(oemAsnyReportResultService, couponChannelStatisticService, logger, params,path));
    }

}

 下面是要执行导出功能的线程

public class KQChannelStatisticExport implements Runnable {

     public KQChannelStatisticExport() {}

     public KQChannelStatisticExport(OemAsnyReportResultService oemAsnyReportResultService,
                    CouponChannelStatisticService couponChannelStatisticService, Logger logger, Map params,
                    String resourceServerPath) {
        this.oemAsnyReportResultService = oemAsnyReportResultService;
        this.couponChannelStatisticService = couponChannelStatisticService;this.logger = logger;
        this.params = params;
        this.resourceServerPath = resourceServerPath;
    }
 
    @Override
    public void run() {

    //执行导出excel的代码

//如果在这里获取session,有可能会报错【shiro报错There is no session with id [xxx] 】

//导出成功后可以在导出记录表新增一条记录
   oemAsnyReportResultService.save(); } }

 

 

创建一个controller

@Controller
public class KQChannelStatisticController{

    @Autowired
    private MultiService multiService ;

    /**
     * 统计导出
     */
    @RequestMapping("/exportKQChannelStatistic")
    @ResponseBody
    public Map exportKQChannelStatistic(HttpServletRequest request) {

    Map result = new HashMap();
        result.put("success", true);

    Map params = new HashMap();

    // 获取参数
    params = getStatParemMap(request);

    //这里是获取session里面的数据,写在controller里面防止报错,如果写在导出的线程中会报错【shiro报错There is no session with id [xxx] 】
    params = AsynExportExcelUtil.joinSonAccount(params);
    try {
       multiService.kqChannelStatisticExport(params);
    } catch (Exception e) {
       logger.error("统计数据导出异常", e);
       result.put("success", false);
    }
       return result;
    }
}

 

大概流程就是这样。。。。

 

转载于:https://www.cnblogs.com/chn58/p/5670013.html

你可能感兴趣的:(多线程异步导出excel)