Support ApplicationReadyEvent from PidFileWriter
Update `ApplicationPidFileWriter` to support `ApplicationReadyEvent` in addition to the already supported `ApplicationEnvironmentPreparedEvent` and `ApplicationPreparedEvent` events. Closes gh-7066 Fixes gh-7027
This commit is contained in:
parent
5a3b881e1f
commit
970dcc3fd3
|
|
@ -30,6 +30,7 @@ import org.springframework.boot.ApplicationPid;
|
||||||
import org.springframework.boot.bind.RelaxedPropertyResolver;
|
import org.springframework.boot.bind.RelaxedPropertyResolver;
|
||||||
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
|
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
|
||||||
import org.springframework.boot.context.event.ApplicationPreparedEvent;
|
import org.springframework.boot.context.event.ApplicationPreparedEvent;
|
||||||
|
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||||
import org.springframework.boot.context.event.SpringApplicationEvent;
|
import org.springframework.boot.context.event.SpringApplicationEvent;
|
||||||
import org.springframework.context.ApplicationListener;
|
import org.springframework.context.ApplicationListener;
|
||||||
import org.springframework.core.Ordered;
|
import org.springframework.core.Ordered;
|
||||||
|
|
@ -49,7 +50,8 @@ import org.springframework.util.Assert;
|
||||||
* <p>
|
* <p>
|
||||||
* Note: access to the Spring {@link Environment} is only possible when the
|
* Note: access to the Spring {@link Environment} is only possible when the
|
||||||
* {@link #setTriggerEventType(Class) triggerEventType} is set to
|
* {@link #setTriggerEventType(Class) triggerEventType} is set to
|
||||||
* {@link ApplicationEnvironmentPreparedEvent} or {@link ApplicationPreparedEvent}.
|
* {@link ApplicationEnvironmentPreparedEvent}, {@link ApplicationReadyEvent}, or
|
||||||
|
* {@link ApplicationPreparedEvent}.
|
||||||
*
|
*
|
||||||
* @author Jakub Kubrynski
|
* @author Jakub Kubrynski
|
||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
|
|
@ -231,6 +233,10 @@ public class ApplicationPidFileWriter
|
||||||
return ((ApplicationPreparedEvent) event).getApplicationContext()
|
return ((ApplicationPreparedEvent) event).getApplicationContext()
|
||||||
.getEnvironment();
|
.getEnvironment();
|
||||||
}
|
}
|
||||||
|
if (event instanceof ApplicationReadyEvent) {
|
||||||
|
return ((ApplicationReadyEvent) event).getApplicationContext()
|
||||||
|
.getEnvironment();
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@ import org.junit.rules.TemporaryFolder;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
|
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
|
||||||
import org.springframework.boot.context.event.ApplicationPreparedEvent;
|
import org.springframework.boot.context.event.ApplicationPreparedEvent;
|
||||||
|
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||||
import org.springframework.boot.context.event.ApplicationStartedEvent;
|
import org.springframework.boot.context.event.ApplicationStartedEvent;
|
||||||
import org.springframework.boot.context.event.SpringApplicationEvent;
|
import org.springframework.boot.context.event.SpringApplicationEvent;
|
||||||
import org.springframework.context.ConfigurableApplicationContext;
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
|
|
@ -99,7 +100,7 @@ public class ApplicationPidFileWriterTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void differentEventTypes() throws Exception {
|
public void tryEnvironmentPreparedEvent() throws Exception {
|
||||||
File file = this.temporaryFolder.newFile();
|
File file = this.temporaryFolder.newFile();
|
||||||
SpringApplicationEvent event = createEnvironmentPreparedEvent("spring.pid.file",
|
SpringApplicationEvent event = createEnvironmentPreparedEvent("spring.pid.file",
|
||||||
file.getAbsolutePath());
|
file.getAbsolutePath());
|
||||||
|
|
@ -111,6 +112,19 @@ public class ApplicationPidFileWriterTests {
|
||||||
assertThat(FileCopyUtils.copyToString(new FileReader(file))).isNotEmpty();
|
assertThat(FileCopyUtils.copyToString(new FileReader(file))).isNotEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void tryReadyEvent() throws Exception {
|
||||||
|
File file = this.temporaryFolder.newFile();
|
||||||
|
SpringApplicationEvent event = createReadyEvent("spring.pid.file",
|
||||||
|
file.getAbsolutePath());
|
||||||
|
ApplicationPidFileWriter listener = new ApplicationPidFileWriter();
|
||||||
|
listener.onApplicationEvent(event);
|
||||||
|
assertThat(FileCopyUtils.copyToString(new FileReader(file))).isEmpty();
|
||||||
|
listener.setTriggerEventType(ApplicationReadyEvent.class);
|
||||||
|
listener.onApplicationEvent(event);
|
||||||
|
assertThat(FileCopyUtils.copyToString(new FileReader(file))).isNotEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void withNoEnvironment() throws Exception {
|
public void withNoEnvironment() throws Exception {
|
||||||
File file = this.temporaryFolder.newFile();
|
File file = this.temporaryFolder.newFile();
|
||||||
|
|
@ -170,6 +184,15 @@ public class ApplicationPidFileWriterTests {
|
||||||
context);
|
context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private SpringApplicationEvent createReadyEvent(String propName, String propValue) {
|
||||||
|
ConfigurableEnvironment environment = createEnvironment(propName, propValue);
|
||||||
|
ConfigurableApplicationContext context = mock(
|
||||||
|
ConfigurableApplicationContext.class);
|
||||||
|
given(context.getEnvironment()).willReturn(environment);
|
||||||
|
return new ApplicationReadyEvent(new SpringApplication(), new String[] {},
|
||||||
|
context);
|
||||||
|
}
|
||||||
|
|
||||||
private ConfigurableEnvironment createEnvironment(String propName, String propValue) {
|
private ConfigurableEnvironment createEnvironment(String propName, String propValue) {
|
||||||
MockPropertySource propertySource = mockPropertySource(propName, propValue);
|
MockPropertySource propertySource = mockPropertySource(propName, propValue);
|
||||||
ConfigurableEnvironment environment = new StandardEnvironment();
|
ConfigurableEnvironment environment = new StandardEnvironment();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue