feat: add email support for send msg when TP' CONFIG or ALARM are triggered

pull/923/head
baymax55 3 years ago
parent 59cb0b28c3
commit 309b122bbf

@ -36,6 +36,10 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
</dependency>
</dependencies>
<build>

@ -19,6 +19,7 @@ package cn.hippo4j.message.config;
import cn.hippo4j.message.api.NotifyConfigBuilder;
import cn.hippo4j.message.platform.DingSendMessageHandler;
import cn.hippo4j.message.platform.EmailSendMessageHandler;
import cn.hippo4j.message.platform.LarkSendMessageHandler;
import cn.hippo4j.message.platform.WeChatSendMessageHandler;
import cn.hippo4j.message.service.AlarmControlHandler;
@ -57,4 +58,9 @@ public class MessageConfiguration {
public SendMessageHandler weChatSendMessageHandler() {
return new WeChatSendMessageHandler();
}
@Bean
public EmailSendMessageHandler emailSendMessageHandler() {
return new EmailSendMessageHandler();
}
}

@ -35,5 +35,10 @@ public enum NotifyPlatformEnum {
/**
* WECHAT
*/
WECHAT
WECHAT,
/**
* Email
*/
Email
}

@ -0,0 +1,174 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.hippo4j.message.platform;
import cn.hippo4j.common.toolkit.Assert;
import cn.hippo4j.common.toolkit.FileUtil;
import cn.hippo4j.common.toolkit.JSONUtil;
import cn.hippo4j.common.toolkit.Singleton;
import cn.hippo4j.message.dto.NotifyConfigDTO;
import cn.hippo4j.message.enums.NotifyPlatformEnum;
import cn.hippo4j.message.platform.base.AbstractRobotSendMessageHandler;
import cn.hippo4j.message.platform.base.RobotMessageActualContent;
import cn.hippo4j.message.platform.base.RobotMessageExecuteDTO;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.nio.charset.Charset;
import java.util.*;
import static cn.hippo4j.message.platform.constant.EmailAlarmConstants.Email_ALARM_TITLE;
import static cn.hippo4j.message.platform.constant.EmailAlarmConstants.Email_NOTICE_TITLE;
/**
* Send Email notification message.
*/
@Slf4j
public class EmailSendMessageHandler extends AbstractRobotSendMessageHandler {
@Override
public String getType() {
return NotifyPlatformEnum.Email.name();
}
@Override
protected RobotMessageActualContent buildMessageActualContent() {
String emailAlarmTxtKey = "message/robot/dynamic-thread-pool/email-alarm.txt";
String emailConfigTxtKey = "message/robot/dynamic-thread-pool/email-config.txt";
return RobotMessageActualContent.builder()
.receiveSeparator(", @")
.changeSeparator(" -> ")
.alarmMessageContent(Singleton.get(emailAlarmTxtKey, () -> FileUtil.readUtf8String(emailAlarmTxtKey)))
.configMessageContent(Singleton.get(emailConfigTxtKey, () -> FileUtil.readUtf8String(emailConfigTxtKey)))
.build();
}
@Override
protected void execute(RobotMessageExecuteDTO robotMessageExecuteDTO) {
NotifyConfigDTO notifyConfig = robotMessageExecuteDTO.getNotifyConfig();
String content = robotMessageExecuteDTO.getText();
String receives = notifyConfig.getReceives();
String secretKey = notifyConfig.getSecretKey();
String[] recipients = receives.split(",");
MailAccount mailAccount = JSONUtil.parseObject(secretKey, MailAccount.class);
Assert.isTrue(mailAccount != null, "mailAccount is null");
mailAccount.setUser(mailAccount.getFrom());
String subject = Objects.equals(notifyConfig.getType(), "CONFIG") ? Email_NOTICE_TITLE : Email_ALARM_TITLE;
try {
MimeMessage mimeMessage = buildMsg(mailAccount, recipients, subject, content);
Transport.send(mimeMessage);
} catch (Exception ex) {
log.error("Email failed to send message", ex);
}
}
private MimeMessage buildMsg(MailAccount mailAccount, String[] recipients, String subject, String content) throws MessagingException {
UserPassAuthenticator authenticator = new UserPassAuthenticator(mailAccount.getUser(), mailAccount.getPass());
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp");
properties.put("mail.smtp.host", mailAccount.getHost());
properties.put("mail.smtp.port", mailAccount.getPort());
properties.put("mail.smtp.auth", "true");
Session session = Session.getInstance(properties, authenticator);
MimeMessage msg = new MimeMessage(session);
// 发件人
String from = mailAccount.getFrom();
msg.setFrom(from);
// 标题
msg.setSubject(subject);
// 发送时间
msg.setSentDate(new Date());
// 内容和附件
MimeMultipart mimeMultipart = new MimeMultipart();
MimeBodyPart body = new MimeBodyPart();
body.setContent(content, "text/html; charset=" + Charset.defaultCharset());
mimeMultipart.addBodyPart(body);
msg.setContent(mimeMultipart);
// 收件人
List<Address> addressList = new ArrayList<>(recipients.length);
for (String recipient : recipients) {
Address to = new InternetAddress(recipient);
addressList.add(to);
}
Address[] addresses = addressList.toArray(new Address[0]);
msg.setRecipients(MimeMessage.RecipientType.TO, addresses);
return msg;
}
@Data
private static class MailAccount {
/**
* SMTP
*/
private String host;
/**
* SMTP
*/
private Integer port;
/**
*
*/
private Boolean auth;
/**
*
*/
private String user;
/**
*
*/
private String pass;
/**
*
*/
private String from;
}
private static class UserPassAuthenticator extends Authenticator {
private final String user;
private final String pass;
/**
*
*
* @param user
* @param pass
*/
public UserPassAuthenticator(String user, String pass) {
this.user = user;
this.pass = pass;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(this.user, this.pass);
}
}
}

