有些日子没有写博客了,恰好最近一直在研究写一个消息中心。
所谓消息中心,是指通过多个平台进行推送消息,比如我们一些部署在服务器上面的项目如果想了解它的运行状态,(例如:报错信息)。最简单的莫过于邮件发送,即通过邮件发送异常日志到你的邮箱。但是这样做并不人性化,邮件通知并不会及时让我们看到。所以我就考虑通过全方位的通知进行提醒,例如:微信、QQ、邮箱、钉钉、短信甚至……智能音箱。
整体项目还没开始写,不过我目前正在做的项目里面涉及到了部分消息中心的功能~先摘出来,后期慢慢整合。
首先,来看一下模板邮件的东西,这边使用的是Hutool的邮件服务。
模板邮件
1.准备工作
前期发邮件的步骤直接去看本博客的《利用Hutool邮件工具》(传送门)
2.邮件模板
在resources/templates
目录下新建mail-template.ftl
。
mail-template.ftl
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="email code">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<!--邮箱模板-->
<body>
<div style="background-color:#ECECEC; padding: 35px;">
<table cellpadding="0" align="center"
style="width: 800px;height: 100%; margin: 0px auto; text-align: left; position: relative; border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; font-size: 13px; font-family:宋体, 黑体; line-height: 1.5; box-shadow: rgb(153, 153, 153) 0px 0px 5px; border-collapse: collapse; background-position: initial initial; background-repeat: initial initial;background:#fff;">
<tbody>
<tr>
<th valign="middle"
style="height: 25px; line-height: 25px; padding: 15px 35px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: RGB(148,0,211); background-color: RGB(148,0,211); border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px;">
<font face="宋体" size="5" style="color: rgb(255, 255, 255); ">XX项目-XXX业务</font>
</th>
</tr>
<tr>
<td style="word-break:break-all">
<div style="padding:25px 35px 39px; background-color:#fff;opacity:0.8;">
<h2 style="margin: 5px 0px; ">
<font color="#333333" style="line-height: 20px; ">
<font style="line-height: 22px; " size="3">
尊敬的管理员,请查收异常信息:</font>
</font>
</h2>
<!-- 中文 -->
<table border="2"; bordercolor="black"; cellpadding="3px">
<tr>
<td>告警主机</td>
<td bgcolor="#FF3333">{0}</td>
</tr>
<tr>
<td>告警时间</td>
<td>{1}</td>
</tr>
<tr>
<td>告警信息</td>
<td bgcolor="#FF3333">{2}</td>
</tr>
<tr>
<td>当前状态</td>
<td>{3}</td>
</tr>
</table>
<br>
<div style="width:100%;margin:0 auto;">
<div
style="padding:10px 10px 0;border-top:1px solid #ccc;color:#757575;margin-bottom:20px;line-height:1.3em;font-size:12px;">
<p>XXX网络科技有限公司</p>
<p>此为系统邮件,请勿回复!</p>
<!--<p>©***</p>-->
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
PS:上面html中的占位符可以自行修改数量,需要多少就加多少。
2、邮件模板处理工具:
EmailTemplateUtil
package com.demo.utils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.MessageFormat;
import java.util.List;
/**
* @Author zf
* @ClassName EmailTemplateUtil.java
* @ProjectName EmailDemo
*/
public class EmailTemplateUtil {
/**
* @Description 读取邮件模板替换模板中的信息
* @param content 模板内容
* @param templatePath 模板路径
* @return
*/
public static String buildContent(List content, String templatePath) {
//加载邮件html模板,获取Resource目录下的模板文件。
Resource resource = new ClassPathResource(templatePath);
InputStream inputStream = null;
BufferedReader fileReader = null;
StringBuffer buffer = new StringBuffer();
String line = "";
try {
inputStream = resource.getInputStream();
fileReader = new BufferedReader(new InputStreamReader(inputStream));
while ((line = fileReader.readLine()) != null) {
buffer.append(line);
}
} catch (Exception e) {
return "发送邮件读取模板失败{}";
} finally {
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//替换html模板中的参数
return MessageFormat.format(buffer.toString(), content.toArray());
}
}
PS:处理工具会接收请求参数,然后遍历list中的每一个元素替换进邮件模板当中,所以list中的元素数量必须要和邮件模板中保持一致。
3、测试方法:
测试类:
public static void main(String[] args) {
//模板邮件参数列表
ArrayList<String> list = new ArrayList<>();
//告警服务器
list.add("192.168.0.1");
//告警发生时间
list.add(DateUtil.now());
//告警信息
status = "false";
//当前运行状态
list.add(status + ",当前监控服务:" + serverName +"。");
list.add("服务发生异常,请尽快排查!");
MailUtil.send("收件人邮箱", "服务状态监控!", EmailTemplateUtil.buildContent(list,"/templates/mail-template.ftl") , true);
}
PS:到这一步正常来说你的邮箱已经可以正常收到美化以后的模板邮件信息了,其他的模板也是按照这种方式自己去添加。
定时任务
定时任务这里也是引用了Hutool工具。
在resources/templates
目录下新建cron.setting
文件。
1、编写配置类:
cron.setting
# 定时任务
[com.demo.server.serverstatus]
StatusMonitorServer.test = */5 * * * *
PS:配置文件里面的
com.demo.server.serverstatus
是指具体包名,StatusMonitorServer
是指具体类名,test
是指具体方法。*/5 * * * *
是指每5分钟执行一次定时任务。
2、开始使用:
让StatusMonitorServer
类去实现(implements)ApplicationRunner
接口的run
方法。
public void test(){
System.out.println("定时任务开始!"+ DateUtil.now());
}
@Override
public void run(ApplicationArguments args) {
//启动定时任务
CronUtil.start();
}
这样,当项目运行的时候定时任务就会被执行,然后读取配置类,在指定的时间(5分钟)去执行对应的方法(test)。
当然,你也可以继续完善这个功能,比如在controller接口里面单独搞一个开始(CronUtil.start();
)、停止(CronUtil.stop();
)定时任务的接口。