
正文
多线程异步导出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;
}
}
大概流程就是这样。。。。








