📄 mbeanexporter.java
字号:
*/
public void setRegistrationBehaviorName(String registrationBehavior) {
setRegistrationBehavior(constants.asNumber(registrationBehavior).intValue());
}
/**
* Specify what action should be taken when attempting to register an MBean
* under an {@link javax.management.ObjectName} that already exists.
* <p>Default is REGISTRATION_FAIL_ON_EXISTING.
* @see #setRegistrationBehaviorName(String)
* @see #REGISTRATION_FAIL_ON_EXISTING
* @see #REGISTRATION_IGNORE_EXISTING
* @see #REGISTRATION_REPLACE_EXISTING
*/
public void setRegistrationBehavior(int registrationBehavior) {
this.registrationBehavior = registrationBehavior;
}
/**
* This callback is only required for resolution of bean names in the "beans"
* <code>Map</code> and for autodetection of MBeans (in the latter case,
* a <code>ListableBeanFactory</code> is required).
* @see #setBeans
* @see #setAutodetect
* @see org.springframework.beans.factory.ListableBeanFactory
*/
public void setBeanFactory(BeanFactory beanFactory) {
if (beanFactory instanceof ListableBeanFactory) {
this.beanFactory = (ListableBeanFactory) beanFactory;
}
else {
logger.info("Not running in a ListableBeanFactory: autodetection of MBeans not available");
}
}
/**
* Start bean registration automatically when deployed in an
* <code>ApplicationContext</code>.
* @see #registerBeans()
*/
public void afterPropertiesSet() throws JMException {
// register the beans now
registerBeans();
}
/**
* Registers the defined beans with the <code>MBeanServer</code>. Each bean is exposed
* to the <code>MBeanServer</code> via a <code>ModelMBean</code>. The actual implemetation
* of the <code>ModelMBean</code> interface used depends on the implementation of the
* <code>ModelMBeanProvider</code> interface that is configured. By default the
* <code>RequiredModelMBean</code> class that is supplied with all JMX implementations
* is used.
* <p>The management interface produced for each bean is dependent on the
* <code>MBeanInfoAssembler</code> implementation being used.
* The <code>ObjectName</code> given to each bean is dependent on the implementation
* of the <code>ObjectNamingStrategy</code> interface being used.
*/
protected void registerBeans() throws JMException {
// If no server was provided then try to find one.
// This is useful in an environment such as JDK 1.5, Tomcat
// or JBoss where there is already an MBeanServer loaded.
if (this.server == null) {
this.server = JmxUtils.locateMBeanServer();
}
// The beans property may be <code>null</code>, for example
// if we are relying solely on autodetection.
if (this.beans == null) {
this.beans = new HashMap();
}
// Perform autodetection, if desired.
if (this.autodetect) {
if (this.beanFactory == null) {
throw new JMException("Cannot autodetect MBeans if not running in a BeanFactory");
}
// Autodetect any beans that are already MBeans.
logger.info("Autodetecting user-defined JMX MBeans");
autodetectMBeans();
// Allow the metadata assembler a chance to vote for bean inclusion.
if (this.assembler instanceof AutodetectCapableMBeanInfoAssembler) {
autodetectBeans((AutodetectCapableMBeanInfoAssembler) this.assembler);
}
}
// Check we now have at least one bean.
if (this.beans.isEmpty()) {
throw new IllegalArgumentException("Must specify at least one bean for registration");
}
this.registeredBeans = new HashSet(this.beans.size());
try {
for (Iterator it = this.beans.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
String beanKey = (String) entry.getKey();
Object value = entry.getValue();
ObjectName objectName = registerBeanNameOrInstance(value, beanKey);
if (objectName != null) {
this.registeredBeans.add(objectName);
notifyListenersOfRegistration(objectName);
}
}
}
catch (InvalidTargetObjectTypeException ex) {
// Unregister beans already registered by this exporter.
unregisterBeans();
// We should never get this!
throw new JMException("An invalid object type was used when specifying a managed resource. " +
"This is a serious error and points to an error in the Spring JMX code. Root cause: " +
ex.getMessage());
}
catch (JMException ex) {
// Unregister beans already registered by this exporter.
unregisterBeans();
throw ex;
}
}
/**
* Registers an individual bean with the <code>MBeanServer</code>. This method
* is responsible for deciding <strong>how</strong> a bean should be exposed
* to the <code>MBeanServer</code>. Specifically, if the <code>mapValue</code>
* is the name of a bean that is configured for lazy initialization, then
* a proxy to the resource is registered with the <code>MBeanServer</code>
* so that the the lazy load behavior is honored. If the bean is already an
* MBean then it will be registered directly with the <code>MBeanServer</code>
* without any intervention. For all other beans or bean names, the resource
* itself is registered with the <code>MBeanServer</code> directly.
* @param beanKey the key associated with this bean in the beans map
* @param mapValue the value configured for this bean in the beans map.
* May be either the <code>String</code> name of a bean, or the bean itself.
* @return the <code>ObjectName</code> under which the resource was registered
* @throws JMException in case of an error in the underlying JMX infrastructure
* @throws InvalidTargetObjectTypeException an error in the definition of the MBean resource
* @see #setBeans
* @see #registerLazyInit
* @see #registerMBean
* @see #registerSimpleBean
*/
private ObjectName registerBeanNameOrInstance(Object mapValue, String beanKey)
throws JMException, InvalidTargetObjectTypeException {
if (mapValue instanceof String) {
// Bean name pointing to a potentially lazy-init bean in the factory.
if (this.beanFactory == null) {
throw new JMException("Cannot resolve bean names if not running in a BeanFactory");
}
String beanName = (String) mapValue;
if (isBeanDefinitionLazyInit(this.beanFactory, beanName)) {
if (logger.isDebugEnabled()) {
logger.debug("Found bean name for lazy init bean with key [" + beanKey +
"]. Registering bean with lazy init support.");
}
return registerLazyInit(beanName, beanKey);
}
else {
if (logger.isDebugEnabled()) {
logger.debug("String value under key [" + beanKey + "] points to a bean that was not " +
"registered for lazy initialization. Registering bean normally with JMX server.");
}
Object bean = this.beanFactory.getBean(beanName);
return registerBeanInstance(bean, beanKey);
}
}
else {
// Plain bean instance -> register it directly.
return registerBeanInstance(mapValue, beanKey);
}
}
/**
* Registers an existing MBean or an MBean adapter for a plain bean
* with the <code>MBeanServer</code>.
* @param beanInstance the bean to register, either an MBean or a plain bean
* @param beanKey the key associated with this bean in the beans map
* @return the <code>ObjectName</code> under which the bean was registered
* with the <code>MBeanServer</code>
* @throws JMException an error in the underlying JMX infrastructure
*/
private ObjectName registerBeanInstance(Object beanInstance, String beanKey)
throws JMException, InvalidTargetObjectTypeException {
if (JmxUtils.isMBean(beanInstance.getClass())) {
if (logger.isDebugEnabled()) {
logger.debug("Located MBean under key [" + beanKey + "]: registering with JMX server");
}
return registerMBean(beanInstance, beanKey);
}
else {
if (logger.isDebugEnabled()) {
logger.debug("Located bean under key [" + beanKey + "] registering with JMX server.");
}
return registerSimpleBean(beanInstance, beanKey);
}
}
/**
* Registers an existing MBean with the <code>MBeanServer</code>.
* @param mbean the bean instance to register
* @param beanKey the key associated with this bean in the beans map
* @return the <code>ObjectName</code> under which the bean was registered
* with the <code>MBeanServer</code>
* @throws JMException an error in the underlying JMX infrastructure
*/
private ObjectName registerMBean(Object mbean, String beanKey) throws JMException {
try {
ObjectName objectName = getObjectName(mbean, beanKey);
if (logger.isDebugEnabled()) {
logger.debug("Registering MBean [" + objectName + "]");
}
doRegister(mbean, objectName);
return objectName;
}
catch (MalformedObjectNameException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Unable to register MBean [" + mbean + "] with key [" + beanKey + "]");
}
return null;
}
}
/**
* Registers a plain bean as MBean with the <code>MBeanServer</code>.
* The management interface for the bean is created by the configured
* <code>MBeanInfoAssembler</code>.
* @param bean the bean instance to register
* @param beanKey the key associated with this bean in the beans map
* @return the <code>ObjectName</code> under which the bean was registered
* with the <code>MBeanServer</code>
* @throws JMException in case of an error in the underlying JMX infrastructure
* @throws InvalidTargetObjectTypeException an error in the definition of the MBean resource
*/
private ObjectName registerSimpleBean(Object bean, String beanKey)
throws JMException, InvalidTargetObjectTypeException {
ObjectName objectName = getObjectName(bean, beanKey);
if (logger.isDebugEnabled()) {
logger.debug("Registering and assembling MBean [" + objectName + "]");
}
ModelMBean mbean = createModelMBean();
mbean.setModelMBeanInfo(getMBeanInfo(bean, beanKey));
mbean.setManagedResource(bean, MR_TYPE_OBJECT_REFERENCE);
doRegister(mbean, objectName);
return objectName;
}
/**
* Registers beans that are configured for lazy initialization with the
* <code>MBeanServer<code> indirectly through a proxy.
* @param beanName the name of the bean in the <code>BeanFactory</code>
* @param beanKey the key associated with this bean in the beans map
* @return the <code>ObjectName</code> under which the bean was registered
* with the <code>MBeanServer</code>
* @throws JMException an error in the underlying JMX infrastructure
* @throws InvalidTargetObjectTypeException an error in the definition of the MBean resource
*/
private ObjectName registerLazyInit(String beanName, String beanKey)
throws JMException, InvalidTargetObjectTypeException {
LazyInitTargetSource targetSource = new LazyInitTargetSource();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -