Delay starting JMXFetch when there's a custom JMX builder #1958
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This takes a similar approach to how we deal with custom log managers, however unlike the logging case there is no clear activating class with a static initialization block. Instead we wait for the
MBeanServerBuilderclass to be loaded and use that as an indication that we can go ahead and access JMX.This is acceptable because
MBeanServerBuilderis the base class for all custom JMX builders and is only used inMBeanServerFactorywhich creates the custom builder instance. Note JMX doesn't let you supply an actual builder instance - it only lets you specify the class name whichMBeanServerFactoryattempts to load, first by the current thread's context classloader, then falling back toClass.forName.The place where
MBeanServerBuilderis loaded should be whereMBeanServerFactoryis looking up the custom builder, unless the application is usingMBeanServerBuilderitself before starting JMX. But that would be unusual because its only use is to create MBean servers and delegates for JMX. Even in that case though we should be OK because at the time the application loadsMBeanServerBuilderthe current thread's context classloader should be able to see the custom JMX builder. And since the classload hook thread inherits that context classloader then it too should be able to see that custom JMX builder. Therefore it doesn't matter if the classload hook or the application win the race to start JMX. Either one should be able to see the custom builder class at that point. (We just need to make sure we make a call to JMX before we change the classload hook thread's context classloader when starting JMXFetch.)The only other classes loaded after
MBeanServerBuilderwould be the internal JMX classes, whose names could vary between JDKs. Alternatively we could wait for the custom JMX builder class, but there's a chance that the initial value is overridden later on and the initially named class is never loaded.