Skip to content

Commit c2db9fa

Browse files
committed
Update admin MBean to only be ready when its own context is ready
Previously, if there was a hierarchy of SpringApplications, the admin MBean would report that the application was ready as soon as any application in the hierarchy was ready. This could lead to a client trying to query a property in the environment before it's available. This commit updates the MBean registrar to that the MBean only reports that the application is ready when the context that contains the registrar has refreshed and fired its ApplicationReadyEvent. Closes gh-6362
1 parent 4963cfd commit c2db9fa

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

spring-boot/src/main/java/org/springframework/boot/admin/SpringApplicationAdminMXBeanRegistrar.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -44,6 +44,7 @@
4444
* {@link MBeanServer}.
4545
*
4646
* @author Stephane Nicoll
47+
* @author Andy Wilkinson
4748
* @since 1.3.0
4849
*/
4950
public class SpringApplicationAdminMXBeanRegistrar
@@ -80,7 +81,9 @@ public void setEnvironment(Environment environment) {
8081

8182
@Override
8283
public void onApplicationEvent(ApplicationReadyEvent event) {
83-
this.ready = true;
84+
if (this.applicationContext.equals(event.getApplicationContext())) {
85+
this.ready = true;
86+
}
8487
}
8588

8689
@Override

spring-boot/src/test/java/org/springframework/boot/admin/SpringApplicationAdminMXBeanRegistrarTests.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,20 +30,26 @@
3030
import org.junit.rules.ExpectedException;
3131

3232
import org.springframework.boot.SpringApplication;
33+
import org.springframework.boot.context.event.ApplicationReadyEvent;
3334
import org.springframework.context.ApplicationListener;
3435
import org.springframework.context.ConfigurableApplicationContext;
3536
import org.springframework.context.annotation.Bean;
3637
import org.springframework.context.annotation.Configuration;
3738
import org.springframework.context.event.ContextRefreshedEvent;
39+
import org.springframework.test.util.ReflectionTestUtils;
3840

3941
import static org.hamcrest.CoreMatchers.is;
4042
import static org.hamcrest.CoreMatchers.nullValue;
43+
import static org.junit.Assert.assertFalse;
4144
import static org.junit.Assert.assertThat;
45+
import static org.junit.Assert.assertTrue;
46+
import static org.mockito.Mockito.mock;
4247

4348
/**
4449
* Tests for {@link SpringApplicationAdminMXBeanRegistrar}.
4550
*
4651
* @author Stephane Nicoll
52+
* @author Andy Wilkinson
4753
*/
4854
public class SpringApplicationAdminMXBeanRegistrarTests {
4955

@@ -89,6 +95,25 @@ public void onApplicationEvent(ContextRefreshedEvent event) {
8995
assertThat(isApplicationReady(objectName), is(true));
9096
}
9197

98+
@Test
99+
public void eventsFromOtherContextsAreIgnored() throws MalformedObjectNameException {
100+
SpringApplicationAdminMXBeanRegistrar registrar = new SpringApplicationAdminMXBeanRegistrar(
101+
OBJECT_NAME);
102+
ConfigurableApplicationContext context = mock(
103+
ConfigurableApplicationContext.class);
104+
registrar.setApplicationContext(context);
105+
registrar.onApplicationEvent(new ApplicationReadyEvent(new SpringApplication(),
106+
null, mock(ConfigurableApplicationContext.class)));
107+
assertFalse(isApplicationReady(registrar));
108+
registrar.onApplicationEvent(
109+
new ApplicationReadyEvent(new SpringApplication(), null, context));
110+
assertTrue(isApplicationReady(registrar));
111+
}
112+
113+
private boolean isApplicationReady(SpringApplicationAdminMXBeanRegistrar registrar) {
114+
return (Boolean) ReflectionTestUtils.getField(registrar, "ready");
115+
}
116+
92117
@Test
93118
public void environmentIsExposed() {
94119
final ObjectName objectName = createObjectName(OBJECT_NAME);

0 commit comments

Comments
 (0)