Commit 72ae5d5a authored by Christian Dupuis's avatar Christian Dupuis

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.
parent 31f7807a
/*
* 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());
}
}
......@@ -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);
}
}
......@@ -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() {
......
/*
* 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());
}
}
......@@ -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();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment