Now after starting application PID file will be created

Fixes gh-550
This commit is contained in:
Jakub Kubrynski
2014-03-23 00:11:49 +01:00
committed by Dave Syer
parent 533e920fe5
commit f6488c7f66
6 changed files with 313 additions and 21 deletions

View File

@@ -16,16 +16,12 @@
package org.springframework.boot.logging;
import java.lang.management.ManagementFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.boot.util.SystemUtils;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.SmartApplicationListener;
@@ -37,6 +33,10 @@ import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ResourceUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* An {@link ApplicationListener} that configures a logging framework depending on what it
* finds on the classpath and in the {@link Environment}. If the environment contains a
@@ -69,11 +69,14 @@ import org.springframework.util.ResourceUtils;
public class LoggingApplicationListener implements SmartApplicationListener {
private static final Map<String, String> ENVIRONMENT_SYSTEM_PROPERTY_MAPPING;
public static final String PID_KEY = "PID";
static {
ENVIRONMENT_SYSTEM_PROPERTY_MAPPING = new HashMap<String, String>();
ENVIRONMENT_SYSTEM_PROPERTY_MAPPING.put("logging.file", "LOG_FILE");
ENVIRONMENT_SYSTEM_PROPERTY_MAPPING.put("logging.path", "LOG_PATH");
ENVIRONMENT_SYSTEM_PROPERTY_MAPPING.put("PID", "PID");
ENVIRONMENT_SYSTEM_PROPERTY_MAPPING.put(PID_KEY, PID_KEY);
}
private static MultiValueMap<LogLevel, String> LOG_LEVEL_LOGGERS;
@@ -120,8 +123,14 @@ public class LoggingApplicationListener implements SmartApplicationListener {
.getClassLoader());
}
else {
if (System.getProperty("PID") == null) {
System.setProperty("PID", getPid());
if (System.getProperty(PID_KEY) == null) {
String applicationPid;
try {
applicationPid = SystemUtils.getApplicationPid();
} catch (IllegalStateException e) {
applicationPid = "????";
}
System.setProperty(PID_KEY, applicationPid);
}
LoggingSystem loggingSystem = LoggingSystem.get(ClassUtils
.getDefaultClassLoader());
@@ -190,19 +199,6 @@ public class LoggingApplicationListener implements SmartApplicationListener {
}
}
private String getPid() {
try {
String name = ManagementFactory.getRuntimeMXBean().getName();
if (name != null) {
return name.split("@")[0];
}
}
catch (Throwable ex) {
// Ignore
}
return "????";
}
public void setOrder(int order) {
this.order = order;
}

View File

@@ -0,0 +1,63 @@
/*
* Copyright 2010-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.StringUtils;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
/**
* Class containing methods related to system utilities
*
* @author Jakub Kubrynski
*/
public class SystemUtils {
private static final Log LOG = LogFactory.getLog(SystemUtils.class);
/**
* Looks for application PID
* @return application PID
* @throws java.lang.IllegalStateException if PID could not be determined
*/
public static String getApplicationPid() {
String pid = null;
try {
RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();
String jvmName = runtimeBean.getName();
if (StringUtils.isEmpty(jvmName)) {
LOG.warn("Cannot get JVM name");
}
if (!jvmName.contains("@")) {
LOG.warn("JVM name doesn't contain process id");
}
pid = jvmName.split("@")[0];
} catch (Throwable e) {
LOG.warn("Cannot get RuntimeMXBean", e);
}
if (pid == null) {
throw new IllegalStateException("Application PID not found");
}
return pid;
}
}

View File

@@ -0,0 +1,21 @@
/*
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Utility classes
*/
package org.springframework.boot.util;

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2010-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.util;
import org.junit.Test;
import static org.junit.Assert.assertNotNull;
/**
* Tests for {@link org.springframework.boot.util.SystemUtils}.
*
* @author Jakub Kubrynski
*/
public class SystemUtilsTest {
@Test
public void shouldGetApplicationPid() throws Exception {
//when
String applicationPid = SystemUtils.getApplicationPid();
//then
assertNotNull(applicationPid);
}
}