页面添加下载任务,后台开启线程异步下载,下载完成更新任务状态


@RequestMapping("addExportTaskAndExportExcel")
@ResponseBody
public MessageBean addExportTaskAndExportExcel(HttpServletRequest request) {
MessageBean messageBean = null;
String topicId = request.getParameter("topicId");
String exportType = request.getParameter("exportType");
String postStartTime = request.getParameter("postStartTime");
String postEndTime = request.getParameter("postEndTime");
String spiderStartTime = request.getParameter("spiderStartTime");
String spiderEndTime = request.getParameter("spiderEndTime");
String exportArticleContentFlag = request.getParameter("exportArticleContentFlag");
try {
TaskBean taskBean = new TaskBean();
String name = "【主题文章导出】";
taskBean.setType((short) 2); //主题文章导出
taskBean.setStatus((short) 1); //处理中
taskBean.setName(name);
taskBean.setRemark(null);
taskBean.setResult(null);

TopicArticleExportTaskBean bean = new TopicArticleExportTaskBean();
bean.setTopicId(topicId);
bean.setExportType(exportType);
bean.setPostStartTime(postStartTime);
bean.setPostEndTime(postEndTime);
bean.setSpiderStartTime(spiderStartTime);
bean.setSpiderEndTime(spiderEndTime);
bean.setExportArticleContentFlag(exportArticleContentFlag);

String contentJson = JsonUtil.toJson(bean);
log.info("===contentJson===" +contentJson + "===");

taskBean.setContent(contentJson);

Integer result = topicSupport.addExportTask(taskBean);
if(result!=null) {
messageBean = buildMessage(MESSAGE_TYPE_SUCCESS, "成功");
} else {
messageBean = buildMessage(MESSAGE_TYPE_ERROR, "失败");
}
String path= request.getRealPath("/");

Thread t= new Thread(new ExportTask(path, bean, result));
t.setDaemon(false);
t.start();

} catch (Exception e) {
log.error("===addExportTaskAndExportExcel==="+e.getMessage(), e);
messageBean = buildMessage(MESSAGE_TYPE_ERROR, "失败");
}
return messageBean;
}

private class ExportTask implements Runnable {
private String path;
private TopicArticleExportTaskBean bean;
private Integer result;

public ExportTask(String path, TopicArticleExportTaskBean bean, Integer result){
this.path = path;
this.bean = bean;
this.result = result;
}

@Override
public void run() {
String topicId =bean.getTopicId();
log.info("===result===" + result + "===topicId===" + bean.getTopicId() + "===postStartTime===" + bean.getPostStartTime()  + "===postEndTime===" + bean.getPostEndTime()  + "===spiderStartTime===" + bean.getSpiderStartTime()  + "===spiderEndTime===" + bean.getSpiderEndTime() + "===");
List<ArticleBean> artList = topicSupport.queryTopicArticleExportList(bean.getTopicId(),bean.getPostStartTime(), bean.getPostEndTime(), bean.getSpiderStartTime(), bean.getSpiderEndTime());

try {
if(bean.getExportArticleContentFlag()!=null && "1".equals(bean.getExportArticleContentFlag())){
if(CollectionUtils.isNotEmpty(artList)){
for(ArticleBean articleBean : artList){
String content = articleService.findArticle(articleBean.getGuid()).getContent();
content = (null == content?"":content);
articleBean.setContent(content);
}
}
}
} catch (ArticleServiceException e1) {
log.error(e1.getMessage(), e1);
}

Map<String,Object> beans = new HashMap<String,Object>();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
beans.put("dateFormat", dateFormat);
beans.put("resultList", artList);
beans.put("tag", SpringContextUtil.getBean(TagUtil.class));
beans.put("constUtil", SpringContextUtil.getBean(ConstUtil.class));
XLSTransformer transformer = new XLSTransformer();
transformer.markAsFixedSizeCollection("resultList");

String templateFile = path+"//WEB-INF/classes/template/topicArticleReportTemplate.xls";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");  
String dateTime = sdf.format(new Date()) ;
String fileName = topicId + "_" + dateTime + ".xls";


String tagerPath = topicArticleExportFilePath + topicId + "/";
File targetFile = new File(tagerPath);
if(!targetFile.exists()){
targetFile.mkdirs();
targetFile = null;
}

String reportFile = topicArticleExportFilePath  +topicId + "/" + fileName;

TaskBean updateTaskBean = new TaskBean();
updateTaskBean.setId(result);
updateTaskBean.setUpdateTime(new Date());
try {
transformer.transformXLS(templateFile, beans, reportFile);
updateTaskBean.setStatus((short)2); //成功
updateTaskBean.setResult(fileName);
Integer updResult = topicSupport.updateExportTask(updateTaskBean);
log.info("===id==="+updateTaskBean.getId() + "===status==="+updateTaskBean.getStatus() + "===fileName==="+updateTaskBean.getResult() + "===updResult==="+updResult);

} catch (ParsePropertyException e) {
log.error("===addExportTaskAndExportExcel===ParsePropertyException==="+e.getMessage(), e);
updateTaskBean.setStatus((short)3); //失败
updateTaskBean.setResult("ParsePropertyException:"+e.getMessage());
try {
topicSupport.updateExportTask(updateTaskBean);
} catch (TaskServiceException e1) {
e1.printStackTrace();
}
} catch (InvalidFormatException e) {
log.error("===addExportTaskAndExportExcel===InvalidFormatException==="+e.getMessage(), e);
updateTaskBean.setStatus((short)3); //失败
updateTaskBean.setResult("InvalidFormatException:"+e.getMessage());
try {
topicSupport.updateExportTask(updateTaskBean);
} catch (TaskServiceException e1) {
e1.printStackTrace();
}

} catch (IOException e) {
log.error("===addExportTaskAndExportExcel===IOException==="+e.getMessage(), e);
updateTaskBean.setStatus((short)3); //失败
updateTaskBean.setResult("IOException:"+e.getMessage());
try {
topicSupport.updateExportTask(updateTaskBean);
} catch (TaskServiceException e1) {
e1.printStackTrace();
}
} catch (TaskServiceException e) {
log.error("===updateExportTask===TaskServiceException==="+e.getMessage(), e);
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值