@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.hippo4j.message.platform.constant;
/**
* Email alarm constants.
*/
public class EmailAlarmConstants {
/**
* Thread Pool Alert Notification Title
*/
public static final String Email_ALARM_TITLE = "动态线程池告警";
/**
* Thread pool parameter change notification title
*/
public static final String Email_NOTICE_TITLE = "动态线程池通知";
}

@ -0,0 +1,20 @@
<font color='#FF0000'>[警报] </font>%s - 动态线程池运行告警(%s <br/>
线程池ID<font color='warning'>%s</font> <br/>
应用名称:<font color='warning'>%s</font> <br/>
应用实例:%s <br/>
核心线程数:%s <br/>
最大线程数:%s <br/>
当前线程数:%s <br/>
活跃线程数:%s <br/>
同存最大线程数:%s <br/>
线程池任务总量:%s <br/>
队列类型:%s <br/>
队列容量:%s <br/>
队列元素个数:%s <br/>
队列剩余个数:%s <br/>
拒绝策略:%s <br/>
拒绝策略执行次数:<font color='#FF0000'>%s</font> ${timout-content} <br/>
OWNER<@%s> <br/>
提示:%d 分钟内此线程池不会重复告警(可配置)<br/>
<b> 播报时间:%s </b>

@ -0,0 +1,17 @@
<font color='info'>[通知] </font>%s - 动态线程池参数变更 <br/>
线程池ID<font color='warning'>%s</font> <br/>
应用名称:<font color='warning'>%s</font> <br/>
应用实例:%s <br/>
核心线程数:%s <br/>
最大线程数:%s <br/>
核心线程超时:%s <br/>
线程存活时间:%s <br/>
执行超时时间:%s <br/>
队列类型:%s <br/>
队列容量:%s <br/>
AGO 拒绝策略:%s <br/>
NOW 拒绝策略:%s <br/>
OWNER<@%s> <br/>
提示:动态线程池配置变更实时通知(无限制) <br/>
<b> 播报时间:%s </b>

@ -95,6 +95,7 @@
<license-maven-plugin.version>3.0</license-maven-plugin.version>
<spotless-maven-plugin.version>2.22.1</spotless-maven-plugin.version>
<maven-checkstyle-plugin.version>3.1.0</maven-checkstyle-plugin.version>
<javax.mail.version>1.6.2</javax.mail.version>
</properties>
<dependencyManagement>
@ -126,6 +127,11 @@
<artifactId>netty-all</artifactId>
<version>${netty.version}</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>${javax.mail.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Loading…
Cancel
Save