This is the second post in the series of posts on writing Hotswap agent for Java Virtual Machine. In the previous post, we saw an higher level overview about the requirements and specification on how it can be done. In this post, we will drill down to the details. I am also planning to write few more posts on implementing this agent using Java Debug Interface. Also, once you understand this Hotswap agent, you can write a lot of tools using Java Debug Interface. The rest of the post gives the details of how to write an agent using JDI.
Java Debug Interface is a programming interface which lets us to write agents that interact with a live JVM. The interaction is of two types. First, JDI gives infrastructure to receive events from the JVM. For example, whenever an exception is thrown, JVM publishes an event. With the help of JDI an in-process agent or an agent running as different process in the same or different system can consume the event. Secondly, with the help of infrastruture given by JDI, the agent can also control JVM. The events together with controlling of JVM helps developers to write tools such as debuggers and profilers.
When you are going to write an agent (a program that monitors and managers remote JVM) using JDI, the first thing that you have to do is to know how are you going to connect to JVM. There are mutiple connectors available. Each connector differ in the way on how they interact with the JVM. For example, LaunchingConnector helps you to launch a JVM before connecting to it but ListeningConnector simply listens to a running JVM. Next thing is the transport mechanism, the method of communication between JVM and the agent.
After deciding both of these, you need connect to JVM. If the connection is successful, you will get an instance of JVM. With the remote JVM object, you can subscribe for specific event by through JDI request. Apart from this, using classes and methods in JDI, the agent can also control JVM such as stopping a thread, running garbage collection, redefine and reload classes etc. The following is the list of events that an agent can subscribe for.
AccessWatchpointEvent
BreakpointEvent
ClassPrepareEvent
ClassUnloadEvent
Event
EventIterator
EventQueue
EventSet
ExceptionEvent
LocatableEvent
MethodEntryEvent
MethodExitEvent
ModificationWatchpointEvent
StepEvent
ThreadDeathEvent
ThreadStartEvent
VMDeathEvent
VMDisconnectEvent
VMStartEvent
WatchpointEvent
BreakpointEvent
ClassPrepareEvent
ClassUnloadEvent
Event
EventIterator
EventQueue
EventSet
ExceptionEvent
LocatableEvent
MethodEntryEvent
MethodExitEvent
ModificationWatchpointEvent
StepEvent
ThreadDeathEvent
ThreadStartEvent
VMDeathEvent
VMDisconnectEvent
VMStartEvent
WatchpointEvent
If you want to write a simple tool to reload classes on the fly, you have to identify Connector, Transport, subscribe for events like JVM Start, JVM End. You have to use redefineClasses method and then reload the classes into the JVM. I believe, this post would have given you some idea on how to write a Hotswap agent. In the next post, we will see the implementation of Hotswap agent with the help of code.