多線程異步導出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<String, Object> 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<String, Object> 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<String, Object> exportKQChannelStatistic(HttpServletRequest request) {

    Map<String, Object> result = new HashMap<String, Object>();
        result.put("success", true);

    Map<String, Object> params = new HashMap<String, Object>();

    // 獲取參數
    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;
    }
}

 

大概流程就是這樣。。。。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM