org.springframework.jmx.export
Class MBeanExporter

java.lang.Object
  extended by org.springframework.jmx.export.MBeanExporter
All Implemented Interfaces:
BeanFactoryAware, DisposableBean, InitializingBean

public class MBeanExporter
extends Object
implements BeanFactoryAware, InitializingBean, DisposableBean

A bean that allows for any Spring-managed bean to be exposed to a JMX MBeanServer, without the need to define any JMX-specific information in the bean classes.

If the bean implements one of the JMX management interfaces then MBeanExporter can simply register the MBean with the server automatically, through its autodetection process.

If the bean does not implement one of the JMX management interfaces then MBeanExporter will create the management information using the supplied MBeanInfoAssembler implementation.

A list of MBeanExporterListeners can be registered via the listeners property, allowing application code to be notified of MBean registration and unregistration events.

Since:
1.2
Author:
Rob Harrop, Juergen Hoeller
See Also:
setBeans(java.util.Map), setAutodetect(boolean), setAssembler(org.springframework.jmx.export.assembler.MBeanInfoAssembler), setListeners(org.springframework.jmx.export.MBeanExporterListener[]), MBeanInfoAssembler, MBeanExporterListener

Field Summary
protected  org.apache.commons.logging.Log logger
          Log instance for this class.
static int REGISTRATION_FAIL_ON_EXISTING
          Constant indicating that registration should fail when attempting to register an MBean under a name that already exists.
static int REGISTRATION_IGNORE_EXISTING
          Constant indicating that registration should ignore the affected MBean when attempting to register an MBean under a name that already exists.
static int REGISTRATION_REPLACE_EXISTING
          Constant indicating that registration should replace the affected MBean when attempting to register an MBean under a name that already exists.
 
Constructor Summary
MBeanExporter()
           
 
Method Summary
 void afterPropertiesSet()
          Start bean registration automatically when deployed in an ApplicationContext.
protected  ModelMBean createModelMBean()
          Create an instance of a class that implements ModelMBean.
 void destroy()
          Unregisters all beans that this exported has exposed via JMX when the enclosing ApplicationContext is destroyed.
protected  void doRegister(Object mbean, ObjectName objectName)
          Actually registers the MBean with the server.
protected  boolean isBeanDefinitionLazyInit(ListableBeanFactory beanFactory, String beanName)
          Return whether the specified bean definition should be considered as lazy-init.
protected  void registerBeans()
          Registers the defined beans with the MBeanServer.
 void setAssembler(MBeanInfoAssembler assembler)
          Set the implementation of the MBeanInfoAssembler 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 setBeanFactory(BeanFactory beanFactory)
          This callback is only required for resolution of bean names in the "beans" Map and for autodetection of MBeans (in the latter case, a ListableBeanFactory is required).
 void setBeans(Map beans)
          Supply a Map of beans to be registered with the JMX MBeanServer.
 void setExcludedBeans(String[] excludedBeans)
          Set the list of names for beans that should be excluded from autodetection.
 void setListeners(MBeanExporterListener[] listeners)
          Set the MBeanExporterListeners that should be notified of MBean registration and unregistration events.
 void setNamingStrategy(ObjectNamingStrategy namingStrategy)
          Set the implementation of the ObjectNamingStrategy interface to use for this exporter.
 void setRegistrationBehavior(int registrationBehavior)
          Specify what action should be taken when attempting to register an MBean under an ObjectName that already exists.
 void setRegistrationBehaviorName(String registrationBehavior)
          Set the registration behavior by the name of the corresponding constant, e.g.
 void setServer(MBeanServer server)
          Specify the instance MBeanServer with which all beans should be registered.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REGISTRATION_FAIL_ON_EXISTING

public static final int REGISTRATION_FAIL_ON_EXISTING
Constant indicating that registration should fail when attempting to register an MBean under a name that already exists.

This is the default registration behavior.

See Also:
Constant Field Values

REGISTRATION_IGNORE_EXISTING

public static final int REGISTRATION_IGNORE_EXISTING
Constant indicating that registration should ignore the affected MBean when attempting to register an MBean under a name that already exists.

See Also:
Constant Field Values

REGISTRATION_REPLACE_EXISTING

public static final int REGISTRATION_REPLACE_EXISTING
Constant indicating that registration should replace the affected MBean when attempting to register an MBean under a name that already exists.

See Also:
Constant Field Values

logger

protected final org.apache.commons.logging.Log logger
Log instance for this class.

Constructor Detail

MBeanExporter

public MBeanExporter()
Method Detail

setServer

public void setServer(MBeanServer server)
Specify the instance MBeanServer with which all beans should be registered. The MBeanExporter will attempt to locate an existing MBeanServer if none is supplied.


setBeans

public void setBeans(Map beans)
Supply a Map of beans to be registered with the JMX MBeanServer.

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 custom NamingStrategy.

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 - Map with bean instances or bean names as values
See Also:
setNamingStrategy(org.springframework.jmx.export.naming.ObjectNamingStrategy), KeyNamingStrategy, ObjectName.ObjectName(String)

setAutodetect

public void setAutodetect(boolean autodetect)
Set whether to autodetect MBeans in the bean factory that this exporter runs in. Will also ask an AutodetectCapableMBeanInfoAssembler if available.

This feature is turned off by default. Explicitly specify "true" here to enable autodetection.

See Also:
setAssembler(org.springframework.jmx.export.assembler.MBeanInfoAssembler), AutodetectCapableMBeanInfoAssembler, JmxUtils.isMBean(java.lang.Class)

setExcludedBeans

public void setExcludedBeans(String[] excludedBeans)
Set the list of names for beans that should be excluded from autodetection.


setAssembler

public void setAssembler(MBeanInfoAssembler assembler)
Set the implementation of the MBeanInfoAssembler interface to use for this exporter. Default is a SimpleReflectiveMBeanInfoAssembler.

The passed-in assembler can optionally implement the AutodetectCapableMBeanInfoAssembler interface, which enables it to particiapte in the exporter's MBean autodetection process.

See Also:
SimpleReflectiveMBeanInfoAssembler, AutodetectCapableMBeanInfoAssembler, MetadataMBeanInfoAssembler, setAutodetect(boolean)

setNamingStrategy

public void setNamingStrategy(ObjectNamingStrategy namingStrategy)
Set the implementation of the ObjectNamingStrategy interface to use for this exporter. Default is a KeyNamingStrategy.

See Also:
KeyNamingStrategy, MetadataNamingStrategy

setListeners

public void setListeners(MBeanExporterListener[] listeners)
Set the MBeanExporterListeners that should be notified of MBean registration and unregistration events.

See Also:
MBeanExporterListener

setRegistrationBehaviorName

public void setRegistrationBehaviorName(String registrationBehavior)
Set the registration behavior by the name of the corresponding constant, e.g. "REGISTRATION_IGNORE_EXISTING".

See Also:
setRegistrationBehavior(int), REGISTRATION_FAIL_ON_EXISTING, REGISTRATION_IGNORE_EXISTING, REGISTRATION_REPLACE_EXISTING

setRegistrationBehavior

public void setRegistrationBehavior(int registrationBehavior)
Specify what action should be taken when attempting to register an MBean under an ObjectName that already exists.

Default is REGISTRATION_FAIL_ON_EXISTING.

See Also:
setRegistrationBehaviorName(String), REGISTRATION_FAIL_ON_EXISTING, REGISTRATION_IGNORE_EXISTING, REGISTRATION_REPLACE_EXISTING

setBeanFactory

public void setBeanFactory(BeanFactory beanFactory)
This callback is only required for resolution of bean names in the "beans" Map and for autodetection of MBeans (in the latter case, a ListableBeanFactory is required).

Specified by:
setBeanFactory in interface BeanFactoryAware
Parameters:
beanFactory - owning BeanFactory (may not be null). The bean can immediately call methods on the factory.
See Also:
setBeans(java.util.Map), setAutodetect(boolean), ListableBeanFactory

afterPropertiesSet

public void afterPropertiesSet()
                        throws JMException
Start bean registration automatically when deployed in an ApplicationContext.

Specified by:
afterPropertiesSet in interface InitializingBean
Throws:
JMException
See Also:
registerBeans()

registerBeans

protected void registerBeans()
                      throws JMException
Registers the defined beans with the MBeanServer. Each bean is exposed to the MBeanServer via a ModelMBean. The actual implemetation of the ModelMBean interface used depends on the implementation of the ModelMBeanProvider interface that is configured. By default the RequiredModelMBean 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. The ObjectName given to each bean is dependent on the implementation of the ObjectNamingStrategy interface being used.

Throws:
JMException

doRegister

protected void doRegister(Object mbean,
                          ObjectName objectName)
                   throws JMException
Actually registers the MBean with the server. The behavior when encountering an existing MBean can be configured using the setRegistrationBehavior(int) and setRegistrationBehaviorName(String) methods.

Throws:
JMException

isBeanDefinitionLazyInit

protected boolean isBeanDefinitionLazyInit(ListableBeanFactory beanFactory,
                                           String beanName)
Return whether the specified bean definition should be considered as lazy-init.

Parameters:
beanFactory - the bean factory that is supposed to contain the bean definition
beanName - the name of the bean to check
See Also:
ConfigurableListableBeanFactory.getBeanDefinition(java.lang.String), BeanDefinition.isLazyInit()

createModelMBean

protected ModelMBean createModelMBean()
                               throws MBeanException
Create an instance of a class that implements ModelMBean.

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 of ModelMBean

Returns:
a new instance of a class that implements ModelMBean
Throws:
MBeanException - if creation of the ModelMBean failed

destroy

public void destroy()
Unregisters all beans that this exported has exposed via JMX when the enclosing ApplicationContext is destroyed.

Specified by:
destroy in interface DisposableBean


Copyright (c) 2002-2006 The Spring Framework Project.