From 045980b7383cd521b43caa61ae9e1160238d6479 Mon Sep 17 00:00:00 2001 From: chengshiwen Date: Sat, 6 Feb 2021 21:29:46 +0800 Subject: [PATCH] [1.3.5-prepare][Fix][Mail] Fix exception: send mail failed under default mail config --- .../alert/utils/Constants.java | 4 ++-- .../alert/utils/MailUtils.java | 19 ++++++++++++------- .../server/worker/task/sql/SqlTask.java | 4 ++++ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/Constants.java b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/Constants.java index 540b1a9409..2fc4749a55 100644 --- a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/Constants.java +++ b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/Constants.java @@ -28,8 +28,6 @@ public class Constants { */ public static final String ALERT_PROPERTIES_PATH = "/alert.properties"; - public static final String DATA_SOURCE_PROPERTIES_PATH = "/dao/data_source.properties"; - public static final String SINGLE_SLASH = "/"; /** @@ -41,6 +39,8 @@ public class Constants { public static final String MESSAGE = "message"; + public static final String MAIL_ENABLED = "mail.enabled"; + public static final String MAIL_PROTOCOL = "mail.protocol"; public static final String MAIL_SERVER_HOST = "mail.server.host"; diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/MailUtils.java b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/MailUtils.java index bb565f5133..98f7b75769 100644 --- a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/MailUtils.java +++ b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/MailUtils.java @@ -51,11 +51,7 @@ public class MailUtils { public static final String MAIL_PASSWD = PropertyUtils.getString(Constants.MAIL_PASSWD); - public static final Boolean MAIL_USE_START_TLS = PropertyUtils.getBoolean(Constants.MAIL_SMTP_STARTTLS_ENABLE); - - public static final Boolean MAIL_USE_SSL = PropertyUtils.getBoolean(Constants.MAIL_SMTP_SSL_ENABLE); - - public static final String xlsFilePath = PropertyUtils.getString(Constants.XLS_FILE_PATH,"/tmp/xls"); + public static final String XLS_FILE_PATH = PropertyUtils.getString(Constants.XLS_FILE_PATH, "/tmp/xls"); public static final String STARTTLS_ENABLE = PropertyUtils.getString(Constants.MAIL_SMTP_STARTTLS_ENABLE); @@ -89,6 +85,15 @@ public class MailUtils { */ public static Map sendMails(Collection receivers, Collection receiversCc, String title, String content, String showType) { Map retMap = new HashMap<>(); + + // if mail is default config, no need to process + if (StringUtils.isEmpty(MAIL_SERVER_HOST) || "xxx.xxx.com".equals(MAIL_SERVER_HOST)) { + retMap.put(Constants.MAIL_ENABLED, false); + retMap.put(Constants.STATUS, true); + return retMap; + } + + retMap.put(Constants.MAIL_ENABLED, true); retMap.put(Constants.STATUS, false); // if there is no receivers && no receiversCc, no need to process @@ -260,13 +265,13 @@ public class MailUtils { part1.setContent(partContent, Constants.TEXT_HTML_CHARSET_UTF_8); // set attach file MimeBodyPart part2 = new MimeBodyPart(); - File file = new File(xlsFilePath + Constants.SINGLE_SLASH + title + Constants.EXCEL_SUFFIX_XLS); + File file = new File(XLS_FILE_PATH + Constants.SINGLE_SLASH + title + Constants.EXCEL_SUFFIX_XLS); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } // make excel file - ExcelUtils.genExcelFile(content,title,xlsFilePath); + ExcelUtils.genExcelFile(content,title,XLS_FILE_PATH); part2.attachFile(file); part2.setFileName(MimeUtility.encodeText(title + Constants.EXCEL_SUFFIX_XLS,Constants.UTF_8,"B")); diff --git a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/worker/task/sql/SqlTask.java b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/worker/task/sql/SqlTask.java index c742b7efa3..b07c556240 100644 --- a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/worker/task/sql/SqlTask.java +++ b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/worker/task/sql/SqlTask.java @@ -49,6 +49,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; +import static org.apache.dolphinscheduler.alert.utils.Constants.MAIL_ENABLED; import static org.apache.dolphinscheduler.common.Constants.*; import static org.apache.dolphinscheduler.common.enums.DbType.HIVE; /** @@ -454,6 +455,9 @@ public class SqlTask extends AbstractTask { receiversCcList, title, content, ShowType.valueOf(showTypeName).getDescp()); if(!(boolean) mailResult.get(STATUS)){ throw new RuntimeException("send mail failed!"); + }else if(!(boolean) mailResult.get(MAIL_ENABLED)){ + logger.info("mail info : {} {}", title, content); + logger.warn("mail wasn't sent since the mail config isn't set"); } }else{ logger.error("showType: {} is not valid " ,showTypeName);