Skip to content

Commit f6488c7

Browse files
jkubrynskiDave Syer
authored andcommitted
Now after starting application PID file will be created
Fixes spring-projectsgh-550
1 parent 533e920 commit f6488c7

File tree

6 files changed

+313
-21
lines changed

6 files changed

+313
-21
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* Copyright 2010-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.system;
18+
19+
import org.apache.commons.logging.Log;
20+
import org.apache.commons.logging.LogFactory;
21+
import org.springframework.boot.SpringApplication;
22+
import org.springframework.boot.context.event.ApplicationStartedEvent;
23+
import org.springframework.boot.util.SystemUtils;
24+
import org.springframework.context.ApplicationEvent;
25+
import org.springframework.context.event.SmartApplicationListener;
26+
import org.springframework.core.Ordered;
27+
28+
import java.io.File;
29+
import java.io.FileNotFoundException;
30+
import java.io.FileOutputStream;
31+
import java.io.IOException;
32+
import java.util.concurrent.atomic.AtomicBoolean;
33+
34+
/**
35+
* An {@link org.springframework.context.ApplicationListener} that saves
36+
* application PID into file
37+
*
38+
* @author Jakub Kubrynski
39+
*/
40+
public class ApplicationPidListener implements SmartApplicationListener {
41+
42+
private static Class<?>[] EVENT_TYPES = {ApplicationStartedEvent.class};
43+
44+
private static final String DEFAULT_PID_FILE_NAME = "application.pid";
45+
46+
private static final AtomicBoolean pidFileCreated = new AtomicBoolean(false);
47+
48+
private int order = Ordered.HIGHEST_PRECEDENCE + 13;
49+
50+
private final Log log = LogFactory.getLog(getClass());
51+
52+
private String pidFileName = DEFAULT_PID_FILE_NAME;
53+
54+
@Override
55+
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
56+
for (Class<?> type : EVENT_TYPES) {
57+
if (type.isAssignableFrom(eventType)) {
58+
return true;
59+
}
60+
}
61+
return false;
62+
}
63+
64+
@Override
65+
public boolean supportsSourceType(Class<?> sourceType) {
66+
return SpringApplication.class.isAssignableFrom(sourceType);
67+
}
68+
69+
@Override
70+
public void onApplicationEvent(ApplicationEvent applicationEvent) {
71+
if (pidFileCreated.get()) {
72+
return;
73+
}
74+
75+
String applicationPid;
76+
try {
77+
applicationPid = SystemUtils.getApplicationPid();
78+
} catch (IllegalStateException ignore) {
79+
return;
80+
}
81+
82+
if (pidFileCreated.compareAndSet(false, true)) {
83+
File file = new File(pidFileName);
84+
FileOutputStream fileOutputStream = null;
85+
try {
86+
fileOutputStream = new FileOutputStream(file);
87+
fileOutputStream.write(applicationPid.getBytes());
88+
} catch (FileNotFoundException e) {
89+
log.warn(String.format("Cannot create pid file %s !", pidFileName));
90+
} catch (IOException e) {
91+
log.warn(String.format("Cannot write to pid file %s!", pidFileName));
92+
} finally {
93+
if (fileOutputStream != null) {
94+
try {
95+
fileOutputStream.close();
96+
} catch (IOException e) {
97+
log.warn(String.format("Cannot close pid file %s!", pidFileName));
98+
}
99+
}
100+
}
101+
}
102+
}
103+
104+
public void setOrder(int order) {
105+
this.order = order;
106+
}
107+
108+
@Override
109+
public int getOrder() {
110+
return order;
111+
}
112+
113+
/**
114+
* Sets the pid file name. This file will contains current process id.
115+
*
116+
* @param pidFileName the name of file containing pid
117+
*/
118+
public void setPidFileName(String pidFileName) {
119+
this.pidFileName = pidFileName;
120+
}
121+
122+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2010-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.system;
18+
19+
import org.junit.Test;
20+
import org.springframework.boot.SpringApplication;
21+
import org.springframework.boot.context.event.ApplicationStartedEvent;
22+
23+
import java.io.File;
24+
25+
import static org.junit.Assert.assertTrue;
26+
27+
/**
28+
* Created by jkubrynski@gmail.com / 2014-03-25
29+
*/
30+
public class ApplicationPidListenerTest {
31+
32+
private static final String[] NO_ARGS = {};
33+
34+
public static final String PID_FILE_NAME = "test.pid";
35+
36+
@Test
37+
public void shouldCreatePidFile() {
38+
//given
39+
ApplicationPidListener sut = new ApplicationPidListener();
40+
sut.setPidFileName(PID_FILE_NAME);
41+
42+
//when
43+
sut.onApplicationEvent(new ApplicationStartedEvent(
44+
new SpringApplication(), NO_ARGS));
45+
46+
//then
47+
File pidFile = new File(PID_FILE_NAME);
48+
assertTrue(pidFile.exists());
49+
pidFile.delete();
50+
}
51+
52+
}

spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,12 @@
1616

1717
package org.springframework.boot.logging;
1818

