最近來了個實習僧小弟,安排他實現對目標網站 連通性檢測的小功能,簡單講就是將下邊的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
禁止
生產操做,只是提供個思路 ,固然能夠衍生其餘業務場景的操做