先新建一個執行類
@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; } }
大概流程就是這樣。。。。