Rename SpringApplicationLifecycle => Admin

Rename SpringApplicationLifecycle JMX beans to SpringApplicationAdmin
and relocate to a dedicated package.

Fixes gh-3124
This commit is contained in:
Phillip Webb
2015-06-09 16:06:28 -07:00
parent f8f4bd6c95
commit 105039cdb2
12 changed files with 92 additions and 70 deletions

View File

@@ -31,8 +31,8 @@ public class SampleApplication {
public static void main(String[] args) throws Exception {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName name = new ObjectName(
"org.springframework.boot:type=Lifecycle,name=springApplicationLifecycle");
SpringApplicationLifecycle mbean = new SpringApplicationLifecycle();
"org.springframework.boot:type=SpringApplicationAdmin,name=springApplicationAdmin");
SpringApplicationAdmin mbean = new SpringApplicationAdmin();
mbs.registerMBean(mbean, name);
// Flag the app as ready
@@ -51,7 +51,7 @@ public class SampleApplication {
}
}
public interface SpringApplicationLifecycleMXBean {
public interface SpringApplicationAdminMXBean {
boolean isReady();
@@ -59,7 +59,7 @@ public class SampleApplication {
}
static class SpringApplicationLifecycle implements SpringApplicationLifecycleMXBean {
static class SpringApplicationAdmin implements SpringApplicationAdminMXBean {
private boolean ready;

View File

@@ -30,8 +30,8 @@ public class SampleApplication {
public static void main(String[] args) throws Exception {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName name = new ObjectName(
"org.springframework.boot:type=Lifecycle,name=springApplicationLifecycle");
SpringApplicationLifecycle mbean = new SpringApplicationLifecycle();
"org.springframework.boot:type=SpringApplicationAdmin,name=springApplicationAdmin");
SpringApplicationAdmin mbean = new SpringApplicationAdmin();
mbs.registerMBean(mbean, name);
// Flag the app as ready
@@ -50,7 +50,7 @@ public class SampleApplication {
}
}
public interface SpringApplicationLifecycleMXBean {
public interface SpringApplicationAdminMXBean {
boolean isReady();
@@ -58,7 +58,7 @@ public class SampleApplication {
}
static class SpringApplicationLifecycle implements SpringApplicationLifecycleMXBean {
static class SpringApplicationAdmin implements SpringApplicationAdminMXBean {
private boolean ready;

View File

@@ -32,39 +32,25 @@ import javax.management.remote.JMXServiceURL;
import org.apache.maven.plugin.MojoExecutionException;
/**
* A JMX client for the {@code SpringApplicationLifecycle} mbean. Permits to obtain
* information about the lifecycle of a given Spring application.
* A JMX client for the {@code SpringApplicationAdmin} MBean. Permits to obtain
* information about a given Spring application.
*
* @author Stephane Nicoll
*/
class SpringApplicationLifecycleClient {
class SpringApplicationAdminClient {
// Note: see SpringApplicationLifecycleAutoConfiguration
static final String DEFAULT_OBJECT_NAME = "org.springframework.boot:type=Lifecycle,name=springApplicationLifecycle";
// Note: see SpringApplicationAdminJmxAutoConfiguration
static final String DEFAULT_OBJECT_NAME = "org.springframework.boot:type=SpringApplicationAdmin,name=springApplicationAdmin";
private final MBeanServerConnection connection;
private final ObjectName objectName;
public SpringApplicationLifecycleClient(MBeanServerConnection connection,
String jmxName) {
public SpringApplicationAdminClient(MBeanServerConnection connection, String jmxName) {
this.connection = connection;
this.objectName = toObjectName(jmxName);
}
/**
* Create a connector for an {@link javax.management.MBeanServer} exposed on the
* current machine and the current port. Security should be disabled.
* @param port the port on which the mbean server is exposed
* @return a connection
* @throws IOException if the connection to that server failed
*/
public static JMXConnector createLocalJmxConnector(int port) throws IOException {
String url = "service:jmx:rmi:///jndi/rmi://127.0.0.1:" + port + "/jmxrmi";
JMXServiceURL serviceUrl = new JMXServiceURL(url);
return JMXConnectorFactory.connect(serviceUrl, null);
}
/**
* Check if the spring application managed by this instance is ready. Returns
* {@code false} if the mbean is not yet deployed so this method should be repeatedly
@@ -123,4 +109,17 @@ class SpringApplicationLifecycleClient {
}
}
/**
* Create a connector for an {@link javax.management.MBeanServer} exposed on the
* current machine and the current port. Security should be disabled.
* @param port the port on which the mbean server is exposed
* @return a connection
* @throws IOException if the connection to that server failed
*/
public static JMXConnector connect(int port) throws IOException {
String url = "service:jmx:rmi:///jndi/rmi://127.0.0.1:" + port + "/jmxrmi";
JMXServiceURL serviceUrl = new JMXServiceURL(url);
return JMXConnectorFactory.connect(serviceUrl, null);
}
}

View File

@@ -59,7 +59,7 @@ public class StartMojo extends AbstractRunMojo {
* spring application.
*/
@Parameter
private String jmxName = SpringApplicationLifecycleClient.DEFAULT_OBJECT_NAME;
private String jmxName = SpringApplicationAdminClient.DEFAULT_OBJECT_NAME;
/**
* The port to use to expose the platform MBeanServer if the application needs to be
@@ -151,11 +151,11 @@ public class StartMojo extends AbstractRunMojo {
private void waitForSpringApplication(long wait, int maxAttempts)
throws MojoExecutionException {
SpringApplicationLifecycleClient helper = new SpringApplicationLifecycleClient(
SpringApplicationAdminClient client = new SpringApplicationAdminClient(
ManagementFactory.getPlatformMBeanServer(), this.jmxName);
getLog().debug("Waiting for spring application to start...");
for (int i = 0; i < maxAttempts; i++) {
if (helper.isReady()) {
if (client.isReady()) {
return;
}
String message = "Spring application is not ready yet, waiting " + wait
@@ -227,7 +227,7 @@ public class StartMojo extends AbstractRunMojo {
private void doWaitForSpringApplication(MBeanServerConnection connection)
throws IOException, MojoExecutionException, MojoFailureException {
final SpringApplicationLifecycleClient client = new SpringApplicationLifecycleClient(
final SpringApplicationAdminClient client = new SpringApplicationAdminClient(
connection, this.jmxName);
try {
execute(this.wait, this.maxAttempts, new Callable<Boolean>() {
@@ -294,8 +294,7 @@ public class StartMojo extends AbstractRunMojo {
@Override
public JMXConnector call() throws Exception {
try {
return SpringApplicationLifecycleClient
.createLocalJmxConnector(this.port);
return SpringApplicationAdminClient.connect(this.port);
}
catch (IOException ex) {
if (hasCauseWithType(ex, ConnectException.class)) {

View File

@@ -53,7 +53,7 @@ public class StopMojo extends AbstractMojo {
* application.
*/
@Parameter
private String jmxName = SpringApplicationLifecycleClient.DEFAULT_OBJECT_NAME;
private String jmxName = SpringApplicationAdminClient.DEFAULT_OBJECT_NAME;
/**
* The port to use to lookup the platform MBeanServer if the application has been
@@ -81,8 +81,7 @@ public class StopMojo extends AbstractMojo {
private void stopForkedProcess() throws IOException, MojoFailureException,
MojoExecutionException {
JMXConnector connector = SpringApplicationLifecycleClient
.createLocalJmxConnector(this.jmxPort);
JMXConnector connector = SpringApplicationAdminClient.connect(this.jmxPort);
try {
MBeanServerConnection connection = connector.getMBeanServerConnection();
doStop(connection);
@@ -99,7 +98,7 @@ public class StopMojo extends AbstractMojo {
private void doStop(MBeanServerConnection connection) throws IOException,
MojoExecutionException {
try {
new SpringApplicationLifecycleClient(connection, this.jmxName).stop();
new SpringApplicationAdminClient(connection, this.jmxName).stop();
}
catch (InstanceNotFoundException ex) {
throw new MojoExecutionException(