parent
533e920fe5
commit
f6488c7f66
@ -0,0 +1,122 @@
|
||||
/*
|
||||
* 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.actuate.system;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.context.event.ApplicationStartedEvent;
|
||||
import org.springframework.boot.util.SystemUtils;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.event.SmartApplicationListener;
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* An {@link org.springframework.context.ApplicationListener} that saves
|
||||
* application PID into file
|
||||
*
|
||||
* @author Jakub Kubrynski
|
||||
*/
|
||||
public class ApplicationPidListener implements SmartApplicationListener {
|
||||
|
||||
private static Class<?>[] EVENT_TYPES = {ApplicationStartedEvent.class};
|
||||
|
||||
private static final String DEFAULT_PID_FILE_NAME = "application.pid";
|
||||
|
||||
private static final AtomicBoolean pidFileCreated = new AtomicBoolean(false);
|
||||
|
||||
private int order = Ordered.HIGHEST_PRECEDENCE + 13;
|
||||
|
||||
private final Log log = LogFactory.getLog(getClass());
|
||||
|
||||
private String pidFileName = DEFAULT_PID_FILE_NAME;
|
||||
|
||||
@Override
|
||||
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
|
||||
for (Class<?> type : EVENT_TYPES) {
|
||||
if (type.isAssignableFrom(eventType)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsSourceType(Class<?> sourceType) {
|
||||
return SpringApplication.class.isAssignableFrom(sourceType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ApplicationEvent applicationEvent) {
|
||||
if (pidFileCreated.get()) {
|
||||
return;
|
||||
}
|
||||
|
||||
String applicationPid;
|
||||
try {
|
||||
applicationPid = SystemUtils.getApplicationPid();
|
||||
} catch (IllegalStateException ignore) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (pidFileCreated.compareAndSet(false, true)) {
|
||||
File file = new File(pidFileName);
|
||||
FileOutputStream fileOutputStream = null;
|
||||
try {
|
||||
fileOutputStream = new FileOutputStream(file);
|
||||
fileOutputStream.write(applicationPid.getBytes());
|
||||
} catch (FileNotFoundException e) {
|
||||
log.warn(String.format("Cannot create pid file %s !", pidFileName));
|
||||
} catch (IOException e) {
|
||||
log.warn(String.format("Cannot write to pid file %s!", pidFileName));
|
||||
} finally {
|
||||
if (fileOutputStream != null) {
|
||||
try {
|
||||
fileOutputStream.close();
|
||||
} catch (IOException e) {
|
||||
log.warn(String.format("Cannot close pid file %s!", pidFileName));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setOrder(int order) {
|
||||
this.order = order;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return order;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the pid file name. This file will contains current process id.
|
||||
*
|
||||
* @param pidFileName the name of file containing pid
|
||||
*/
|
||||
public void setPidFileName(String pidFileName) {
|
||||
this.pidFileName = pidFileName;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* 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.actuate.system;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.context.event.ApplicationStartedEvent;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Created by jkubrynski@gmail.com / 2014-03-25
|
||||
*/
|
||||
public class ApplicationPidListenerTest {
|
||||
|
||||
private static final String[] NO_ARGS = {};
|
||||
|
||||
public static final String PID_FILE_NAME = "test.pid";
|
||||
|
||||
@Test
|
||||
public void shouldCreatePidFile() {
|
||||
//given
|
||||
ApplicationPidListener sut = new ApplicationPidListener();
|
||||
sut.setPidFileName(PID_FILE_NAME);
|
||||
|
||||
//when
|
||||
sut.onApplicationEvent(new ApplicationStartedEvent(
|
||||
new SpringApplication(), NO_ARGS));
|
||||
|
||||
//then
|
||||
File pidFile = new File(PID_FILE_NAME);
|
||||
assertTrue(pidFile.exists());
|
||||
pidFile.delete();
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue