Auto configuration support for JMX export
This commit is contained in:
parent
cf05a5d578
commit
b931cce386
|
|
@ -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.
|
||||
*
|
||||
* <p>
|
||||
* To enable auto export of annotation beans set <code>spring.jmx.enabled: true</code>.
|
||||
*
|
||||
* @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 {
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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,\
|
||||
|
|
|
|||
|
|
@ -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++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue