From 3957ef2ff70a31e692ffe568b36b311ea5ff48fa Mon Sep 17 00:00:00 2001 From: xuxueli <931591021@qq.com> Date: Thu, 16 Aug 2018 22:06:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0RM?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/XXL-JOB官方文档.md | 2 +- .../xxl/job/core/executor/XxlJobExecutor.java | 2 +- .../java/com/xxl/job/core/util/NetUtil.java | 35 +++++-------------- .../resources/applicationcontext-xxl-job.xml | 2 +- 4 files changed, 11 insertions(+), 30 deletions(-) diff --git a/doc/XXL-JOB官方文档.md b/doc/XXL-JOB官方文档.md index 24abe7c4..885cb812 100644 --- a/doc/XXL-JOB官方文档.md +++ b/doc/XXL-JOB官方文档.md @@ -347,7 +347,7 @@ XXL-JOB是一个轻量级分布式任务调度平台,其核心设计目标是 - + diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/executor/XxlJobExecutor.java b/xxl-job-core/src/main/java/com/xxl/job/core/executor/XxlJobExecutor.java index 0a95822d..f25502b3 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/executor/XxlJobExecutor.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/executor/XxlJobExecutor.java @@ -129,7 +129,7 @@ public class XxlJobExecutor implements ApplicationContextAware { private NetComServerFactory serverFactory = new NetComServerFactory(); private void initExecutorServer(int port, String ip, String appName, String accessToken) throws Exception { // valid param - port = port>0?port: NetUtil.findAvailablePort(9999,ip); + port = port>0?port: NetUtil.findAvailablePort(9999); // start server NetComServerFactory.putService(ExecutorBiz.class, new ExecutorBizImpl()); // rpc-service, base on jetty diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/util/NetUtil.java b/xxl-job-core/src/main/java/com/xxl/job/core/util/NetUtil.java index 4642e887..a10b17be 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/util/NetUtil.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/util/NetUtil.java @@ -2,10 +2,8 @@ package com.xxl.job.core.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.util.StringUtils; import java.io.IOException; -import java.net.InetSocketAddress; import java.net.ServerSocket; /** @@ -17,24 +15,23 @@ public class NetUtil { private static Logger logger = LoggerFactory.getLogger(NetUtil.class); /** - * find avaliable port by ip + * find avaliable port * * @param defaultPort - * @param ip * @return */ - public static int findAvailablePort(int defaultPort,String ip) { + public static int findAvailablePort(int defaultPort) { int portTmp = defaultPort; while (portTmp < 65535) { - if (!isPortUsed(portTmp,ip)) { + if (!isPortUsed(portTmp)) { return portTmp; } else { portTmp++; } } - portTmp = --defaultPort; + portTmp = defaultPort--; while (portTmp > 0) { - if (!isPortUsed(portTmp,ip)) { + if (!isPortUsed(portTmp)) { return portTmp; } else { portTmp--; @@ -43,34 +40,18 @@ public class NetUtil { throw new IllegalStateException("no available port."); } - /** - * find avaliable port - * - * @param defaultPort - * @return - */ - public static int findAvailablePort(int defaultPort) { - return findAvailablePort(defaultPort,null); - } - - /** * check port used * * @param port - * @param ip 为空则为 InetAddress.anyLocalAddress() * @return */ - public static boolean isPortUsed(int port,String ip) { + public static boolean isPortUsed(int port) { boolean used = false; ServerSocket serverSocket = null; try { - if(StringUtils.isEmpty(ip)){ - serverSocket = new ServerSocket(port); - }else { - serverSocket = new ServerSocket(); - serverSocket.bind(new InetSocketAddress(ip,port)); - } + serverSocket = new ServerSocket(port); + used = false; } catch (IOException e) { logger.debug(">>>>>>>>>>> xxl-job, port[{}] is in use.", port); used = true; diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-spring/src/main/resources/applicationcontext-xxl-job.xml b/xxl-job-executor-samples/xxl-job-executor-sample-spring/src/main/resources/applicationcontext-xxl-job.xml index ba5543a6..942fb2f0 100644 --- a/xxl-job-executor-samples/xxl-job-executor-sample-spring/src/main/resources/applicationcontext-xxl-job.xml +++ b/xxl-job-executor-samples/xxl-job-executor-sample-spring/src/main/resources/applicationcontext-xxl-job.xml @@ -29,7 +29,7 @@ - +