Using SmartLifecycle to register MBeans

Prior to this commit, MBeans were registered in a post construct
call of MBeanExporter. This commit moves that logic after the
initialization phase using the SmartLifecycle callback.

Issue: SPR-8045
This commit is contained in:
Marten Deinum
2014-06-04 13:03:11 +02:00
committed by Stephane Nicoll
parent fc91add35e
commit 2ede219e66
2 changed files with 128 additions and 33 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -51,6 +51,7 @@ import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.SmartLifecycle;
import org.springframework.core.Constants;
import org.springframework.jmx.export.assembler.AutodetectCapableMBeanInfoAssembler;
import org.springframework.jmx.export.assembler.MBeanInfoAssembler;
@@ -67,6 +68,7 @@ import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
/**
* JMX exporter that allows for exposing any <i>Spring-managed bean</i> to a
* JMX {@link javax.management.MBeanServer}, without the need to define any
@@ -98,7 +100,7 @@ import org.springframework.util.ObjectUtils;
* @see MBeanExporterListener
*/
public class MBeanExporter extends MBeanRegistrationSupport
implements MBeanExportOperations, BeanClassLoaderAware, BeanFactoryAware, InitializingBean, DisposableBean {
implements MBeanExportOperations, BeanClassLoaderAware, BeanFactoryAware, InitializingBean, DisposableBean, SmartLifecycle {
/**
* Autodetection mode indicating that no autodetection should be used.
@@ -178,6 +180,14 @@ public class MBeanExporter extends MBeanRegistrationSupport
/** Stores the BeanFactory for use in autodetection process */
private ListableBeanFactory beanFactory;
private boolean autoStartup = true;
private volatile boolean running = false;
private int phase = Integer.MAX_VALUE;
private final Object lifecycleMonitor = new Object();
/**
* Supply a {@code Map} of beans to be registered with the JMX
@@ -412,17 +422,6 @@ public class MBeanExporter extends MBeanRegistrationSupport
if (this.server == null) {
this.server = JmxUtils.locateMBeanServer();
}
try {
logger.info("Registering beans for JMX exposure on startup");
registerBeans();
registerNotificationListeners();
}
catch (RuntimeException ex) {
// Unregister beans already registered by this exporter.
unregisterNotificationListeners();
unregisterBeans();
throw ex;
}
}
/**
@@ -1054,6 +1053,78 @@ public class MBeanExporter extends MBeanRegistrationSupport
}
}
/**
* Set whether to automatically start the container after initialization.
* <p>Default is "true"; set this to "false" to allow for manual startup
* through the {@link #start()} method.
*/
public void setAutoStartup(boolean autoStartup) {
this.autoStartup = autoStartup;
}
@Override
public boolean isAutoStartup() {
return this.autoStartup;
}
@Override
public void stop(Runnable callback) {
synchronized (this.lifecycleMonitor) {
stop();
callback.run();
}
}
@Override
public void start() {
logger.info("Registering beans for JMX exposure");
synchronized (this.lifecycleMonitor) {
try {
registerBeans();
registerNotificationListeners();
} catch (RuntimeException ex) {
// Unregister beans already registered by this exporter.
unregisterNotificationListeners();
unregisterBeans();
throw ex;
}
}
running = true;
}
@Override
public void stop() {
logger.info("Unregistering JMX-exposed beans on stop");
synchronized (this.lifecycleMonitor) {
unregisterNotificationListeners();
unregisterBeans();
running = false;
}
}
@Override
public boolean isRunning() {
synchronized (this.lifecycleMonitor) {
return this.running;
}
}
/**
* Specify the phase in which this container should be started and
* stopped. The startup order proceeds from lowest to highest, and
* the shutdown order is the reverse of that. By default this value
* is Integer.MAX_VALUE meaning that this container starts as late
* as possible and stops as soon as possible.
*/
public void setPhase(int phase) {
this.phase = phase;
}
@Override
public int getPhase() {
return this.phase;
}
//---------------------------------------------------------------------
// Inner classes for internal use