-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartAndKillProcess.java
48 lines (38 loc) · 1.55 KB
/
StartAndKillProcess.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.github.vitmonk.javanotes.process;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang.time.StopWatch;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Starting and killing process programmatically.
*/
public class StartAndKillProcess {
private static final Logger LOGGER = LoggerFactory.getLogger(StartAndKillProcess.class);
private static final int PROCESS_LIFESPAN_IN_SECONDS = 8;
@Test
public void testProcessKill() throws Exception {
// java.lang.ProcessBuilder
Process process = new ProcessBuilder("ping", "bing.com", "-n", "100").start();
// java.lang.Runtime
// Process process = Runtime.getRuntime().exec("ping bing.com -n 100");
LOGGER.info("Started process");
StopWatch stopWatch = new StopWatch();
stopWatch.start();
BufferedReader consoleOutput = new BufferedReader(new InputStreamReader(process.getInputStream()));
String outputLine = null;
while ((outputLine = consoleOutput.readLine()) != null) {
if (processShouldBeKilled(stopWatch)) {
process.destroyForcibly();
LOGGER.info("DESTROY PROCESS EMITTED!");
}
LOGGER.info(outputLine);
}
LOGGER.info("Process destroyed");
}
private boolean processShouldBeKilled(StopWatch stopWatch) {
return TimeUnit.MILLISECONDS.toSeconds(stopWatch.getTime()) > PROCESS_LIFESPAN_IN_SECONDS;
}
}