Class MBeanExporter
- All Implemented Interfaces:
Aware
,BeanClassLoaderAware
,BeanFactoryAware
,DisposableBean
,InitializingBean
,SmartInitializingSingleton
,MBeanExportOperations
- Direct Known Subclasses:
AnnotationMBeanExporter
MBeanServer
, without the need to define any
JMX-specific information in the bean classes.
If a bean implements one of the JMX management interfaces, MBeanExporter can simply register the MBean with the server through its autodetection process.
If a bean does not implement one of the JMX management interfaces, MBeanExporter
will create the management information using the supplied MBeanInfoAssembler
.
A list of MBeanExporterListeners
can be registered
via the listeners
property, allowing
application code to be notified of MBean registration and unregistration events.
This exporter is compatible with MBeans as well as MXBeans.
- Since:
- 1.2
- Author:
- Rob Harrop, Juergen Hoeller, Rick Evans, Mark Fisher, Stephane Nicoll, Sam Brannen
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Deprecated.static final int
Deprecated.as of 6.1, in favor of the"autodetect" flag
static final int
Deprecated.as of 6.1, in favor of the"autodetect" flag
static final int
Deprecated.as of 6.1, in favor of the"autodetect" flag
Fields inherited from class org.springframework.jmx.support.MBeanRegistrationSupport
logger, server
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected DynamicMBean
adaptMBeanIfPossible
(Object bean) Build an adapted MBean for the given bean instance, if possible.void
addExcludedBean
(String excludedBean) Add the name of bean that should be excluded from autodetection.void
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,ApplicationContextAware
etc.void
Kick off bean registration automatically after the regular singleton instantiation phase.protected ModelMBean
createAndConfigureMBean
(Object managedResource, String beanKey) Creates an MBean that is configured with the appropriate management interface for the supplied managed resource.protected ModelMBean
Create an instance of a class that implementsModelMBean
.void
destroy()
Unregisters all beans that this exported has exposed via JMX when the enclosingApplicationContext
is destroyed.protected ObjectName
getObjectName
(Object bean, String beanKey) Retrieve theObjectName
for a bean.protected boolean
isBeanDefinitionLazyInit
(ListableBeanFactory beanFactory, String beanName) Return whether the specified bean definition should be considered as lazy-init.protected boolean
Determine whether the given bean class qualifies as an MBean as-is.protected void
onRegister
(ObjectName objectName) Called when an MBean is registered.protected void
onUnregister
(ObjectName objectName) Called when an MBean is unregistered.protected ObjectName
registerBeanNameOrInstance
(Object mapValue, String beanKey) Register an individual bean with theMBeanServer
.protected void
Register the defined beans with theMBeanServer
.registerManagedResource
(Object managedResource) Register the supplied resource with JMX.void
registerManagedResource
(Object managedResource, ObjectName objectName) Register the supplied resource with JMX.void
setAllowEagerInit
(boolean allowEagerInit) Specify whether to allow eager initialization of candidate beans when autodetecting MBeans in the Spring application context.void
setAssembler
(MBeanInfoAssembler assembler) Set the implementation of theMBeanInfoAssembler
interface to use for this exporter.void
setAutodetect
(boolean autodetect) Set whether to autodetect MBeans in the bean factory that this exporter runs in.void
setAutodetectMode
(int autodetectMode) Deprecated.as of 6.1, in favor of the"autodetect" flag
void
setAutodetectModeName
(String constantName) Deprecated.as of 6.1, in favor of the"autodetect" flag
void
setBeanClassLoader
(ClassLoader classLoader) Callback that supplies the beanclass loader
to a bean instance.void
setBeanFactory
(BeanFactory beanFactory) void
Supply aMap
of beans to be registered with the JMXMBeanServer
.void
setEnsureUniqueRuntimeObjectNames
(boolean ensureUniqueRuntimeObjectNames) Indicates whether Spring should ensure thatObjectNames
generated by the configuredObjectNamingStrategy
for runtime-registered MBeans (registerManagedResource(java.lang.Object)
) should get modified: to ensure uniqueness for every instance of a managedClass
.void
setExcludedBeans
(String... excludedBeans) Set the list of names for beans that should be excluded from autodetection.void
setExposeManagedResourceClassLoader
(boolean exposeManagedResourceClassLoader) Indicates whether the managed resource should be exposed on thethread context ClassLoader
before allowing any invocations on the MBean to occur.void
setListeners
(MBeanExporterListener... listeners) Set theMBeanExporterListener
s that should be notified of MBean registration and unregistration events.void
setNamingStrategy
(ObjectNamingStrategy namingStrategy) Set the implementation of theObjectNamingStrategy
interface to use for this exporter.void
setNotificationListenerMappings
(Map<?, ? extends NotificationListener> listeners) Set theNotificationListeners
to register with theMBeanServer
.void
setNotificationListeners
(NotificationListenerBean... notificationListeners) Set theNotificationListenerBeans
containing theNotificationListeners
that will be registered with theMBeanServer
.void
unregisterManagedResource
(ObjectName objectName) Remove the specified MBean from the underlying MBeanServer registry.Methods inherited from class org.springframework.jmx.support.MBeanRegistrationSupport
doRegister, doUnregister, getRegisteredObjectNames, getServer, onRegister, setRegistrationPolicy, setServer, unregisterBeans
-
Field Details
-
AUTODETECT_NONE
Deprecated.as of 6.1, in favor of the"autodetect" flag
Autodetection mode indicating that no autodetection should be used.- See Also:
-
AUTODETECT_MBEAN
Deprecated.as of 6.1, in favor of the"autodetect" flag
Autodetection mode indicating that only valid MBeans should be autodetected.- See Also:
-
AUTODETECT_ASSEMBLER
Deprecated.as of 6.1, in favor of the"autodetect" flag
Autodetection mode indicating that only theMBeanInfoAssembler
should be able to autodetect beans.- See Also:
-
AUTODETECT_ALL
Deprecated.as of 6.1, in favor of the"autodetect" flag
Autodetection mode indicating that all autodetection mechanisms should be used.- See Also:
-
-
Constructor Details
-
MBeanExporter
public MBeanExporter()
-
-
Method Details
-
setBeans
Supply aMap
of beans to be registered with the JMXMBeanServer
.The String keys are the basis for the creation of JMX object names. By default, a JMX
ObjectName
will be created straight from the given key. This can be customized through specifying a customNamingStrategy
.Both bean instances and bean names are allowed as values. Bean instances are typically linked in through bean references. Bean names will be resolved as beans in the current factory, respecting lazy-init markers (that is, not triggering initialization of such beans).
- Parameters:
beans
- a Map with JMX names as keys and bean instances or bean names as values- See Also:
-
setAutodetect
public void setAutodetect(boolean autodetect) Set whether to autodetect MBeans in the bean factory that this exporter runs in. Will also ask anAutodetectCapableMBeanInfoAssembler
if available.This feature is turned off by default. Explicitly specify
true
here to enable autodetection. -
setAutodetectModeName
Deprecated.as of 6.1, in favor of the"autodetect" flag
Set the autodetection mode to use by name.- Throws:
IllegalArgumentException
- if the supplied value is not resolvable to one of theAUTODETECT_
constants or isnull
- See Also:
-
setAutodetectMode
Deprecated.as of 6.1, in favor of the"autodetect" flag
Set the autodetection mode to use.- Throws:
IllegalArgumentException
- if the supplied value is not one of theAUTODETECT_
constants- See Also:
-
setAllowEagerInit
public void setAllowEagerInit(boolean allowEagerInit) Specify whether to allow eager initialization of candidate beans when autodetecting MBeans in the Spring application context.Default is "false", respecting lazy-init flags on bean definitions. Switch this to "true" in order to search lazy-init beans as well, including FactoryBean-produced objects that haven't been initialized yet.
-
setAssembler
Set the implementation of theMBeanInfoAssembler
interface to use for this exporter. Default is aSimpleReflectiveMBeanInfoAssembler
.The passed-in assembler can optionally implement the
AutodetectCapableMBeanInfoAssembler
interface, which enables it to participate in the exporter's MBean autodetection process. -
setNamingStrategy
Set the implementation of theObjectNamingStrategy
interface to use for this exporter. Default is aKeyNamingStrategy
.- See Also:
-
setEnsureUniqueRuntimeObjectNames
public void setEnsureUniqueRuntimeObjectNames(boolean ensureUniqueRuntimeObjectNames) Indicates whether Spring should ensure thatObjectNames
generated by the configuredObjectNamingStrategy
for runtime-registered MBeans (registerManagedResource(java.lang.Object)
) should get modified: to ensure uniqueness for every instance of a managedClass
.The default value is
true
. -
setExposeManagedResourceClassLoader
public void setExposeManagedResourceClassLoader(boolean exposeManagedResourceClassLoader) Indicates whether the managed resource should be exposed on thethread context ClassLoader
before allowing any invocations on the MBean to occur.The default value is
true
, exposing aSpringModelMBean
which performs thread context ClassLoader management. Switch this flag off to expose a standard JMXRequiredModelMBean
. -
setExcludedBeans
Set the list of names for beans that should be excluded from autodetection. -
addExcludedBean
Add the name of bean that should be excluded from autodetection. -
setListeners
Set theMBeanExporterListener
s that should be notified of MBean registration and unregistration events.- See Also:
-
setNotificationListeners
Set theNotificationListenerBeans
containing theNotificationListeners
that will be registered with theMBeanServer
. -
setNotificationListenerMappings
Set theNotificationListeners
to register with theMBeanServer
.The key of each entry in the
Map
is aString
representation of theObjectName
or the bean name of the MBean the listener should be registered for. Specifying an asterisk (*
) for a key will cause the listener to be associated with all MBeans registered by this class at startup time.The value of each entry is the
NotificationListener
to register. For more advanced options such as registeringNotificationFilters
and handback objects seesetNotificationListeners(NotificationListenerBean[])
. -
setBeanClassLoader
Description copied from interface:BeanClassLoaderAware
Callback that supplies the beanclass loader
to a bean instance.Invoked after the population of normal bean properties but before an initialization callback such as
InitializingBean's
InitializingBean.afterPropertiesSet()
method or a custom init-method.- Specified by:
setBeanClassLoader
in interfaceBeanClassLoaderAware
- Parameters:
classLoader
- the owning class loader
-
setBeanFactory
This callback is only required for resolution of bean names in the"beans"
Map
and for autodetection of MBeans (in the latter case, aListableBeanFactory
is required).- Specified by:
setBeanFactory
in interfaceBeanFactoryAware
- Parameters:
beanFactory
- owning BeanFactory (nevernull
). The bean can immediately call methods on the factory.- See Also:
-
afterPropertiesSet
public void afterPropertiesSet()Description copied from interface:InitializingBean
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,ApplicationContextAware
etc.This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.
- Specified by:
afterPropertiesSet
in interfaceInitializingBean
-
afterSingletonsInstantiated
public void afterSingletonsInstantiated()Kick off bean registration automatically after the regular singleton instantiation phase.- Specified by:
afterSingletonsInstantiated
in interfaceSmartInitializingSingleton
- See Also:
-
destroy
public void destroy()Unregisters all beans that this exported has exposed via JMX when the enclosingApplicationContext
is destroyed.- Specified by:
destroy
in interfaceDisposableBean
-
registerManagedResource
Description copied from interface:MBeanExportOperations
Register the supplied resource with JMX. If the resource is not a valid MBean already, Spring will generate a management interface for it. The exact interface generated will depend on the implementation and its configuration. This call also generates anObjectName
for the managed resource and returns this to the caller.- Specified by:
registerManagedResource
in interfaceMBeanExportOperations
- Parameters:
managedResource
- the resource to expose via JMX- Returns:
- the
ObjectName
under which the resource was exposed - Throws:
MBeanExportException
- if Spring is unable to generate anObjectName
or register the MBean
-
registerManagedResource
public void registerManagedResource(Object managedResource, ObjectName objectName) throws MBeanExportException Description copied from interface:MBeanExportOperations
Register the supplied resource with JMX. If the resource is not a valid MBean already, Spring will generate a management interface for it. The exact interface generated will depend on the implementation and its configuration.- Specified by:
registerManagedResource
in interfaceMBeanExportOperations
- Parameters:
managedResource
- the resource to expose via JMXobjectName
- theObjectName
under which to expose the resource- Throws:
MBeanExportException
- if Spring is unable to register the MBean
-
unregisterManagedResource
Description copied from interface:MBeanExportOperations
Remove the specified MBean from the underlying MBeanServer registry.- Specified by:
unregisterManagedResource
in interfaceMBeanExportOperations
- Parameters:
objectName
- theObjectName
of the resource to remove
-
registerBeans
protected void registerBeans()Register the defined beans with theMBeanServer
.Each bean is exposed to the
MBeanServer
via aModelMBean
. The actual implementation of theModelMBean
interface used depends on the implementation of theModelMBeanProvider
interface that is configured. By default, theRequiredModelMBean
class that is supplied with all JMX implementations is used.The management interface produced for each bean is dependent on the
MBeanInfoAssembler
implementation being used. TheObjectName
given to each bean is dependent on the implementation of theObjectNamingStrategy
interface being used. -
isBeanDefinitionLazyInit
Return whether the specified bean definition should be considered as lazy-init.- Parameters:
beanFactory
- the bean factory that is supposed to contain the bean definitionbeanName
- the name of the bean to check- See Also:
-
registerBeanNameOrInstance
protected ObjectName registerBeanNameOrInstance(Object mapValue, String beanKey) throws MBeanExportException Register an individual bean with theMBeanServer
.This method is responsible for deciding how a bean should be exposed to the
MBeanServer
. Specifically, if the suppliedmapValue
is the name of a bean that is configured for lazy initialization, then a proxy to the resource is registered with theMBeanServer
so that the lazy load behavior is honored. If the bean is already an MBean then it will be registered directly with theMBeanServer
without any intervention. For all other beans or bean names, the resource itself is registered with theMBeanServer
directly.- Parameters:
mapValue
- the value configured for this bean in the beans map; may be either theString
name of a bean, or the bean itselfbeanKey
- the key associated with this bean in the beans map- Returns:
- the
ObjectName
under which the resource was registered - Throws:
MBeanExportException
- if the export failed- See Also:
-
setBeans(java.util.Map<java.lang.String, java.lang.Object>)
registerBeanInstance(java.lang.Object, java.lang.String)
registerLazyInit(java.lang.String, java.lang.String)
-
getObjectName
protected ObjectName getObjectName(Object bean, @Nullable String beanKey) throws MalformedObjectNameException Retrieve theObjectName
for a bean.If the bean implements the
SelfNaming
interface, then theObjectName
will be retrieved usingSelfNaming.getObjectName()
. Otherwise, the configuredObjectNamingStrategy
is used.- Parameters:
bean
- the name of the bean in theBeanFactory
beanKey
- the key associated with the bean in the beans map- Returns:
- the
ObjectName
for the supplied bean - Throws:
MalformedObjectNameException
- if the retrievedObjectName
is malformed
-
isMBean
Determine whether the given bean class qualifies as an MBean as-is.The default implementation delegates to
JmxUtils.isMBean(java.lang.Class<?>)
, which checks forDynamicMBean
classes as well as classes with corresponding "*MBean" interface (Standard MBeans) or corresponding "*MXBean" interface (Java MXBeans).- Parameters:
beanClass
- the bean class to analyze- Returns:
- whether the class qualifies as an MBean
- See Also:
-
adaptMBeanIfPossible
Build an adapted MBean for the given bean instance, if possible.The default implementation builds a JMX 1.2 StandardMBean for the target's MBean/MXBean interface in case of an AOP proxy, delegating the interface's management operations to the proxy.
- Parameters:
bean
- the original bean instance- Returns:
- the adapted MBean, or
null
if not possible - Throws:
JMException
-
createAndConfigureMBean
protected ModelMBean createAndConfigureMBean(Object managedResource, String beanKey) throws MBeanExportException Creates an MBean that is configured with the appropriate management interface for the supplied managed resource.- Parameters:
managedResource
- the resource that is to be exported as an MBeanbeanKey
- the key associated with the managed bean- Throws:
MBeanExportException
- See Also:
-
createModelMBean()
getMBeanInfo(Object, String)
-
createModelMBean
Create an instance of a class that implementsModelMBean
.This method is called to obtain a
ModelMBean
instance to use when registering a bean. This method is called once per bean during the registration phase and must return a new instance ofModelMBean
- Returns:
- a new instance of a class that implements
ModelMBean
- Throws:
MBeanException
- if creation of the ModelMBean failed
-
onRegister
Called when an MBean is registered. Notifies all registeredMBeanExporterListeners
of the registration event.Please note that if an
MBeanExporterListener
throws a (runtime) exception when notified, this will essentially interrupt the notification process and any remaining listeners that have yet to be notified will not (obviously) receive theMBeanExporterListener.mbeanRegistered(javax.management.ObjectName)
callback.- Overrides:
onRegister
in classMBeanRegistrationSupport
- Parameters:
objectName
- theObjectName
of the registered MBean
-
onUnregister
Called when an MBean is unregistered. Notifies all registeredMBeanExporterListeners
of the unregistration event.Please note that if an
MBeanExporterListener
throws a (runtime) exception when notified, this will essentially interrupt the notification process and any remaining listeners that have yet to be notified will not (obviously) receive theMBeanExporterListener.mbeanUnregistered(javax.management.ObjectName)
callback.- Overrides:
onUnregister
in classMBeanRegistrationSupport
- Parameters:
objectName
- theObjectName
of the unregistered MBean
-
"autodetect" flag