最近来了个实习僧小弟,安排他实现对目标网站 连通性检测的小功能,简单讲就是将下边的shell 脚本换成Java 代码来实现web
#!/bin/bash
URL="https://www.baidu"
HTTP_CODE=`curl -o /dev/null -s -w "%{http_code}" "${URL}"`
#echo $HTTP_CODE
if [ $HTTP_CODE != '200' ];then
curl 'https://oapi.dingtalk.com/robot/send?access_token=xx' \
-H 'Content-Type: application/json' \
-d '{"msgtype": "text",
"text": {
"content": "百度平台状态不正常,请注意!"
},
"isAtAll": true
}'
fi复制代码
@Scheduled(cron = "0 0 0/1 * * ? ")
public void startSchedule() {
log.info("开始执行定时任务 ,检测百度网站连通性");
try {
HttpResponse response = HttpRequest.get("").execute();
if (HttpStatus.HTTP_OK != response.getStatus()) {
this.send2DingTalk(response.getStatus());
}
log.info("请求百度成功,返回报文:{}",response.body());
} catch (HttpException e) {
log.error("请求异常百度:{}", e);
this.send2DingTalk(e.getMessage());
}
log.info("执行检测百度网站连通任务完毕");
}复制代码
部署在服务器上,个人老jio本 都已经呼叫任务状态不正常了,但是小弟的Java 代码仍是没有执行通知spring
核心拿到 spring context 而后执行它的 startSchedule
方法
shell
RequestMappingHandlerAdapter
执行invokeHandlerMethod
到达目标接口上进行处理 RequestMappingHandlerAdapter
类中有 getApplicationContext() @Nullable
public final ApplicationContext getApplicationContext() throws IllegalStateException {
if (this.applicationContext == null && this.isContextRequired()) {
throw new IllegalStateException("ApplicationObjectSupport instance [" + this + "] does not run in an ApplicationContext");
} else {
return this.applicationContext;
}
}复制代码
RequestMappingHandlerAdapter
target 目标,而后执行 getApplicationContext
tt -t org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter invokeHandlerMethod复制代码
tt -i 1019 -w 'target.getApplicationContext()'复制代码
tt -i 1000 -w 'target.getApplicationContext().getBean("baiduSchedule").startSchedule()'复制代码
ok 任务从新触发了json
事故缘由调查清楚,因为使用hutool 的工具类 没有设置timeout 致使无限等待,因此没有执行catch 逻辑api
禁止
生产操做,只是提供个思路 ,固然能够衍生其余业务场景的操做