发送邮件含Excel,表头数据,记录如下,异步查询提高查询效率:
@RequestMapping("email")
public void email(HttpServletRequest request, HttpServletResponse response) throws IOException {
//加载邮件html模板long startTime =
String fileName = "scale-alarm.html";
URL url = this.getClass().getClassLoader().getResource("templates/" + fileName);
StringBuffer buffer = new StringBuffer();
BufferedInputStream bis = null;
try {
File f = new File(url.toURI());
FileInputStream fis = new FileInputStream(f);
bis = new BufferedInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String s = "";
while ((s = br.readLine()) != null) {
buffer.append(s).append("\n");
}
} catch (Exception e) {
System.out.println(e);
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
//查询接口数据
//存储一个公共的缓存用于拿到总体值
PlaceHolder ph = new PlaceHolder();
redis.setEx(ApiConstant.EMAIL_DAY.join("_", "total"), JSONObject.toJSONString(ph), 15L, TimeUnit.HOURS);
//查询表头数据
long startTime = System.currentTimeMillis();
CompletableFuture<String> f1 = CompletableFuture.supplyAsync(() -> {
try {
//查询Excel,之后查询表头
String sdate = null;
String edate = null;
emailTitleData.getYdgcsxdje(sdate, edate);
} catch (Exception e) {
logger.error("runTitleNum->Exception:{}", e);
}
return "";
});
CompletableFuture<String> f2 = CompletableFuture.supplyAsync(() -> {
try {
//查询Excel,之后查询表头
try {
homePageService.queryDaySalesYear(DateUtils.getYearDay(), DateUtils.getYearLast());
} catch (ParseException e) {
e.printStackTrace();
}
} catch (Exception e) {
logger.error("runTitleNum->Exception:{}", e);
}
return "";
});
String headMsg = EmailTemplate.headMsg;
CompletableFuture.allOf(f1, f2).join();
String ph1 = redis.get(ApiConstant.EMAIL_DAY.join("_", "total"));
PlaceHolder phNew = JSONObject.parseObject(ph1, PlaceHolder.class);
Object[] headParams = {//自己的参数};
String headMsgNew = MessageFormat.format(headMsg, headParams);
//查询Excel,之后查询表头
long futureSqTime = System.currentTimeMillis();
CompletableFuture<List<DataTemplate>> futureSq = CompletableFuture.supplyAsync(() -> {
try {
return emailExcelData.getSqExcel();
} catch (Exception e) {
logger.error("getSqExcel->Exception:", e);
}
return null;
});
CompletableFuture<List<DataTemplate>> futureZr = CompletableFuture.supplyAsync(() -> {
try {
return emailExcelData.getZrSqtpExcel();
} catch (Exception e) {
logger.error("getZrSqtpExcel->Exception:", e);
}
return null;
});
CompletableFuture<List<DataTemplate>> futureYd = CompletableFuture.supplyAsync(() -> {
try {
return emailExcelData.getYdExcel();
} catch (Exception e) {
logger.error("getYdExcel->Exception:", e);
}
return null;
});
CompletableFuture.allOf(futureSq, futureZr, futureYd).join();
System.out.println("接口EmailController耗时futureSq:"+String.valueOf(System.currentTimeMillis()-futureSqTime));
long statrTimeTd = System.currentTimeMillis();
String headerzrsq = "<td>列1</td><td>列2</td><td>列3</td><td>列4</td><td>列5</td><td>列6</td><td>列7</td><td>列8</td>";
StringBuilder linesBufferzrsq = new StringBuilder();
try {
List<DataTemplate> listSq = futureZr.get();
listSq.forEach(dataFactory -> {
linesBufferzrsq.append("<tr><td>" + dataFactory.getStr1() +
"</td><td>" + dataFactory.getStr2() +
"</td><td>" + dataFactory.getStr3() +
"</td><td>" + dataFactory.getStr4() +
"</td><td>" + dataFactory.getStr5() +
"</td><td>" + dataFactory.getStr6() +
"</td><td>" + dataFactory.getStr7() +
"</td><td>" + dataFactory.getStr8() + "</td></tr>");
});
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
String header = "<td>列1</td><td>列2</td><td>列3</td><td>列4</td><td>列5</td><td>列6</td><td>列7</td><td>列8</td>";
StringBuilder linesBuffer = new StringBuilder();
List<DataTemplate> list3 = null;
try {
list3 = futureYd.get();
list3.forEach(dataFactory -> {
linesBuffer.append("<tr><td>" + dataFactory.getStr1() +
"</td><td>" + dataFactory.getStr2() +
"</td><td>" + dataFactory.getStr3() +
"</td><td>" + dataFactory.getStr4() +
"</td><td>" + dataFactory.getStr5() +
"</td><td>" + dataFactory.getStr6() +
"</td><td>" + dataFactory.getStr7() +
"</td><td>" + dataFactory.getStr8() + "</td></tr>");
});
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
String emailHeadColor = "#FF0000";//颜色
Object[] params = {//自己参数 };
//填充html模板中的五个参数
String htmlText = MessageFormat.format(buffer.toString(), params);
//改变表格样式
htmlText = htmlText.replaceAll("<td>", "<td style=\"padding:6px 10px; line-height: 150%;border: 1px solid #eee; width: 100px; word-wrap: break-word;\">");
// htmlText = htmlText.replaceAll("<tr>", "<tr style=\"border-bottom: 1px solid #eee; color:#666;\">");
try {
OutputStream out = response.getOutputStream();
byte[] tag_byte = htmlText.getBytes();
out.write(tag_byte);
out.close();
} catch (Exception e) {
System.out.println(e);
}
}