关于java:XXLJOB实现钉钉报警

57次阅读

共计 2733 个字符,预计需要花费 7 分钟才能阅读完成。

XXL-JOB 实现钉钉报警

原生的 xxl-job 并没有钉钉报警性能,然而官网文档有写:能够很不便的集成钉钉报警性能,即新增一个实现 “com.xxl.job.admin.core.alarm.JobAlarm” 接口的告警实现即可

 

1. 代码构造

 

2. 批改 JobFailMonitorHelper

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

 

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;
    }

}

正文完
 0