From b931cce386bec52abcb6db80b6c73994dba8274e Mon Sep 17 00:00:00 2001 From: Christian Dupuis Date: Wed, 11 Dec 2013 21:30:56 +0100 Subject: [PATCH] Auto configuration support for JMX export --- .../jmx/JmxAutoConfiguration.java | 47 +++++++ .../main/resources/META-INF/spring.factories | 1 + .../jmx/JmxAutoConfigurationTests.java | 115 ++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jmx/JmxAutoConfiguration.java create mode 100644 spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jmx/JmxAutoConfigurationTests.java diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jmx/JmxAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jmx/JmxAutoConfiguration.java new file mode 100644 index 0000000000..1274092a44 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jmx/JmxAutoConfiguration.java @@ -0,0 +1,47 @@ +/* + * 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.autoconfigure.jmx; + +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.EnableMBeanExport; +import org.springframework.jmx.export.MBeanExporter; + +/** + * {@link EnableAutoConfiguration Auto-configuration} to enable/disiable Spring's + * {@link EnableMBeanExport} mechanism based on configuration properties. + * + *

+ * To enable auto export of annotation beans set spring.jmx.enabled: true. + * + * @author Christian Dupuis + */ +@Configuration +@ConditionalOnClass({ MBeanExporter.class }) +@ConditionalOnMissingBean({ MBeanExporter.class }) +@ConditionalOnExpression("${spring.jmx.enabled:false}") +public class JmxAutoConfiguration { + + @Configuration + @EnableMBeanExport(defaultDomain = "${spring.jmx.default_domain:}", server = "${spring.jmx.server:}") + public static class MBeanExport { + } + +} diff --git a/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories index ce99bdcbab..ed699f8673 100644 --- a/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories @@ -11,6 +11,7 @@ org.springframework.boot.autoconfigure.redis.RedisAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\ org.springframework.boot.autoconfigure.jms.JmsTemplateAutoConfiguration,\ +org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration,\ org.springframework.boot.autoconfigure.mobile.DeviceResolverAutoConfiguration,\ org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\ org.springframework.boot.autoconfigure.reactor.ReactorAutoConfiguration,\ diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jmx/JmxAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jmx/JmxAutoConfigurationTests.java new file mode 100644 index 0000000000..77e52ac8d8 --- /dev/null +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jmx/JmxAutoConfigurationTests.java @@ -0,0 +1,115 @@ +/* + * 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.autoconfigure.jmx; + +import org.junit.After; +import org.junit.Test; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.jmx.export.MBeanExporter; +import org.springframework.jmx.export.annotation.ManagedAttribute; +import org.springframework.jmx.export.annotation.ManagedOperation; +import org.springframework.jmx.export.annotation.ManagedResource; +import org.springframework.mock.env.MockEnvironment; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +/** + * Tests for {@link JmxAutoConfiguration} + * + * @author Christian Dupuis + */ +public class JmxAutoConfigurationTests { + + private AnnotationConfigApplicationContext context; + + @After + public void tearDown() { + if (this.context != null) { + this.context.close(); + } + } + + @Test(expected = NoSuchBeanDefinitionException.class) + public void testDefaultMBeanExport() { + this.context = new AnnotationConfigApplicationContext(); + this.context.register(JmxAutoConfiguration.class); + this.context.refresh(); + + this.context.getBean(MBeanExporter.class); + fail(); + } + + @Test(expected = NoSuchBeanDefinitionException.class) + public void testDisabledMBeanExport() { + MockEnvironment env = new MockEnvironment(); + env.setProperty("spring.jmx.enabled", "false"); + this.context = new AnnotationConfigApplicationContext(); + this.context.setEnvironment(env); + this.context.register(TestConfiguration.class, JmxAutoConfiguration.class); + this.context.refresh(); + + this.context.getBean(MBeanExporter.class); + fail(); + } + + @Test + public void testDefaultDomainConfiguredOnMBeanExport() { + MockEnvironment env = new MockEnvironment(); + env.setProperty("spring.jmx.enabled", "true"); + env.setProperty("spring.jmx.default_domain", "my-test-domain"); + this.context = new AnnotationConfigApplicationContext(); + this.context.setEnvironment(env); + this.context.register(TestConfiguration.class, JmxAutoConfiguration.class); + this.context.refresh(); + + MBeanExporter mBeanExporter = this.context.getBean(MBeanExporter.class); + assertNotNull(mBeanExporter); + + // TODO cdupuis add test for default domain + } + + @Configuration + public static class TestConfiguration { + + @Bean + public Counter counter() { + return new Counter(); + } + + @ManagedResource + public static class Counter { + + private int counter = 0; + + @ManagedAttribute + public int get() { + return this.counter; + } + + @ManagedOperation + public void increment() { + this.counter++; + } + + } + + } +}