Skip to content

Commit b81c00a

Browse files
committed
Refactor the ProcessWrapper.shutdown() method.
* Removes the use of the now deprecated signalStop() method. * Changes safeGetPid() to handle all Throwable objects. * Changes shutdown() to call stop() and then forcibly destroy the child Process if the child Process is still running. * Annotates the API with Spring's @nonnull and @nullable annotations. * Edits Javadoc.
1 parent b6d043a commit b81c00a

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/process/ProcessExecutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public static ProcessWrapper launch(File workingDirectory, String classpath, Cla
133133

134134
ProcessWrapper processWrapper = new ProcessWrapper(process, ProcessConfiguration.create(processBuilder));
135135

136-
processWrapper.register((input) -> System.err.printf("[FORK] - %s%n", input));
136+
processWrapper.register(input -> System.err.printf("[FORK] - %s%n", input));
137137
processWrapper.registerShutdownHook();
138138

139139
return processWrapper;

spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/process/ProcessUtils.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@
3636
import org.springframework.util.StringUtils;
3737

3838
/**
39-
* The {@link ProcessUtils} class is a utility class for working with Operating System (OS) {@link Process processes}.
39+
* Abstract utility class for processing Operating System (OS) {@link Process processes}.
4040
*
4141
* @author John Blum
42-
* @see File
43-
* @see Process
42+
* @see java.io.File
43+
* @see java.lang.Process
4444
* @since 0.0.1
4545
*/
4646
@SuppressWarnings("unused")
@@ -50,7 +50,6 @@ public abstract class ProcessUtils {
5050

5151
protected static final String TERM_TOKEN = "<TERM/>";
5252

53-
/* (non-Javadoc) */
5453
public static int currentPid() {
5554

5655
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();

spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/process/ProcessWrapper.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.springframework.data.gemfire.tests.util.IOUtils;
4040
import org.springframework.data.gemfire.tests.util.ThreadUtils;
4141
import org.springframework.data.gemfire.tests.util.ThrowableUtils;
42+
import org.springframework.lang.NonNull;
4243
import org.springframework.util.Assert;
4344
import org.springframework.util.StringUtils;
4445

@@ -83,7 +84,7 @@ public class ProcessWrapper {
8384

8485
private String host = DEFAULT_HOST;
8586

86-
public ProcessWrapper(Process process, ProcessConfiguration processConfiguration) {
87+
public ProcessWrapper(@NonNull Process process, @NonNull ProcessConfiguration processConfiguration) {
8788

8889
Assert.notNull(process, "Process is required");
8990

@@ -134,7 +135,7 @@ private Runnable newProcessInputStreamReaderRunnable(InputStream in) {
134135
};
135136
}
136137

137-
private Thread newThread(String name, Runnable task) {
138+
private @NonNull Thread newThread(@NonNull String name, @NonNull Runnable task) {
138139

139140
Assert.hasText(name, "Thread name is required");
140141
Assert.notNull(task, "Thread task is required");
@@ -279,6 +280,7 @@ public void signal(Object value) {
279280
}
280281
}
281282

283+
@Deprecated
282284
public void signalStop() {
283285

284286
try {
@@ -305,7 +307,8 @@ public int stop(long milliseconds) {
305307
boolean interrupted = false;
306308
int exitValue = -1;
307309
int pid = safeGetPid();
308-
long timeout = (System.currentTimeMillis() + milliseconds);
310+
long timeout = System.currentTimeMillis() + milliseconds;
311+
309312
AtomicBoolean exited = new AtomicBoolean(false);
310313

311314
ExecutorService executorService = Executors.newSingleThreadExecutor();
@@ -330,9 +333,7 @@ public int stop(long milliseconds) {
330333
}
331334
}
332335
catch (TimeoutException cause) {
333-
334336
exitValue = -1;
335-
336337
this.log.warning(String.format("Process [%1$d] did not stop within the allotted timeout of %2$d seconds%n",
337338
pid, TimeUnit.MILLISECONDS.toSeconds(milliseconds)));
338339
}
@@ -341,7 +342,6 @@ public int stop(long milliseconds) {
341342
}
342343
finally {
343344
executorService.shutdownNow();
344-
345345
if (interrupted) {
346346
Thread.currentThread().interrupt();
347347
}
@@ -356,13 +356,17 @@ public int stop(long milliseconds) {
356356

357357
public int shutdown() {
358358

359+
int pid = safeGetPid();
360+
this.log.info(String.format("Stopping process [%d]...%n", pid));
361+
359362
if (isRunning()) {
360-
this.log.info(String.format("Stopping process [%d]...%n", safeGetPid()));
361-
signalStop();
362-
waitFor();
363+
stop();
364+
if (isRunning()) {
365+
this.process.destroyForcibly();
366+
}
363367
}
364368

365-
return stop();
369+
return safeExitValue();
366370
}
367371

368372
public boolean unregister(ProcessInputStreamListener listener) {

0 commit comments

Comments
 (0)