任务告警模块化,代码重构优化。

pull/15/MERGE
xuxueli 5 years ago
parent 882a439ef1
commit 7127c0400e

@ -1245,8 +1245,7 @@ API服务请求参考代码com.xxl.job.executor.ExecutorBizTest
- 2、提供基于HTTP的任务HandlerBean任务JobHandler="HttpJobHandler"业务方只需要提供HTTP链接即可不限制语言、平台
### 5.18 任务失败告警
默认提供邮件失败告警,可扩展短信、钉钉等方式,扩展代码位置为 "com.xxl.job.admin.core.alarm.JobAlarmer"。
如果需要新增一种告警方式,只需要新增一个实现 "com.xxl.job.admin.core.alarm.JobAlarm" 接口的告警实现即可
默认提供邮件失败告警,可扩展短信、钉钉等方式。如果需要新增一种告警方式,只需要新增一个实现 "com.xxl.job.admin.core.alarm.JobAlarm" 接口的告警实现即可。可以参考默认提供邮箱告警实现 "EmailJobAlarm"。
### 5.19 调度中心Docker镜像构建
可以通过以下命令快速构建调度中心,并启动运行;

@ -1,17 +1,38 @@
package com.xxl.job.admin.core.alarm;
import com.xxl.job.admin.core.alarm.impl.EmailJobAlarm;
import com.xxl.job.admin.core.model.XxlJobInfo;
import com.xxl.job.admin.core.model.XxlJobLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Component
public class JobAlarmer {
public class JobAlarmer implements ApplicationContextAware, InitializingBean {
private static Logger logger = LoggerFactory.getLogger(JobAlarmer.class);
@Resource
private EmailJobAlarm emailJobAlarm;
private ApplicationContext applicationContext;
private List<JobAlarm> jobAlarmList;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
@Override
public void afterPropertiesSet() throws Exception {
Map<String, JobAlarm> serviceBeanMap = applicationContext.getBeansOfType(JobAlarm.class);
if (serviceBeanMap != null && serviceBeanMap.size() > 0) {
jobAlarmList = new ArrayList<JobAlarm>(serviceBeanMap.values());
}
}
/**
* job alarm
@ -22,15 +43,23 @@ public class JobAlarmer {
*/
public boolean alarm(XxlJobInfo info, XxlJobLog jobLog) {
// alarm by email
boolean emailResult = emailJobAlarm.doAlarm(info, jobLog);
boolean result = false;
if (jobAlarmList!=null && jobAlarmList.size()>0) {
result = true; // success means all-success
for (JobAlarm alarm: jobAlarmList) {
boolean resultItem = false;
try {
resultItem = alarm.doAlarm(info, jobLog);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
if (!resultItem) {
result = false;
}
}
}
// do something, custom alarm strategy, such as sms
// ...
return emailResult;
return result;
}
}

@ -61,13 +61,7 @@ public class JobFailMonitorHelper {
// 2、fail alarm monitor
int newAlarmStatus = 0; // 告警状态0-默认、-1=锁定状态、1-无需告警、2-告警成功、3-告警失败
if (info!=null && info.getAlarmEmail()!=null && info.getAlarmEmail().trim().length()>0) {
boolean alarmResult = true;
try {
alarmResult = XxlJobAdminConfig.getAdminConfig().getJobAlarmer().alarm(info, log);
} catch (Exception e) {
alarmResult = false;
logger.error(e.getMessage(), e);
}
boolean alarmResult = XxlJobAdminConfig.getAdminConfig().getJobAlarmer().alarm(info, log);
newAlarmStatus = alarmResult?2:3;
} else {
newAlarmStatus = 1;

Loading…
Cancel
Save