监听java程序终止发送钉钉消息通知

以springboot项目为例 kill -9 进程号则监控无效 kill 进程号有效

public class HuiFanServiceApplication {
   public static void main(String[] args) {
      SpringApplication.run(HuiFanServiceApplication.class, args);
      ProjectUtil projectation=SpringUtil.getBean(ProjectUtil.class);
      System.out.println("----------------------------------------------");
      System.out.println("         项目名称:"+projectation.getName() +"");
      System.out.println("         项目版本:"+projectation.getVersion() +"");
      System.out.println("         项目构建日期:"+projectation.getTime()+"");
      System.out.println("         项目版权:"+projectation.getCopyright() +"");
      System.out.println("----------------------------------------------");

      new HookListener().start();
   }

}
public class HookListener {
    public void start()
    {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            System.out.println("程序终止。。。");
            ApiResult<String> result = new ApiResult<>();
            DingDingNotifyServiceUtil.sendSMSMessage("手机号","健康云病案复印程序停止啦(内容要包含智能群助手自定义关键词)", result,null);
        }));
    }
}

 

public class DingDingNotifyServiceUtil {
   private static final Logger logger = LoggerFactory.getLogger(DingDingNotifyServiceUtil.class);

   // 钉钉地址
   private static  String DING_DING_NOTIFY_URL = "(钉钉群智能群助手webhook)https://oapi.dingtalk.com/robot/send?access_token=";
   private static  String dingdingToken = "钉钉群智能群助手(webhook)";

   public static ApiResult<String> sendSMSMessage(String mobile, String content,
                                       ApiResult<String> result, String url) {
      try {
         if (StringUtil.isEmpty(url)){
            url = DING_DING_NOTIFY_URL;
         }
         OkHttpClient instance15 = OkhttpBuilder.getInstance15();
         DingDingTextNotifyParame parame = new DingDingTextNotifyParame(content, Arrays.asList(mobile.split(",")),
               false);
         logger.info("【开始调用钉钉机器人通知, 入参:{},url:{}】", JSON.toJSONString(parame), url);
         HttpResponse httpResponse = OkHttp3Utils.postBody(instance15, url + dingdingToken, null,
               OkHttp3Utils.CONTENT_TYPE_JSON, JSON.toJSONString(parame));
         logger.info("【调用钉钉机器人通知结束, 耗时:{}, 出参:{}】", httpResponse.getExecuteTime(),httpResponse.getResponseSt());
         if (!httpResponse.isSuccss()) {
            result.setSuccess(false);
            result.setMessage("钉钉调用机器人通知异常");
            return result;
         }
         if (!StringUtil.isEmpty(httpResponse.getResponseSt())) {
            JSONObject jsonObject = JSON.parseObject(httpResponse.getResponseSt());
            Integer errcode = jsonObject.getIntValue("errcode");
            if(errcode != null &&errcode==0){
               result.setSuccess(true);
               result.setMessage("成功");
               return result;
            }
            result.setSuccess(false);
            result.setMessage(jsonObject.getString("errmsg"));
            return result;
            
         }
      } catch (Exception e) {
         result.setSuccess(false);
         result.setMessage("钉钉调用机器人通知异常");
         logger.error("钉钉调用机器人通知异常", e);
      }
      return result;
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值