From 76b629ac5f8320c0b1501b53db358575b32cdf39 Mon Sep 17 00:00:00 2001 From: "raoq@vip.qq.com" Date: Mon, 11 Jun 2018 11:17:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3xxl-job.xml=E7=AB=AF=E5=8F=A3?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E8=8E=B7=E5=8F=96=E7=9A=84=E6=8F=8F=E8=BF=B0?= =?UTF-8?q?=E5=92=8C=E6=89=A7=E8=A1=8C=E5=99=A8=E5=9C=A8=E6=8C=87=E5=AE=9A?= =?UTF-8?q?ip=E8=87=AA=E5=8A=A8=E8=8E=B7=E5=8F=96=E7=AB=AF=E5=8F=A3?= =?UTF-8?q?=E6=97=B6=E7=9A=84=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.修正xml配置文件描述信息 int类型不允许xml为空的配置会报NPE 2.修复指定执行器ip时自动获取端口号的问题,指定ip与InetAddress.anyLocalAddress()结果不一致会导致验证端口是否使用错误。 --- .../xxl/job/core/executor/XxlJobExecutor.java | 2 +- .../java/com/xxl/job/core/util/NetUtil.java | 35 ++++++++++++++----- .../resources/applicationcontext-xxl-job.xml | 2 +- 3 files changed, 29 insertions(+), 10 deletions(-) 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 ac2324f4..e19e162d 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); + port = port>0?port: NetUtil.findAvailablePort(9999,ip); // 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 a10b17be..4642e887 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,8 +2,10 @@ 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; /** @@ -15,23 +17,24 @@ public class NetUtil { private static Logger logger = LoggerFactory.getLogger(NetUtil.class); /** - * find avaliable port + * find avaliable port by ip * * @param defaultPort + * @param ip * @return */ - public static int findAvailablePort(int defaultPort) { + public static int findAvailablePort(int defaultPort,String ip) { int portTmp = defaultPort; while (portTmp < 65535) { - if (!isPortUsed(portTmp)) { + if (!isPortUsed(portTmp,ip)) { return portTmp; } else { portTmp++; } } - portTmp = defaultPort--; + portTmp = --defaultPort; while (portTmp > 0) { - if (!isPortUsed(portTmp)) { + if (!isPortUsed(portTmp,ip)) { return portTmp; } else { portTmp--; @@ -40,18 +43,34 @@ 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) { + public static boolean isPortUsed(int port,String ip) { boolean used = false; ServerSocket serverSocket = null; try { - serverSocket = new ServerSocket(port); - used = false; + if(StringUtils.isEmpty(ip)){ + serverSocket = new ServerSocket(port); + }else { + serverSocket = new ServerSocket(); + serverSocket.bind(new InetSocketAddress(ip,port)); + } } 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 afa930db..ba5543a6 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 @@ - +