From 72ae5d5a97dec5aa42fda5d391c43bb1df833fe4 Mon Sep 17 00:00:00 2001 From: Christian Dupuis Date: Thu, 19 Dec 2013 15:49:18 +0100 Subject: [PATCH] Rename invoke JMX operation Rename invoke JMX operation to getData for endpoints that provide actuator data. Special case for ShutdownEndpoint to provide a shutdown method. --- .../endpoint/jmx/DataEndpointMBean.java | 41 +++++++++++++++++++ .../actuate/endpoint/jmx/EndpointMBean.java | 17 ++++---- .../endpoint/jmx/EndpointMBeanExporter.java | 10 ++++- .../endpoint/jmx/ShutdownEndpointMBean.java | 38 +++++++++++++++++ .../jmx/EndpointMBeanExporterTests.java | 6 +-- 5 files changed, 101 insertions(+), 11 deletions(-) create mode 100644 spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jmx/DataEndpointMBean.java create mode 100644 spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jmx/ShutdownEndpointMBean.java diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jmx/DataEndpointMBean.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jmx/DataEndpointMBean.java new file mode 100644 index 00000000000..3598a65bf39 --- /dev/null +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jmx/DataEndpointMBean.java @@ -0,0 +1,41 @@ +/* + * Copyright 2013 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.actuate.endpoint.jmx; + +import org.springframework.boot.actuate.endpoint.Endpoint; +import org.springframework.jmx.export.annotation.ManagedAttribute; +import org.springframework.jmx.export.annotation.ManagedResource; + +/** + * Simple wrapper around {@link Endpoint} implementations that provide actuator data of + * some sort. + * + * @author Christian Dupuis + */ +@ManagedResource +public class DataEndpointMBean extends EndpointMBean { + + public DataEndpointMBean(String beanName, Endpoint endpoint) { + super(beanName, endpoint); + } + + @ManagedAttribute(description = "Invoke the underlying endpoint") + public Object getData() { + return convert(getEndpoint().invoke()); + } + +} diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jmx/EndpointMBean.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jmx/EndpointMBean.java index d53662155ce..85c1f0432f5 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jmx/EndpointMBean.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jmx/EndpointMBean.java @@ -69,9 +69,16 @@ public class EndpointMBean implements SelfNaming { return this.endpoint.isSensitive(); } - @ManagedAttribute(description = "Invoke the underlying endpoint") - public Object invoke() { - Object result = this.endpoint.invoke(); + @Override + public ObjectName getObjectName() throws MalformedObjectNameException { + return this.metadataNamingStrategy.getObjectName(this, this.beanName); + } + + public Endpoint getEndpoint() { + return this.endpoint; + } + + protected Object convert(Object result) { if (result == null) { return null; } @@ -87,8 +94,4 @@ public class EndpointMBean implements SelfNaming { return this.mapper.convertValue(result, Map.class); } - @Override - public ObjectName getObjectName() throws MalformedObjectNameException { - return this.metadataNamingStrategy.getObjectName(this, this.beanName); - } } diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jmx/EndpointMBeanExporter.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jmx/EndpointMBeanExporter.java index 05c9dd9b531..7f1592032d0 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jmx/EndpointMBeanExporter.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jmx/EndpointMBeanExporter.java @@ -28,6 +28,7 @@ import org.apache.commons.logging.LogFactory; import org.springframework.beans.BeansException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.boot.actuate.endpoint.Endpoint; +import org.springframework.boot.actuate.endpoint.ShutdownEndpoint; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationListener; @@ -95,13 +96,20 @@ public class EndpointMBeanExporter implements SmartLifecycle, ApplicationContext protected void registerEndpoint(String beanName, Endpoint endpoint, MBeanExporter mbeanExporter) { try { - mbeanExporter.registerManagedResource(new EndpointMBean(beanName, endpoint)); + mbeanExporter.registerManagedResource(getEndpointMBean(beanName, endpoint)); } catch (MBeanExportException ex) { logger.error("Could not register MBean for endpoint [" + beanName + "]", ex); } } + protected EndpointMBean getEndpointMBean(String beanName, Endpoint endpoint) { + if (endpoint instanceof ShutdownEndpoint) { + return new ShutdownEndpointMBean(beanName, endpoint); + } + return new DataEndpointMBean(beanName, endpoint); + } + // SmartLifeCycle implementation public final int getPhase() { diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jmx/ShutdownEndpointMBean.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jmx/ShutdownEndpointMBean.java new file mode 100644 index 00000000000..0a48775ad77 --- /dev/null +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jmx/ShutdownEndpointMBean.java @@ -0,0 +1,38 @@ +/* + * Copyright 2013 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.actuate.endpoint.jmx; + +import org.springframework.boot.actuate.endpoint.Endpoint; +import org.springframework.boot.actuate.endpoint.ShutdownEndpoint; +import org.springframework.jmx.export.annotation.ManagedOperation; + +/** + * Special endpoint wrapper for {@link ShutdownEndpoint}. + * + * @author Christian Dupuis + */ +public class ShutdownEndpointMBean extends EndpointMBean { + + public ShutdownEndpointMBean(String beanName, Endpoint endpoint) { + super(beanName, endpoint); + } + + @ManagedOperation(description = "Shutdown the ApplicationContext") + public Object shutdown() { + return convert(getEndpoint().invoke()); + } +} diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/jmx/EndpointMBeanExporterTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/jmx/EndpointMBeanExporterTests.java index 17a183b4dcd..b0b041113cf 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/jmx/EndpointMBeanExporterTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/jmx/EndpointMBeanExporterTests.java @@ -70,8 +70,8 @@ public class EndpointMBeanExporterTests { MBeanInfo mbeanInfo = mbeanExporter.getServer().getMBeanInfo( getObjectName("endpoint1", this.context)); assertNotNull(mbeanInfo); - assertEquals(4, mbeanInfo.getOperations().length); - assertEquals(3, mbeanInfo.getAttributes().length); + assertEquals(5, mbeanInfo.getOperations().length); + assertEquals(5, mbeanInfo.getAttributes().length); } @Test @@ -127,7 +127,7 @@ public class EndpointMBeanExporterTests { private ObjectName getObjectName(String beanKey, ApplicationContext applicationContext) throws MalformedObjectNameException { - return new EndpointMBean(beanKey, + return new DataEndpointMBean(beanKey, (Endpoint) applicationContext.getBean(beanKey)).getObjectName(); }