How to Re‑trigger a Stalled Spring Scheduled Task with Arthas
This article explains how to diagnose and manually re‑execute a Spring @Scheduled task that fails to send notifications by using Arthas to obtain the ApplicationContext, invoke the bean method, and reveals that a missing timeout in Hutool caused the original hang.
Background
Recently an intern was asked to replace a shell script that checks connectivity to a target website with Java code.
<ol>
<li><code>#!/bin/bash</code></li>
<li><code>URL="https://www.baidu"</code></li>
<li><code>HTTP_CODE=`curl -o /dev/null -s -w "%{http_code}" "${URL}"`</code></li>
<li><code>#echo $HTTP_CODE</code></li>
<li><code>if [ $HTTP_CODE != '200' ]; then</code></li>
<li><code>curl 'https://oapi.dingtalk.com/robot/send?access_token=xx' \</code></li>
<li><code>-H 'Content-Type: application/json' \</code></li>
<li><code>-d '{"msgtype": "text",</code></li>
<li><code> "text": {</code></li>
<li><code> "content": "百度平台状态不正常,请注意!"</code></li>
<li><code> },</code></li>
<li><code> "isAtAll": true</code></li>
<li><code> }'</code></li>
<li><code>fi</code></li>
</ol>Implementation
Using Spring Task
<ol>
<li><code>@Scheduled(cron = "0 0 0/1 * * ? ")</code></li>
<li><code>public void startSchedule(){</code></li>
<li><code> log.info("开始执行定时任务 ,检测百度网站连通性");</code></li>
<li><code> try{</code></li>
<li><code> HttpResponse response = HttpRequest.get("").execute();</code></li>
<li><code> if(HttpStatus.HTTP_OK != response.getStatus()){
this.send2DingTalk(response.getStatus());
}</code></li>
<li><code> log.info("请求百度成功,返回报文:{}", response.body());</code></li>
<li><code> }catch(HttpException e){</code></li>
<li><code> log.error("请求异常百度:{}", e);
this.send2DingTalk(e.getMessage());
}</code></li>
<li><code> log.info("执行检测百度网站连通任务完毕");</code></li>
<li><code>}</code></li>
</ol>Problem Description
The task runs hourly on a server, but the Java code never sends the DingTalk notification; logs show only the start message.
Logs show the start message but no end message, suggesting the task is stuck.
Need a way to trigger the failure path quickly for debugging.
Spring Task has no UI or API to invoke manually.
Reproducing the Issue with Arthas
Obtain the Spring ApplicationContext and invoke the
startSchedulemethod directly.
Identify the Monitoring Point
Spring MVC requests go through
RequestMappingHandlerAdapterand its
invokeHandlerMethod.
The class contains
getApplicationContext()to retrieve the context.
<code>@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;
}
}
</code>Execute any request to capture the target
RequestMappingHandlerAdapterand then call
getApplicationContext().
Arthas tt Command to Get ApplicationContext
Run
tt -t org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter invokeHandlerMethodto trace the method.
After a normal web request, the trace captures the target bean.
Then execute an OGNL expression to invoke the scheduled bean:
<code>tt -i 1000 -w 'target.getApplicationContext().getBean("baiduSchedule").startSchedule()'</code>The task is re‑triggered.
Root cause: the Hutool HTTP utility used in the script lacked a timeout, causing an indefinite wait and preventing the catch block from executing.
Summary
Do not run such operations in production; the article only provides a proof‑of‑concept.
The core technique is using Arthas to fetch the Spring ApplicationContext and invoke a bean method.
Arthas is an open‑source Java diagnostic tool from Alibaba, widely used by developers.
Follow us for more practical Java EE examples.
Java Architecture Diary
Committed to sharing original, high‑quality technical articles; no fluff or promotional content.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.