XXL_JOB v2.4 接入钉钉群机器人告警

发布于:2025-04-04 ⋅ 阅读:(29) ⋅ 点赞:(0)
@Component
public class DingJobAlarm implements JobAlarm {

    private static Logger logger = LoggerFactory.getLogger(DingJobAlarm.class);

    @Autowired
    private RestTemplate restTemplate;

    @Value("${dingding.robot.webhook.url}")
    private String webhookUrl;

    @Override
    public boolean doAlarm(XxlJobInfo info, XxlJobLog jobLog) {
        boolean alarmResult = true;
        // send monitor email 这里可以读取任务配置页面填写的告警邮箱内容,可以往多个钉钉群里推送告警
        //  if (info != null && info.getAlarmEmail() != null && info.getAlarmEmail().trim().length() > 0) {
        if (info != null) {
            // if (!info.getAlarmEmail().contains("access_token")) {
            //     return alarmResult;
            // }
            Set<String> dingWebhookSet = new HashSet<String>(Arrays.asList(info.getAlarmEmail().split(",")));
            Map<String, Object> map = loadEmailJobAlarmTemplate(info, jobLog);
            // 发送钉钉消息 这里可以通过info.getAlarmEmail()传给多个 dingWebhookSet
            // for (String dingWebhook : dingWebhookSet) {
            try {
                restTemplate.postForEntity(webhookUrl, map, Object.class);
            } catch (Exception e) {
                logger.error(">>>>>>>>>>> xxl-job, job fail alarm email send error, JobLogId:{}", jobLog.getId(), e);
                alarmResult = false;
            }
            // }
        }
        return alarmResult;
    }

    private static final Map<String, Object> loadEmailJobAlarmTemplate(XxlJobInfo info, XxlJobLog jobLog) {
        HashMap<String, Object> map = new HashMap<>(2);
        /** 设置消息类型 **/
        map.put("msgtype", "text");

        /** 设置消息内容  内容中必须包含在钉钉机器人配置的关键字 -- start **/
        String content =
                "【告警信息】 \t\n" +
                        // "负责人 : \t" + info.getAuthor() + "\t\n" +
                        "任务id : \t" + info.getId() + "\t\n" +
                        "任务名称 : \t" + info.getJobDesc() + "\t\n" +
                        // "执行器名称 : \t" + info.getExecutorHandler() + "\t\n" +
                        // "执行器ip : \t" + jobLog.getExecutorAddress() + "\t\n" +
                        // "任务参数 : \t" + jobLog.getExecutorParam() + "\t\n" +
                        "LogId : \t" + jobLog.getId() + "\t\n" +
                        "TriggerMsg : \t" + jobLog.getTriggerMsg().replace("<br>", "\n")
                        .replace("<span style=\"color:#00c0ef;\" > >>>>>>>>>>>", "")
                        .replace("<<<<<<<<<<< </span>", "") + "\t\n" +
                        "HandleCode : \t" + jobLog.getHandleMsg() + "\t\n" +
                        "报警时间 : \t" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "\t\n";
        HashMap<String, String> cmap = new HashMap<>(1);
        cmap.put("content", content);
        map.put("text", cmap);
        /** 设置消息内容 -- stop **/

        /** 设置是否@指定人-start
            *需要@群里用户时,注意xxl_job里面的任务负责人需要填写:姓名-手机号
         **/
        // Map<String, Object> atmap = new HashMap<String, Object>();
        // String[] authorList = info.getAuthor().split(",");
        // ArrayList arrayList = new ArrayList();
        // for (String author : authorList) {
        //     if ("".equals(author) || author.split("-").length < 2) {
        //         continue;
        //     }
        //     arrayList.add(author.split("-")[1]);
        // }
        // if (arrayList.size() > 0) {
        //     atmap.put("atMobiles", arrayList.toArray());
        //     map.put("at", atmap);
        // }
        /** 设置是否@指定人 --stop **/
        return map;
    }
}

钉钉机器人设置: