Rename ApplicationPidListener
Rename ApplicationPidListener to ApplicationPidFileWriter (keeping the old class in a deprecated form) Fixes gh-1673
This commit is contained in:
parent
c7455040df
commit
77ccd9a80b
|
@ -0,0 +1,131 @@
|
||||||
|
/*
|
||||||
|
* 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 java.io.File;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.springframework.boot.ApplicationPid;
|
||||||
|
import org.springframework.boot.context.event.ApplicationStartedEvent;
|
||||||
|
import org.springframework.context.ApplicationListener;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An {@link ApplicationListener} that saves application PID 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
|
||||||
|
* @author Phillip Webb
|
||||||
|
* @since 1.2.0
|
||||||
|
*/
|
||||||
|
public class ApplicationPidFileWriter implements
|
||||||
|
ApplicationListener<ApplicationStartedEvent>, Ordered {
|
||||||
|
|
||||||
|
private static final Log logger = LogFactory.getLog(ApplicationPidFileWriter.class);
|
||||||
|
|
||||||
|
private static final String DEFAULT_FILE_NAME = "application.pid";
|
||||||
|
|
||||||
|
private static final String[] PROPERTY_VARIABLES = { "PIDFILE", "pidfile" };
|
||||||
|
|
||||||
|
private static final AtomicBoolean created = new AtomicBoolean(false);
|
||||||
|
|
||||||
|
private int order = Ordered.HIGHEST_PRECEDENCE + 13;
|
||||||
|
|
||||||
|
private final File file;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new {@link ApplicationPidFileWriter} instance using the filename
|
||||||
|
* 'application.pid'.
|
||||||
|
*/
|
||||||
|
public ApplicationPidFileWriter() {
|
||||||
|
this.file = new File(DEFAULT_FILE_NAME);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new {@link ApplicationPidFileWriter} instance with a specified filename.
|
||||||
|
* @param filename the name of file containing pid
|
||||||
|
*/
|
||||||
|
public ApplicationPidFileWriter(String filename) {
|
||||||
|
this(new File(filename));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new {@link ApplicationPidFileWriter} instance with a specified file.
|
||||||
|
* @param file the file containing pid
|
||||||
|
*/
|
||||||
|
public ApplicationPidFileWriter(File file) {
|
||||||
|
Assert.notNull(file, "File must not be null");
|
||||||
|
String override = getOverride();
|
||||||
|
if (override != null) {
|
||||||
|
this.file = new File(override);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.file = file;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getOverride() {
|
||||||
|
for (String property : PROPERTY_VARIABLES) {
|
||||||
|
try {
|
||||||
|
String override = System.getProperty(property);
|
||||||
|
override = (override != null ? override : System.getenv(property));
|
||||||
|
if (override != null) {
|
||||||
|
return override;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Throwable ex) {
|
||||||
|
System.err.println("Could not resolve '" + property
|
||||||
|
+ "' as system property: " + ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onApplicationEvent(ApplicationStartedEvent event) {
|
||||||
|
if (created.compareAndSet(false, true)) {
|
||||||
|
try {
|
||||||
|
new ApplicationPid().write(this.file);
|
||||||
|
this.file.deleteOnExit();
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
logger.warn(String.format("Cannot create pid file %s", this.file));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOrder(int order) {
|
||||||
|
this.order = order;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return this.order;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reset the created flag for testing purposes.
|
||||||
|
*/
|
||||||
|
static void reset() {
|
||||||
|
created.set(false);
|
||||||
|
}
|
||||||
|
}
|
|
@ -16,17 +16,6 @@
|
||||||
|
|
||||||
package org.springframework.boot.actuate.system;
|
package org.springframework.boot.actuate.system;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
|
||||||
import org.apache.commons.logging.LogFactory;
|
|
||||||
import org.springframework.boot.ApplicationPid;
|
|
||||||
import org.springframework.boot.context.event.ApplicationStartedEvent;
|
|
||||||
import org.springframework.context.ApplicationListener;
|
|
||||||
import org.springframework.core.Ordered;
|
|
||||||
import org.springframework.util.Assert;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An {@link org.springframework.context.ApplicationListener} that saves application PID
|
* An {@link org.springframework.context.ApplicationListener} that saves application PID
|
||||||
* into file. This application listener will be triggered exactly once per JVM, and the
|
* into file. This application listener will be triggered exactly once per JVM, and the
|
||||||
|
@ -37,96 +26,9 @@ import org.springframework.util.Assert;
|
||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
* @since 1.0.2
|
* @since 1.0.2
|
||||||
|
* @deprecated since 1.2.0 in favor of {@link ApplicationPidFileWriter}
|
||||||
*/
|
*/
|
||||||
public class ApplicationPidListener implements
|
@Deprecated
|
||||||
ApplicationListener<ApplicationStartedEvent>, Ordered {
|
public class ApplicationPidListener extends ApplicationPidFileWriter {
|
||||||
|
|
||||||
private static final Log logger = LogFactory.getLog(ApplicationPidListener.class);
|
|
||||||
|
|
||||||
private static final String DEFAULT_FILE_NAME = "application.pid";
|
|
||||||
|
|
||||||
private static final String[] PROPERTY_VARIABLES = { "PIDFILE", "pidfile" };
|
|
||||||
|
|
||||||
private static final AtomicBoolean created = new AtomicBoolean(false);
|
|
||||||
|
|
||||||
private int order = Ordered.HIGHEST_PRECEDENCE + 13;
|
|
||||||
|
|
||||||
private final File file;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new {@link ApplicationPidListener} instance using the filename
|
|
||||||
* 'application.pid'.
|
|
||||||
*/
|
|
||||||
public ApplicationPidListener() {
|
|
||||||
this.file = new File(DEFAULT_FILE_NAME);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new {@link ApplicationPidListener} instance with a specified filename.
|
|
||||||
* @param filename the name of file containing pid
|
|
||||||
*/
|
|
||||||
public ApplicationPidListener(String filename) {
|
|
||||||
this(new File(filename));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new {@link ApplicationPidListener} instance with a specified file.
|
|
||||||
* @param file the file containing pid
|
|
||||||
*/
|
|
||||||
public ApplicationPidListener(File file) {
|
|
||||||
Assert.notNull(file, "File must not be null");
|
|
||||||
String override = getOverride();
|
|
||||||
if (override != null) {
|
|
||||||
this.file = new File(override);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
this.file = file;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getOverride() {
|
|
||||||
for (String property : PROPERTY_VARIABLES) {
|
|
||||||
try {
|
|
||||||
String override = System.getProperty(property);
|
|
||||||
override = (override != null ? override : System.getenv(property));
|
|
||||||
if (override != null) {
|
|
||||||
return override;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Throwable ex) {
|
|
||||||
System.err.println("Could not resolve '" + property
|
|
||||||
+ "' as system property: " + ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onApplicationEvent(ApplicationStartedEvent event) {
|
|
||||||
if (created.compareAndSet(false, true)) {
|
|
||||||
try {
|
|
||||||
new ApplicationPid().write(this.file);
|
|
||||||
this.file.deleteOnExit();
|
|
||||||
}
|
|
||||||
catch (Exception ex) {
|
|
||||||
logger.warn(String.format("Cannot create pid file %s", this.file));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setOrder(int order) {
|
|
||||||
this.order = order;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getOrder() {
|
|
||||||
return this.order;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reset the created flag for testing purposes.
|
|
||||||
*/
|
|
||||||
static void reset() {
|
|
||||||
created.set(false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,12 +33,12 @@ import static org.hamcrest.Matchers.not;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for {@link ApplicationPidListener}.
|
* Tests for {@link ApplicationPidFileWriter}.
|
||||||
*
|
*
|
||||||
* @author Jakub Kubrynski
|
* @author Jakub Kubrynski
|
||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
*/
|
*/
|
||||||
public class ApplicationPidListenerTests {
|
public class ApplicationPidFileWriterTests {
|
||||||
|
|
||||||
private static final ApplicationStartedEvent EVENT = new ApplicationStartedEvent(
|
private static final ApplicationStartedEvent EVENT = new ApplicationStartedEvent(
|
||||||
new SpringApplication(), new String[] {});
|
new SpringApplication(), new String[] {});
|
||||||
|
@ -50,13 +50,13 @@ public class ApplicationPidListenerTests {
|
||||||
@After
|
@After
|
||||||
public void resetListener() {
|
public void resetListener() {
|
||||||
System.clearProperty("PIDFILE");
|
System.clearProperty("PIDFILE");
|
||||||
ApplicationPidListener.reset();
|
ApplicationPidFileWriter.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void createPidFile() throws Exception {
|
public void createPidFile() throws Exception {
|
||||||
File file = this.temporaryFolder.newFile();
|
File file = this.temporaryFolder.newFile();
|
||||||
ApplicationPidListener listener = new ApplicationPidListener(file);
|
ApplicationPidFileWriter listener = new ApplicationPidFileWriter(file);
|
||||||
listener.onApplicationEvent(EVENT);
|
listener.onApplicationEvent(EVENT);
|
||||||
assertThat(FileCopyUtils.copyToString(new FileReader(file)), not(isEmptyString()));
|
assertThat(FileCopyUtils.copyToString(new FileReader(file)), not(isEmptyString()));
|
||||||
}
|
}
|
||||||
|
@ -65,7 +65,7 @@ public class ApplicationPidListenerTests {
|
||||||
public void overridePidFile() throws Exception {
|
public void overridePidFile() throws Exception {
|
||||||
File file = this.temporaryFolder.newFile();
|
File file = this.temporaryFolder.newFile();
|
||||||
System.setProperty("PIDFILE", this.temporaryFolder.newFile().getAbsolutePath());
|
System.setProperty("PIDFILE", this.temporaryFolder.newFile().getAbsolutePath());
|
||||||
ApplicationPidListener listener = new ApplicationPidListener(file);
|
ApplicationPidFileWriter listener = new ApplicationPidFileWriter(file);
|
||||||
listener.onApplicationEvent(EVENT);
|
listener.onApplicationEvent(EVENT);
|
||||||
assertThat(
|
assertThat(
|
||||||
FileCopyUtils.copyToString(new FileReader(System.getProperty("PIDFILE"))),
|
FileCopyUtils.copyToString(new FileReader(System.getProperty("PIDFILE"))),
|
|
@ -856,7 +856,7 @@ if needed.
|
||||||
|
|
||||||
[[production-ready-process-monitoring]]
|
[[production-ready-process-monitoring]]
|
||||||
== Process monitoring
|
== Process monitoring
|
||||||
In Spring Boot Actuator you can find `ApplicationPidListener` which creates file
|
In Spring Boot Actuator you can find `ApplicationPidFileWriter` which creates file
|
||||||
containing application PID (by default in application directory and file name is
|
containing application PID (by default in application directory and file name is
|
||||||
`application.pid`). It's not activated by default, but you can do it in two simple
|
`application.pid`). It's not activated by default, but you can do it in two simple
|
||||||
ways described below.
|
ways described below.
|
||||||
|
@ -870,7 +870,7 @@ In `META-INF/spring.factories` file you have to activate the listener:
|
||||||
[indent=0]
|
[indent=0]
|
||||||
----
|
----
|
||||||
org.springframework.context.ApplicationListener=\
|
org.springframework.context.ApplicationListener=\
|
||||||
org.springframework.boot.actuate.system.ApplicationPidListener
|
org.springframework.boot.actuate.system.ApplicationPidFileWriter
|
||||||
----
|
----
|
||||||
|
|
||||||
|
|
||||||
|
@ -878,7 +878,7 @@ In `META-INF/spring.factories` file you have to activate the listener:
|
||||||
[[production-ready-process-monitoring-programmatically]]
|
[[production-ready-process-monitoring-programmatically]]
|
||||||
=== Programmatically
|
=== Programmatically
|
||||||
You can also activate this listener by invoking `SpringApplication.addListeners(...)`
|
You can also activate this listener by invoking `SpringApplication.addListeners(...)`
|
||||||
method and passing `ApplicationPidListener` object. You can also customize file name
|
method and passing `ApplicationPidFileWriter` object. You can also customize file name
|
||||||
and path through constructor.
|
and path through constructor.
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue