mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Allow override of pidfile location
The file name can now be overridden at runtime with a System property or environment variable named "PIDFILE" (or "pidfile"). Fixes gh-1579
This commit is contained in:
parent
0ff511d978
commit
c3d1241e71
@ -26,10 +26,13 @@ import org.springframework.boot.context.event.ApplicationStartedEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.SystemPropertyUtils;
|
||||
|
||||
/**
|
||||
* An {@link org.springframework.context.ApplicationListener} that saves application PID
|
||||
* into file. This application listener will be triggered exactly once per JVM.
|
||||
* into file. This application listener will be triggered exactly once per JVM, and the file
|
||||
* name can be overridden at runtime with a System property or environment variable named
|
||||
* "PIDFILE" (or "pidfile").
|
||||
*
|
||||
* @author Jakub Kubrynski
|
||||
* @author Dave Syer
|
||||
@ -62,8 +65,7 @@ public class ApplicationPidListener implements
|
||||
* @param filename the name of file containing pid
|
||||
*/
|
||||
public ApplicationPidListener(String filename) {
|
||||
Assert.notNull(filename, "Filename must not be null");
|
||||
this.file = new File(filename);
|
||||
this(new File(filename));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -72,7 +74,10 @@ public class ApplicationPidListener implements
|
||||
*/
|
||||
public ApplicationPidListener(File file) {
|
||||
Assert.notNull(file, "File must not be null");
|
||||
this.file = file;
|
||||
String actual = SystemPropertyUtils.resolvePlaceholders("${PIDFILE}", true);
|
||||
actual = !actual.contains("$") ? actual : SystemPropertyUtils.resolvePlaceholders("${pidfile}", true);
|
||||
actual = !actual.contains("$") ? actual : file.getAbsolutePath();
|
||||
this.file = new File(actual);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -33,12 +33,12 @@ import static org.hamcrest.Matchers.not;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Tests fpr {@link ApplicationPidListener}.
|
||||
* Tests for {@link ApplicationPidListener}.
|
||||
*
|
||||
* @author Jakub Kubrynski
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class ApplicationPidListenerTest {
|
||||
public class ApplicationPidListenerTests {
|
||||
|
||||
private static final ApplicationStartedEvent EVENT = new ApplicationStartedEvent(
|
||||
new SpringApplication(), new String[] {});
|
||||
@ -49,6 +49,7 @@ public class ApplicationPidListenerTest {
|
||||
@Before
|
||||
@After
|
||||
public void resetListener() {
|
||||
System.clearProperty("PIDFILE");
|
||||
ApplicationPidListener.reset();
|
||||
}
|
||||
|
||||
@ -60,4 +61,13 @@ public class ApplicationPidListenerTest {
|
||||
assertThat(FileCopyUtils.copyToString(new FileReader(file)), not(isEmptyString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void overridePidFile() throws Exception {
|
||||
File file = this.temporaryFolder.newFile();
|
||||
System.setProperty("PIDFILE", this.temporaryFolder.newFile().getAbsolutePath());
|
||||
ApplicationPidListener listener = new ApplicationPidListener(file);
|
||||
listener.onApplicationEvent(EVENT);
|
||||
assertThat(FileCopyUtils.copyToString(new FileReader(System.getProperty("PIDFILE"))), not(isEmptyString()));
|
||||
}
|
||||
|
||||
}
|
@ -34,7 +34,7 @@ public class ApplicationStartedEvent extends SpringApplicationEvent {
|
||||
|
||||
/**
|
||||
* @param application the current application
|
||||
* @param args the argumemts the application is running with
|
||||
* @param args the arguments the application is running with
|
||||
*/
|
||||
public ApplicationStartedEvent(SpringApplication application, String[] args) {
|
||||
super(application, args);
|
||||
|
Loading…
Reference in New Issue
Block a user