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