Click to enter _ more _java thousand ask
1. What is ATTACHAPI?
The Attach API is a set of extension APIs provided by Sun, which is not a standard Java API. As the name implies, Attach, some programs are attached to the JVM. Simply put, a program is connected to a running JVM, and after the connection, the program can run in the JVM. This procedure is called the JVMTI Agent program.
We use it to dynamically set the Load proxy class (JVMTI agent) during the run, allowing us to easily monitor a JVM and run an additional agent.
Learn JVMTI agent look here: JVMTI is what
2. ATTACHAPI provides those interfaces
The Attach API is very simple in function and provides several functions as follows:
- Gets the description of all current JVM instances.
- Loads the agent program for the specified JVM.
Gets the description of all current JVM instances
Example:
publicclass TestAgent { publicstaticvoidmain(String[] args) throws AttachNotSupportedException, IOException, AgentLoadException, AgentInitializationException { List<VirtualMachineDescriptor> list = VirtualMachine.list(); for (VirtualMachineDescriptor vmd : list) { System.out.println("pid:"":" + vmd.displayName()); } }}
The results of the implementation are as follows:
Pid:8536:com.test.jvmti.testagent
pid:6697:
Loading the agent for the specified JVM
In the "How to write JVMTI agent" This article has a complete example, clearly explained by the ATTACHAPI for the specified JVM loading agent program, specifically as follows:
See JVMTI Agent Program here: How to write JVMTI Agent program
Java thousand asked _08jdk detailed (016) _ATTACHAPI is what