|
4 | 4 | import java.net.ServerSocket;
|
5 | 5 | import java.util.List;
|
6 | 6 | import java.util.Random;
|
| 7 | +import java.util.concurrent.Callable; |
| 8 | +import java.util.concurrent.ExecutorService; |
| 9 | +import java.util.concurrent.Executors; |
| 10 | +import java.util.concurrent.Future; |
| 11 | +import java.util.concurrent.TimeUnit; |
| 12 | +import java.util.concurrent.TimeoutException; |
7 | 13 |
|
8 | 14 | import javax.net.ServerSocketFactory;
|
9 | 15 |
|
@@ -32,33 +38,71 @@ private SocketUtils() {
|
32 | 38 | }
|
33 | 39 |
|
34 | 40 | public static long findTcpListenProcess(int port) {
|
| 41 | + // Add a timeout of 5 seconds to prevent blocking |
| 42 | + final int TIMEOUT_SECONDS = 5; |
| 43 | + ExecutorService executor = Executors.newSingleThreadExecutor(); |
35 | 44 | try {
|
36 |
| - if (OSUtils.isWindows()) { |
37 |
| - String[] command = { "netstat", "-ano", "-p", "TCP" }; |
38 |
| - List<String> lines = ExecutingCommand.runNative(command); |
39 |
| - for (String line : lines) { |
40 |
| - if (line.contains("LISTENING")) { |
41 |
| - // TCP 0.0.0.0:49168 0.0.0.0:0 LISTENING 476 |
42 |
| - String[] strings = line.trim().split("\\s+"); |
43 |
| - if (strings.length == 5) { |
44 |
| - if (strings[1].endsWith(":" + port)) { |
45 |
| - return Long.parseLong(strings[4]); |
46 |
| - } |
47 |
| - } |
48 |
| - } |
| 45 | + Future<Long> future = executor.submit(new Callable<Long>() { |
| 46 | + @Override |
| 47 | + public Long call() throws Exception { |
| 48 | + return doFindTcpListenProcess(port); |
49 | 49 | }
|
| 50 | + }); |
| 51 | + |
| 52 | + try { |
| 53 | + return future.get(TIMEOUT_SECONDS, TimeUnit.SECONDS); |
| 54 | + } catch (TimeoutException e) { |
| 55 | + future.cancel(true); |
| 56 | + return -1; |
| 57 | + } catch (Exception e) { |
| 58 | + return -1; |
| 59 | + } |
| 60 | + } finally { |
| 61 | + executor.shutdownNow(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private static long doFindTcpListenProcess(int port) { |
| 66 | + try { |
| 67 | + if (OSUtils.isWindows()) { |
| 68 | + return findTcpListenProcessOnWindows(port); |
50 | 69 | }
|
51 | 70 |
|
52 | 71 | if (OSUtils.isLinux() || OSUtils.isMac()) {
|
53 |
| - String pid = ExecutingCommand.getFirstAnswer("lsof -t -s TCP:LISTEN -i TCP:" + port); |
54 |
| - if (!pid.trim().isEmpty()) { |
55 |
| - return Long.parseLong(pid); |
56 |
| - } |
| 72 | + return findTcpListenProcessOnUnix(port); |
57 | 73 | }
|
58 | 74 | } catch (Throwable e) {
|
59 | 75 | // ignore
|
60 | 76 | }
|
| 77 | + return -1; |
| 78 | + } |
| 79 | + |
| 80 | + private static long findTcpListenProcessOnWindows(int port) { |
| 81 | + String[] command = { "netstat", "-ano", "-p", "TCP" }; |
| 82 | + List<String> lines = ExecutingCommand.runNative(command); |
| 83 | + for (String line : lines) { |
| 84 | + if (line.contains("LISTENING")) { |
| 85 | + // TCP 0.0.0.0:49168 0.0.0.0:0 LISTENING 476 |
| 86 | + String[] strings = line.trim().split("\\s+"); |
| 87 | + if (strings.length == 5) { |
| 88 | + if (strings[1].endsWith(":" + port)) { |
| 89 | + return Long.parseLong(strings[4]); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + return -1; |
| 95 | + } |
61 | 96 |
|
| 97 | + private static long findTcpListenProcessOnUnix(int port) { |
| 98 | + String pid = ExecutingCommand.getFirstAnswer("lsof -t -s TCP:LISTEN -i TCP:" + port); |
| 99 | + if (pid != null && !pid.trim().isEmpty()) { |
| 100 | + try { |
| 101 | + return Long.parseLong(pid.trim()); |
| 102 | + } catch (NumberFormatException e) { |
| 103 | + // ignore |
| 104 | + } |
| 105 | + } |
62 | 106 | return -1;
|
63 | 107 | }
|
64 | 108 |
|
|
0 commit comments