In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to call chain and method execution time statistics in Java". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to call chain and method execution time statistics in Java.
Bee-mite is a name I gave to this project. Take a look at the project directory. There is not much code. (I haven't done the function of getting sessionid yet. It's easy. I'll add it later.)
Asmip package:
Implement bytecode stuffing, modify the bytecode before the class is loaded, and insert the burying point. At present, the business code call chaining pile has been implemented, which intercepts and obtains the class name, method name and the parameters of the method call before the method execution, and obtains the exception information when the method executes an exception. It also realizes the burying point of the method execution time, obtains the system time before the method execution, sends a diary event, obtains the system time after the method execution, and sends an event.
Business package:
Code stuffing filter, using responsibility connection mode, inserting piles on bytecode multiple times.
Ipevent package:
Event encapsulation, the buried code throws the event to the thread pool, and the thread pool dispatches the event to the listener for processing.
Logs package:
Provides an event listener interface, and the specific implementation is for the user to implement. I provide two default implementation classes here. Under the logimpl package, the default implementation class only prints the diary and prints the diary information in the console.
Usage
How to use it? I wrote a test module in the project, which is a simple web project, a controller UserHander, and a service layer implementation class UserServiceImpl. When the client sends a "/ user/wujiuye/123" request, the execution chain is the queryUser method of UserHander-> the queryUser method of UserServiceImpl.
This is a spring boot project. Set the parameter to VM options,-the absolute path of the javaagent:bee-mite.jar package, followed by a parameter, that is, the package name. To insert piles on the business code under which package in the bee-mite-webdemo project, such as "com.wujiuye", is to insert piles on all classes under the "com.wujiuye" package. Of course, I have filtered out the interfaces, static methods, get and set methods.
Run the spring boot project, type url in the browser, and observe the information printed on the console as follows.
The technology used
This is the use of asm, javaagent, responsibility mode. Because the bytecode is inserted into the business code, the buried point code is executed when the business code is executed, and if the handler is also carried out in the business code, it will be a time-consuming operation, affecting performance and slowing down the response speed of a request. So when the buried code is executed, I directly throw a message event and ask the thread pool to dispatch the message to the listener to handle the event. This allows you to perform time-consuming operations, such as storing the diary in a database for persistence, or using redis storage to facilitate later troubleshooting of project code exceptions.
What did the bytecode stake do?
I wrote two test classes under the test package of the bee-mite module, in which UserServiceImpl is the target of the stuffing. Running the main method of TestAop will generate a file called TargerProxy.class in the project's targer/classes directory, which is the bytecode file after the UserServiceImpl stub. Let's take a look at what bee-mite has done for us.
❖ source code
After ❖ asm inserts the bytecode
Because of the use of the chain of responsibility mode, the code will be inserted twice, in order to easily expand the function later. I believe you can also see what code my bee-mite has inserted for me. These codes are inserted through asm's own bytecode instructions. Of course, it is not very difficult to say that the difficulty is the insertion of the try-catch code block, but it is difficult to find out without reading the document. The location of the three label of the visitTryCatchBlock method and the exception handling of the catch block are the difficulties. I finally found the exception handling table by adding the try-catch block to the source code class and then javap looking at the bytecode.
Exception table:
From to target type
0 27 30 Class java/lang/Exception so far, I believe you have a deeper understanding of "how to call chain and method execution time statistics in Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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: 0
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.