XXL-JOB实现钉钉报警

原生的xxl-job并没有钉钉报警功能,但是官方文档有写:可以很方便的集成钉钉报警功能,即新增一个实现 "com.xxl.job.admin.core.alarm.JobAlarm" 接口的告警实现即可

y4bBAU.jpg

 

1.代码结构

y4q93j.png

 

2.修改 JobFailMonitorHelper

如果不配置邮箱的话,是不会触发报警的。所以要修改下com.xxl.job.admin/core/thread下的JobFailMonitorHelper

y4L2TK.png

 

3.DingJobAlarm钉钉报警实现

如果在定时任务配置页面。负责人一项配置的是手机号,那么报警消息推送的时候就会有@艾特功能。

package com.xxl.job.admin.core.alarm.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.xxl.job.admin.core.alarm.JobAlarm;
import com.xxl.job.admin.core.model.XxlJobInfo;
import com.xxl.job.admin.core.model.XxlJobLog;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.*;

import com.alibaba.fastjson.JSON;


@Component
public class DingJobAlarm implements JobAlarm {

    private static final int timeout = 10000;

    @Value("${alarm.dingWebhook}")
    private String dingWebhook;

    @Override
    public boolean doAlarm(XxlJobInfo info, XxlJobLog jobLog) {
        boolean dingResult = true;
//        System.out.println("ding ---------------------");
        try {
            StringBuilder content = new StringBuilder("任务失败!\n");

            content.append("任务名称城市数据:").append(info.getJobDesc()).append("\n");
            content.append("负责人:").append(info.getAuthor()).append("\n");
            content.append("执行器名称:").append(info.getExecutorHandler()).append("\n");
            content.append("执行器ip:").append(jobLog.getExecutorAddress()).append("\n");
            content.append("任务参数:").append(jobLog.getExecutorParam()).append("\n");
            String msg = jobLog.getTriggerMsg();
            if(null != msg && !"".equals(msg.trim())){
                int start = msg.lastIndexOf("</span><br>")+11;
                int end = msg.lastIndexOf("<br><br>");
                if (end <= start){
                    end = msg.lastIndexOf("<br>");
                }
                msg = msg.substring(start, end);
            }
            content.append(msg).append("\n");
            content.append("执行任务时间:").append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())).append("\n");

            List<String> mobileList = new ArrayList<>();
            mobileList.add(info.getAuthor());

            String reqStr = buildReqStr(content.toString(), false, mobileList);

            //推送消息(http请求)
            String result = postJson(dingWebhook, reqStr);
//            System.out.println("result == " + result);

        } catch (Exception e) {
            e.printStackTrace();
            dingResult = false;
        }
        return dingResult;
    }

    private static String buildReqStr(String content, boolean isAtAll, List<String> mobileList) {
        //消息内容
        HashMap<String, String> contentMap = new HashMap<>();
        contentMap.put("content", content);

        //通知人
        HashMap<String, Object> atMap = new HashMap<>();
        //1.是否通知所有人
        atMap.put("isAtAll", isAtAll);
        //2.通知具体人的手机号码列表
        atMap.put("atMobiles", mobileList);

        HashMap<String, Object> reqMap = new HashMap<>();
        reqMap.put("msgtype", "text");
        reqMap.put("text", contentMap);
        reqMap.put("at", atMap);
        return JSON.toJSONString(reqMap);

    }


    private static String postJson(String url, String content){
        String body = null;
        try {
            body = HttpUtil.post(url, content);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return body;
    }

}

SyntaxError
199 声望19 粉丝