Skip to content

Commit c6657aa

Browse files
committed
Merge pull request #7066 from youngm/pidotherevents
* pr/7066: Support ApplicationReadyEvent from PidFileWriter
2 parents 5a3b881 + 970dcc3 commit c6657aa

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

spring-boot/src/main/java/org/springframework/boot/system/ApplicationPidFileWriter.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.boot.bind.RelaxedPropertyResolver;
3131
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
3232
import org.springframework.boot.context.event.ApplicationPreparedEvent;
33+
import org.springframework.boot.context.event.ApplicationReadyEvent;
3334
import org.springframework.boot.context.event.SpringApplicationEvent;
3435
import org.springframework.context.ApplicationListener;
3536
import org.springframework.core.Ordered;
@@ -49,7 +50,8 @@
4950
* <p>
5051
* Note: access to the Spring {@link Environment} is only possible when the
5152
* {@link #setTriggerEventType(Class) triggerEventType} is set to
52-
* {@link ApplicationEnvironmentPreparedEvent} or {@link ApplicationPreparedEvent}.
53+
* {@link ApplicationEnvironmentPreparedEvent}, {@link ApplicationReadyEvent}, or
54+
* {@link ApplicationPreparedEvent}.
5355
*
5456
* @author Jakub Kubrynski
5557
* @author Dave Syer
@@ -231,6 +233,10 @@ private Environment getEnvironment(SpringApplicationEvent event) {
231233
return ((ApplicationPreparedEvent) event).getApplicationContext()
232234
.getEnvironment();
233235
}
236+
if (event instanceof ApplicationReadyEvent) {
237+
return ((ApplicationReadyEvent) event).getApplicationContext()
238+
.getEnvironment();
239+
}
234240
return null;
235241
}
236242

spring-boot/src/test/java/org/springframework/boot/system/ApplicationPidFileWriterTests.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.boot.SpringApplication;
3030
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
3131
import org.springframework.boot.context.event.ApplicationPreparedEvent;
32+
import org.springframework.boot.context.event.ApplicationReadyEvent;
3233
import org.springframework.boot.context.event.ApplicationStartedEvent;
3334
import org.springframework.boot.context.event.SpringApplicationEvent;
3435
import org.springframework.context.ConfigurableApplicationContext;
@@ -99,7 +100,7 @@ public void overridePidFileWithSpring() throws Exception {
99100
}
100101

101102
@Test
102-
public void differentEventTypes() throws Exception {
103+
public void tryEnvironmentPreparedEvent() throws Exception {
103104
File file = this.temporaryFolder.newFile();
104105
SpringApplicationEvent event = createEnvironmentPreparedEvent("spring.pid.file",
105106
file.getAbsolutePath());
@@ -111,6 +112,19 @@ public void differentEventTypes() throws Exception {
111112
assertThat(FileCopyUtils.copyToString(new FileReader(file))).isNotEmpty();
112113
}
113114

115+
@Test
116+
public void tryReadyEvent() throws Exception {
117+
File file = this.temporaryFolder.newFile();
118+
SpringApplicationEvent event = createReadyEvent("spring.pid.file",
119+
file.getAbsolutePath());
120+
ApplicationPidFileWriter listener = new ApplicationPidFileWriter();
121+
listener.onApplicationEvent(event);
122+
assertThat(FileCopyUtils.copyToString(new FileReader(file))).isEmpty();
123+
listener.setTriggerEventType(ApplicationReadyEvent.class);
124+
listener.onApplicationEvent(event);
125+
assertThat(FileCopyUtils.copyToString(new FileReader(file))).isNotEmpty();
126+
}
127+
114128
@Test
115129
public void withNoEnvironment() throws Exception {
116130
File file = this.temporaryFolder.newFile();
@@ -170,6 +184,15 @@ private SpringApplicationEvent createPreparedEvent(String propName,
170184
context);
171185
}
172186

187+
private SpringApplicationEvent createReadyEvent(String propName, String propValue) {
188+
ConfigurableEnvironment environment = createEnvironment(propName, propValue);
189+
ConfigurableApplicationContext context = mock(
190+
ConfigurableApplicationContext.class);
191+
given(context.getEnvironment()).willReturn(environment);
192+
return new ApplicationReadyEvent(new SpringApplication(), new String[] {},
193+
context);
194+
}
195+
173196
private ConfigurableEnvironment createEnvironment(String propName, String propValue) {
174197
MockPropertySource propertySource = mockPropertySource(propName, propValue);
175198
ConfigurableEnvironment environment = new StandardEnvironment();

0 commit comments

Comments
 (0)