Skip to content

Commit c3d1241

Browse files
author
Dave Syer
committed
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
1 parent 0ff511d commit c3d1241

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/system/ApplicationPidListener.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@
2626
import org.springframework.context.ApplicationListener;
2727
import org.springframework.core.Ordered;
2828
import org.springframework.util.Assert;
29+
import org.springframework.util.SystemPropertyUtils;
2930

3031
/**
3132
* An {@link org.springframework.context.ApplicationListener} that saves application PID
32-
* into file. This application listener will be triggered exactly once per JVM.
33+
* into file. This application listener will be triggered exactly once per JVM, and the file
34+
* name can be overridden at runtime with a System property or environment variable named
35+
* "PIDFILE" (or "pidfile").
3336
*
3437
* @author Jakub Kubrynski
3538
* @author Dave Syer
@@ -62,8 +65,7 @@ public ApplicationPidListener() {
6265
* @param filename the name of file containing pid
6366
*/
6467
public ApplicationPidListener(String filename) {
65-
Assert.notNull(filename, "Filename must not be null");
66-
this.file = new File(filename);
68+
this(new File(filename));
6769
}
6870

6971
/**
@@ -72,7 +74,10 @@ public ApplicationPidListener(String filename) {
7274
*/
7375
public ApplicationPidListener(File file) {
7476
Assert.notNull(file, "File must not be null");
75-
this.file = file;
77+
String actual = SystemPropertyUtils.resolvePlaceholders("${PIDFILE}", true);
78+
actual = !actual.contains("$") ? actual : SystemPropertyUtils.resolvePlaceholders("${pidfile}", true);
79+
actual = !actual.contains("$") ? actual : file.getAbsolutePath();
80+
this.file = new File(actual);
7681
}
7782

7883
@Override
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333
import static org.junit.Assert.assertThat;
3434

3535
/**
36-
* Tests fpr {@link ApplicationPidListener}.
36+
* Tests for {@link ApplicationPidListener}.
3737
*
3838
* @author Jakub Kubrynski
3939
* @author Dave Syer
4040
*/
41-
public class ApplicationPidListenerTest {
41+
public class ApplicationPidListenerTests {
4242

4343
private static final ApplicationStartedEvent EVENT = new ApplicationStartedEvent(
4444
new SpringApplication(), new String[] {});
@@ -49,6 +49,7 @@ public class ApplicationPidListenerTest {
4949
@Before
5050
@After
5151
public void resetListener() {
52+
System.clearProperty("PIDFILE");
5253
ApplicationPidListener.reset();
5354
}
5455

@@ -60,4 +61,13 @@ public void createPidFile() throws Exception {
6061
assertThat(FileCopyUtils.copyToString(new FileReader(file)), not(isEmptyString()));
6162
}
6263

64+
@Test
65+
public void overridePidFile() throws Exception {
66+
File file = this.temporaryFolder.newFile();
67+
System.setProperty("PIDFILE", this.temporaryFolder.newFile().getAbsolutePath());
68+
ApplicationPidListener listener = new ApplicationPidListener(file);
69+
listener.onApplicationEvent(EVENT);
70+
assertThat(FileCopyUtils.copyToString(new FileReader(System.getProperty("PIDFILE"))), not(isEmptyString()));
71+
}
72+
6373
}

spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationStartedEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class ApplicationStartedEvent extends SpringApplicationEvent {
3434

3535
/**
3636
* @param application the current application
37-
* @param args the argumemts the application is running with
37+
* @param args the arguments the application is running with
3838
*/
3939
public ApplicationStartedEvent(SpringApplication application, String[] args) {
4040
super(application, args);

0 commit comments

Comments
 (0)