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 implement the hot replacement of java class loader and class

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains the "java class loader and class hot replacement how to achieve", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "java class loader and class hot replacement how to achieve" it!

Any Class used needs to be loaded into JVM by classLoader. The loading process is divided into

DefineClass

LoadClass

ResolveClass

That is, according to the contents of different forms of class files provided, it is converted into a Class, loaded and linked.

The so-called hotswap is to replace the contents of the loaded Class with new code.

We use Javassist to simulate a similar effect here, by dynamically modifying the content of a method in class to achieve the effect of hot replacement.

Basic steps:

First define an interface that contains a method, the contents of which are later used for hot replacement

Define the implementation of an interface

Receive the parameters passed on the page in Servlet, and the content of the parameters on the page is the content of the method body corresponding to the above interface definition method and method

After receiving the parameter, use Javassist to perform the hot replacement operation according to the content of the parameter.

The code for the basic Javassist looks something like this:

ITemp test = new Temp (); / / declare an interface

ClassPool pool = new ClassPool (true)

Private ITemp hotswap (String str) {

String methodBody = null

Try {

/ / add to classpath

Pool.insertClassPath (new ClassClassPath (ITemp.class))

String className = "ITemp_" + UUID.randomUUID ()

CtClass clazz = pool.makeClass (className)

Clazz.setInterfaces (new CtClass [] {pool.get ("com.xxxx.ITemp")})

MethodBody = str

Clazz.addMethod (CtMethod.make (methodBody, clazz))

ITemp fun = (ITemp) clazz.toClass () .newInstance ()

CalculatorClass.writeFile ("d:/temp")

Return fun

} catch (Exception e) {

}

In the process of using, after receiving the parameter, point the instance returned by the hotswap method to the instance test. Then the execution of the corresponding method in the test has been changed to the logic passed in on the page.

Principle

How is the above realized?

In fact, the replacement above, in essence, regenerates an implementation class of the ITemp interface, and then what we define on the page is the content of the method of the implementation class. Then our replacement is to change the reference of one implementation class to another.

In the update Server, the reference to service points to the new implementation, and the old one will be released after the reference request is completed.

The other one.

At this point, some students must have mentioned Agent, which is supported by JDK by default, which is based on JVMTI. However, it is important to note that Agent is used at this time, and if you use its pre_main, it supports the so-called enhancement or weaving of Class's defineClass before execution, similar to OpenJPA's Enhance and AJP's weave.

This is equivalent to modifying the class content after reading it, and then passing it to classLoader for defineClass.

After JDK1.6, another form supported by JVMTI is to change the class after the application has been deployed and started, using the agent_main method. At this point, it is necessary to retransform the class. For example, some commonly used Profiler tools in Java use this, attach to the Java process that has been started, and retransform it, such as Btrace.

The transform here invokes Instrumention's RedefineClass, which essentially modifies the definition of the class. Let's take Btrace as an example to see what he did behind his back.

My Trace script reads as follows:

@ BTrace (unsafe=true)

Public class TracingScript {

OnMethod (clazz= "com.xxx.WorkFun", method= "myPrint")

Public void sc () {

Println ("=")

}

}

For the class WorkFun to be monitored, we see that after Btrace monitoring, a new method $btrace$TracingScript$sc () is added to the class.

What this method does is what we defined in the Trace script. For example, we print an equal sign when the corresponding java method is executed, and the corresponding method jvm instruction is as follows:

And how does this approach relate to the methods we want to monitor? It is in the first line of our monitoring method that the call to the trace method is added.

Thank you for your reading, the above is the "java class loader and class hot replacement how to achieve" the content, after the study of this article, I believe you on the java class loader and class hot replacement how to achieve this problem has a deeper understanding, the specific use of the need for you to practice verification. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Internet Technology

Wechat

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

12
Report