提交 | 用户 | 时间
|
e7c126
|
1 |
package com.xxl.job.admin.core.alarm.impl; |
H |
2 |
|
|
3 |
import com.xxl.job.admin.core.alarm.JobAlarm; |
|
4 |
import com.xxl.job.admin.core.conf.XxlJobAdminConfig; |
|
5 |
import com.xxl.job.admin.core.model.XxlJobGroup; |
|
6 |
import com.xxl.job.admin.core.model.XxlJobInfo; |
|
7 |
import com.xxl.job.admin.core.model.XxlJobLog; |
|
8 |
import com.xxl.job.admin.core.util.I18nUtil; |
|
9 |
import com.xxl.job.core.biz.model.ReturnT; |
|
10 |
import org.slf4j.Logger; |
|
11 |
import org.slf4j.LoggerFactory; |
|
12 |
import org.springframework.mail.javamail.MimeMessageHelper; |
|
13 |
import org.springframework.stereotype.Component; |
|
14 |
|
|
15 |
import javax.mail.internet.MimeMessage; |
|
16 |
import java.text.MessageFormat; |
|
17 |
import java.util.Arrays; |
|
18 |
import java.util.HashSet; |
|
19 |
import java.util.Set; |
|
20 |
|
|
21 |
/** |
|
22 |
* job alarm by email |
|
23 |
* |
|
24 |
* @author xuxueli 2020-01-19 |
|
25 |
*/ |
|
26 |
@Component |
|
27 |
public class EmailJobAlarm implements JobAlarm { |
|
28 |
private static Logger logger = LoggerFactory.getLogger(EmailJobAlarm.class); |
|
29 |
|
|
30 |
/** |
|
31 |
* fail alarm |
|
32 |
* |
|
33 |
* @param jobLog |
|
34 |
*/ |
|
35 |
@Override |
|
36 |
public boolean doAlarm(XxlJobInfo info, XxlJobLog jobLog){ |
|
37 |
boolean alarmResult = true; |
|
38 |
|
|
39 |
// send monitor email |
|
40 |
if (info!=null && info.getAlarmEmail()!=null && info.getAlarmEmail().trim().length()>0) { |
|
41 |
|
|
42 |
// alarmContent |
|
43 |
String alarmContent = "Alarm Job LogId=" + jobLog.getId(); |
|
44 |
if (jobLog.getTriggerCode() != ReturnT.SUCCESS_CODE) { |
|
45 |
alarmContent += "<br>TriggerMsg=<br>" + jobLog.getTriggerMsg(); |
|
46 |
} |
|
47 |
if (jobLog.getHandleCode()>0 && jobLog.getHandleCode() != ReturnT.SUCCESS_CODE) { |
|
48 |
alarmContent += "<br>HandleCode=" + jobLog.getHandleMsg(); |
|
49 |
} |
|
50 |
|
|
51 |
// email info |
|
52 |
XxlJobGroup group = XxlJobAdminConfig.getAdminConfig().getXxlJobGroupDao().load(Integer.valueOf(info.getJobGroup())); |
|
53 |
String personal = I18nUtil.getString("admin_name_full"); |
|
54 |
String title = I18nUtil.getString("jobconf_monitor"); |
|
55 |
String content = MessageFormat.format(loadEmailJobAlarmTemplate(), |
|
56 |
group!=null?group.getTitle():"null", |
|
57 |
info.getId(), |
|
58 |
info.getJobDesc(), |
|
59 |
alarmContent); |
|
60 |
|
|
61 |
Set<String> emailSet = new HashSet<String>(Arrays.asList(info.getAlarmEmail().split(","))); |
|
62 |
for (String email: emailSet) { |
|
63 |
|
|
64 |
// make mail |
|
65 |
try { |
|
66 |
MimeMessage mimeMessage = XxlJobAdminConfig.getAdminConfig().getMailSender().createMimeMessage(); |
|
67 |
|
|
68 |
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); |
|
69 |
helper.setFrom(XxlJobAdminConfig.getAdminConfig().getEmailFrom(), personal); |
|
70 |
helper.setTo(email); |
|
71 |
helper.setSubject(title); |
|
72 |
helper.setText(content, true); |
|
73 |
|
|
74 |
XxlJobAdminConfig.getAdminConfig().getMailSender().send(mimeMessage); |
|
75 |
} catch (Exception e) { |
|
76 |
logger.error(">>>>>>>>>>> xxl-job, job fail alarm email send error, JobLogId:{}", jobLog.getId(), e); |
|
77 |
|
|
78 |
alarmResult = false; |
|
79 |
} |
|
80 |
|
|
81 |
} |
|
82 |
} |
|
83 |
|
|
84 |
return alarmResult; |
|
85 |
} |
|
86 |
|
|
87 |
/** |
|
88 |
* load email job alarm template |
|
89 |
* |
|
90 |
* @return |
|
91 |
*/ |
|
92 |
private static final String loadEmailJobAlarmTemplate(){ |
|
93 |
String mailBodyTemplate = "<h5>" + I18nUtil.getString("jobconf_monitor_detail") + ":</span>" + |
|
94 |
"<table border=\"1\" cellpadding=\"3\" style=\"border-collapse:collapse; width:80%;\" >\n" + |
|
95 |
" <thead style=\"font-weight: bold;color: #ffffff;background-color: #ff8c00;\" >" + |
|
96 |
" <tr>\n" + |
|
97 |
" <td width=\"20%\" >"+ I18nUtil.getString("jobinfo_field_jobgroup") +"</td>\n" + |
|
98 |
" <td width=\"10%\" >"+ I18nUtil.getString("jobinfo_field_id") +"</td>\n" + |
|
99 |
" <td width=\"20%\" >"+ I18nUtil.getString("jobinfo_field_jobdesc") +"</td>\n" + |
|
100 |
" <td width=\"10%\" >"+ I18nUtil.getString("jobconf_monitor_alarm_title") +"</td>\n" + |
|
101 |
" <td width=\"40%\" >"+ I18nUtil.getString("jobconf_monitor_alarm_content") +"</td>\n" + |
|
102 |
" </tr>\n" + |
|
103 |
" </thead>\n" + |
|
104 |
" <tbody>\n" + |
|
105 |
" <tr>\n" + |
|
106 |
" <td>{0}</td>\n" + |
|
107 |
" <td>{1}</td>\n" + |
|
108 |
" <td>{2}</td>\n" + |
|
109 |
" <td>"+ I18nUtil.getString("jobconf_monitor_alarm_type") +"</td>\n" + |
|
110 |
" <td>{3}</td>\n" + |
|
111 |
" </tr>\n" + |
|
112 |
" </tbody>\n" + |
|
113 |
"</table>"; |
|
114 |
|
|
115 |
return mailBodyTemplate; |
|
116 |
} |
|
117 |
|
|
118 |
} |