Javaagent Class Unloading

I have a java agent that uses bytecode. I am using attach apis in java 6 to allow users to dynamically load the agent and tool and de-tool code using my java agent. I use a manifest attribute of type Boot-Class-Path to make sure my javagent classes are on the load path so that my users can use classes like ArrayList etc.

However, the problem is version related. Suppose a user dynamically attaches version 1 of my agent. Then I gave him version 2. Now that his application server never closes, since it is attaching version 1 of my agent, version 1 classes are still loading.

I need something like that when my client version 2 is javaagent, version 1 is unloaded.

I know that one way would be to write a class loader for javaagent classes and set the link to the class loader to null. However, in this case, I will not be able to tool classes on the path to loading, since my class loader will be lower in the hierarchy from the loader loader and thus, my users cannot use tool classes such as ArrayList, because if I add a call inside ArrayList methods to one of my agent class methods the bootloader will not be able to see them.

So, is there a way to solve the problem of loading the class and unload the previous agent classes?

+3
source share
4 answers

, , .

- ?

, , ?

, MyToolAgent, , , ToolAgentImplementation. ToolAgentImplementation1_0. 2.0 ToolAgentImplmenetation2_0 MyToolAgent . 1.0, . , .

, , , JVM , - .

+1

, , ...

, ( ), ( ). - 1 : RedefineClasses(), .

(MyToolInstallerV1), , . , , - no-op.

0

, java.util.ServiceLoader - , JAR URLClassLoader.

, " " , - , , ServiceLoader ( /META -INF/services/XXX), :)

.

0
source

Ideal use of OSGI. Not sure if you could connect your agent as a package.

0
source

Source: https://habr.com/ru/post/1714656/


All Articles