|
@@ -2,10 +2,8 @@ package com.xxl.job.core.util;
|
2
|
2
|
|
3
|
3
|
import org.slf4j.Logger;
|
4
|
4
|
import org.slf4j.LoggerFactory;
|
5
|
|
-import org.springframework.util.StringUtils;
|
6
|
5
|
|
7
|
6
|
import java.io.IOException;
|
8
|
|
-import java.net.InetSocketAddress;
|
9
|
7
|
import java.net.ServerSocket;
|
10
|
8
|
|
11
|
9
|
/**
|
|
@@ -17,24 +15,23 @@ public class NetUtil {
|
17
|
15
|
private static Logger logger = LoggerFactory.getLogger(NetUtil.class);
|
18
|
16
|
|
19
|
17
|
/**
|
20
|
|
- * find avaliable port by ip
|
|
18
|
+ * find avaliable port
|
21
|
19
|
*
|
22
|
20
|
* @param defaultPort
|
23
|
|
- * @param ip
|
24
|
21
|
* @return
|
25
|
22
|
*/
|
26
|
|
- public static int findAvailablePort(int defaultPort,String ip) {
|
|
23
|
+ public static int findAvailablePort(int defaultPort) {
|
27
|
24
|
int portTmp = defaultPort;
|
28
|
25
|
while (portTmp < 65535) {
|
29
|
|
- if (!isPortUsed(portTmp,ip)) {
|
|
26
|
+ if (!isPortUsed(portTmp)) {
|
30
|
27
|
return portTmp;
|
31
|
28
|
} else {
|
32
|
29
|
portTmp++;
|
33
|
30
|
}
|
34
|
31
|
}
|
35
|
|
- portTmp = --defaultPort;
|
|
32
|
+ portTmp = defaultPort--;
|
36
|
33
|
while (portTmp > 0) {
|
37
|
|
- if (!isPortUsed(portTmp,ip)) {
|
|
34
|
+ if (!isPortUsed(portTmp)) {
|
38
|
35
|
return portTmp;
|
39
|
36
|
} else {
|
40
|
37
|
portTmp--;
|
|
@@ -44,33 +41,17 @@ public class NetUtil {
|
44
|
41
|
}
|
45
|
42
|
|
46
|
43
|
/**
|
47
|
|
- * find avaliable port
|
48
|
|
- *
|
49
|
|
- * @param defaultPort
|
50
|
|
- * @return
|
51
|
|
- */
|
52
|
|
- public static int findAvailablePort(int defaultPort) {
|
53
|
|
- return findAvailablePort(defaultPort,null);
|
54
|
|
- }
|
55
|
|
-
|
56
|
|
-
|
57
|
|
- /**
|
58
|
44
|
* check port used
|
59
|
45
|
*
|
60
|
46
|
* @param port
|
61
|
|
- * @param ip 为空则为 InetAddress.anyLocalAddress()
|
62
|
47
|
* @return
|
63
|
48
|
*/
|
64
|
|
- public static boolean isPortUsed(int port,String ip) {
|
|
49
|
+ public static boolean isPortUsed(int port) {
|
65
|
50
|
boolean used = false;
|
66
|
51
|
ServerSocket serverSocket = null;
|
67
|
52
|
try {
|
68
|
|
- if(StringUtils.isEmpty(ip)){
|
69
|
|
- serverSocket = new ServerSocket(port);
|
70
|
|
- }else {
|
71
|
|
- serverSocket = new ServerSocket();
|
72
|
|
- serverSocket.bind(new InetSocketAddress(ip,port));
|
73
|
|
- }
|
|
53
|
+ serverSocket = new ServerSocket(port);
|
|
54
|
+ used = false;
|
74
|
55
|
} catch (IOException e) {
|
75
|
56
|
logger.debug(">>>>>>>>>>> xxl-job, port[{}] is in use.", port);
|
76
|
57
|
used = true;
|