Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Arthas to get the Spring ApplicationContext restore problem site

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use Arthas to get Spring ApplicationContext restore problem site". In daily operation, I believe many people have doubts about how to use Arthas to get Spring ApplicationContext restore problem site. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how to use Arthas to get Spring ApplicationContext restore problem site". Next, please follow the editor to study!

Background

Recently, an intern has come and arranged for him to implement the small function of detecting the connectivity of the target website. To put it simply, he will replace the following shell script with Java code to achieve it.

#! / bin/bashURL= "https://www.baidu"HTTP_CODE=`curl-o / dev/null-s-w"% {http_code} "${URL}" `# echo $HTTP_CODEif [$HTTP_CODE! = '200'] Thencurl 'https://oapi.dingtalk.com/robot/send?access_token=xx'\-H' Content-Type: application/json'\-d'{"msgtype": "text", "text": {"content": "Baidu platform is not working properly, please note!" }, "isAtAll": true}'fi function is implemented using spring task@Scheduled (cron = "000 try 1 * *?") public void startSchedule () {log.info ("start performing scheduled tasks to test Baidu website connectivity"); try {HttpResponse response = HttpRequest.get ("). Execute () If (HttpStatus.HTTP_OK! = response.getStatus ()) {this.send2DingTalk (response.getStatus ());} log.info ("request Baidu successfully, return message: {}", response.body ());} catch (HttpException e) {log.error ("request exception Baidu: {}", e); this.send2DingTalk (e.getMessage ()) } log.info ("completion of the task of testing Baidu website connectivity");} problem description

Deployed on the server, my old Jio had already called the task status abnormal, but the younger brother's Java code still did not execute the notification.

To go through the production log, I only entered the beginning and did not output the end of the scheduled task. I felt stuck somewhere. I took it for granted that if I timed out, I would always go to the catch logic, and the troubleshooting would be fruitless.

Since the task is once an hour, how to quickly trigger this exception and restore the accident scene

Because there is no graphical interface and API interface for using simple Spring Task

Arthas restores the accident scene and retriggers the task

The core gets the spring context and executes its startSchedule method

Determine the monitoring point

The request of SpringMVC will be processed on the destination interface by executing invokeHandlerMethod through RequestMappingHandlerAdapter.

In the RequestMappingHandlerAdapter class, there is getApplicationContext ()

@ Nullablepublic 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;}}

Execute any request to get the RequestMappingHandlerAdapter target target, and then execute getApplicationContext

The tt command gets the ApplicationContext

Arthas executes tt

Tt-t org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter invokeHandlerMethod

Tt can capture any web request executed.

Based on the index of the target, execute a custom OGNL expression

Tt-I 1019-w 'target.getApplicationContext ()'

Use ApplicationContext to get the scheduled task bean to execute startSchedulett-I 1000-w 'target.getApplicationContext () .getBean ("baiduSchedule") .startSchedule ()'

The ok task is triggered again.

It is clear from the investigation of the cause of the accident that the tool class using hutool did not set timeout and caused infinite waiting, so the catch logic was not executed.

At this point, the study on "how to use Arthas to get the Spring ApplicationContext restore problem site" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 241

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report