19-
import java.lang.management.ManagementFactory;
20-
import java.util.HashMap;
21-
import java.util.List;
22-
import java.util.Map;
23-
2419
import org.apache.commons.logging.Log;
2520
import org.apache.commons.logging.LogFactory;
2621
import org.springframework.boot.SpringApplication;
2722
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
2823
import org.springframework.boot.context.event.ApplicationStartedEvent;
24+
import org.springframework.boot.util.SystemUtils;
2925
import org.springframework.context.ApplicationEvent;
3026
import org.springframework.context.ApplicationListener;
3127
import org.springframework.context.event.SmartApplicationListener;
@@ -37,6 +33,10 @@
3733
import org.springframework.util.MultiValueMap;
3834
import org.springframework.util.ResourceUtils;
3935

36+
import java.util.HashMap;
37+
import java.util.List;
38+
import java.util.Map;
39+
4040
/**
4141
* An {@link ApplicationListener} that configures a logging framework depending on what it
4242
* finds on the classpath and in the {@link Environment}. If the environment contains a
@@ -69,11 +69,14 @@
6969
public class LoggingApplicationListener implements SmartApplicationListener {
7070

7171
private static final Map<String, String> ENVIRONMENT_SYSTEM_PROPERTY_MAPPING;
72+
73+
public static final String PID_KEY = "PID";
74+
7275
static {
7376
ENVIRONMENT_SYSTEM_PROPERTY_MAPPING = new HashMap<String, String>();
7477
ENVIRONMENT_SYSTEM_PROPERTY_MAPPING.put("logging.file", "LOG_FILE");
7578
ENVIRONMENT_SYSTEM_PROPERTY_MAPPING.put("logging.path", "LOG_PATH");
76-
ENVIRONMENT_SYSTEM_PROPERTY_MAPPING.put("PID", "PID");
79+
ENVIRONMENT_SYSTEM_PROPERTY_MAPPING.put(PID_KEY, PID_KEY);
7780
}
7881

7982
private static MultiValueMap<LogLevel, String> LOG_LEVEL_LOGGERS;
@@ -120,8 +123,14 @@ public void onApplicationEvent(ApplicationEvent event) {
120123
.getClassLoader());
121124
}
122125
else {
123-
if (System.getProperty("PID") == null) {
124-
System.setProperty("PID", getPid());
126+
if (System.getProperty(PID_KEY) == null) {
127+
String applicationPid;
128+
try {
129+
applicationPid = SystemUtils.getApplicationPid();
130+
} catch (IllegalStateException e) {
131+
applicationPid = "????";
132+
}
133+
System.setProperty(PID_KEY, applicationPid);
125134
}
126135
LoggingSystem loggingSystem = LoggingSystem.get(ClassUtils
127136
.getDefaultClassLoader());
@@ -190,19 +199,6 @@ protected void initializeLogLevel(LoggingSystem system, LogLevel level) {
190199
}
191200
}
192201

193-
private String getPid() {
194-
try {
195-
String name = ManagementFactory.getRuntimeMXBean().getName();
196-
if (name != null) {
197-
return name.split("@")[0];
198-
}
199-
}
200-
catch (Throwable ex) {
201-
// Ignore
202-
}
203-
return "????";
204-
}
205-
206202
public void setOrder(int order) {
207203
this.order = order;
208204
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2010-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.util;
18+
19+
import org.apache.commons.logging.Log;
20+
import org.apache.commons.logging.LogFactory;
21+
import org.springframework.util.StringUtils;
22+
23+
import java.lang.management.ManagementFactory;
24+
import java.lang.management.RuntimeMXBean;
25+
26+
/**
27+
* Class containing methods related to system utilities
28+
*
29+
* @author Jakub Kubrynski
30+
*/
31+
public class SystemUtils {
32+
33+
private static final Log LOG = LogFactory.getLog(SystemUtils.class);
34+
35+
/**
36+
* Looks for application PID
37+
* @return application PID
38+
* @throws java.lang.IllegalStateException if PID could not be determined
39+
*/
40+
public static String getApplicationPid() {
41+
String pid = null;
42+
try {
43+
RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();
44+
String jvmName = runtimeBean.getName();
45+
if (StringUtils.isEmpty(jvmName)) {
46+
LOG.warn("Cannot get JVM name");
47+
}
48+
if (!jvmName.contains("@")) {
49+
LOG.warn("JVM name doesn't contain process id");
50+
}
51+
pid = jvmName.split("@")[0];
52+
} catch (Throwable e) {
53+
LOG.warn("Cannot get RuntimeMXBean", e);
54+
}
55+
56+
if (pid == null) {
57+
throw new IllegalStateException("Application PID not found");
58+
}
59+
60+
return pid;
61+
}
62+
63+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Utility classes
19+
*/
20+
package org.springframework.boot.util;
21+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2010-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.util;
18+
19+
import org.junit.Test;
20+
21+
import static org.junit.Assert.assertNotNull;
22+
23+
/**
24+
* Tests for {@link org.springframework.boot.util.SystemUtils}.
25+
*
26+
* @author Jakub Kubrynski
27+
*/
28+
public class SystemUtilsTest {
29+
30+
@Test
31+
public void shouldGetApplicationPid() throws Exception {
32+
//when
33+
String applicationPid = SystemUtils.getApplicationPid();
34+
35+
//then
36+
assertNotNull(applicationPid);
37+
}
38+
}

0 commit comments

Comments
 (0)