执行器端口支持随机生成(小于等于0时),避免端口定义冲突

master
xuxueli 7 years ago
parent 52ba5a88fd
commit 41328f4795
  1. 5
      doc/XXL-JOB官方文档.md
  2. 7
      xxl-job-core/src/main/java/com/xxl/job/core/executor/XxlJobExecutor.java
  3. 70
      xxl-job-core/src/main/java/com/xxl/job/core/util/NetUtil.java

@ -1053,6 +1053,7 @@ Tips: 历史版本(V1.3.x)目前已经Release至稳定版本, 进入维护阶段
- 7、底层系统日志级别规范调整,清理遗留代码;
- 8、修改JobThread捕获Error错误不更新JobLog的问题;
- 9、任务注解调整为 “@JobHandler”,与任务注解统一;
- 10、执行器端口支持随机生成(小于等于0时),避免端口定义冲突;
### TODO LIST
- 1、任务权限管理:执行器为粒度分配权限,核心操作校验权限;
@ -1075,8 +1076,8 @@ Tips: 历史版本(V1.3.x)目前已经Release至稳定版本, 进入维护阶段
- 18、JobHandler提供 init/destroy 方法,支持自定义任务线程销毁逻辑;
- 19、cron表达式的最大长度调整,兼容复杂类型cron;
- 20、执行器回调地址/日志地址格式兼容,是否已"/"结尾均支持;
- 21、执行器端口支持随机生成,避免端口冲突
- 22、任务单机多线程:提升任务单机并行处理能力;
- 21、任务单机多线程:提升任务单机并行处理能力
## 七、其他

@ -9,6 +9,7 @@ import com.xxl.job.core.log.XxlJobFileAppender;
import com.xxl.job.core.rpc.netcom.NetComClientProxy;
import com.xxl.job.core.rpc.netcom.NetComServerFactory;
import com.xxl.job.core.thread.JobThread;
import com.xxl.job.core.util.NetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
@ -28,7 +29,7 @@ public class XxlJobExecutor implements ApplicationContextAware {
// ---------------------- param ----------------------
private String ip;
private int port = 9999;
private int port;
private String appName;
private String adminAddresses;
private String accessToken;
@ -121,6 +122,10 @@ public class XxlJobExecutor implements ApplicationContextAware {
// ---------------------- executor-server(jetty) ----------------------
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);
// start server
NetComServerFactory.putService(ExecutorBiz.class, new ExecutorBizImpl()); // rpc-service, base on jetty
NetComServerFactory.setAccessToken(accessToken);
serverFactory.start(port, ip, appName); // jetty + registry

@ -0,0 +1,70 @@
package com.xxl.job.core.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.ServerSocket;
/**
* net util
*
* @author xuxueli 2017-11-29 17:00:25
*/
public class NetUtil {
private static Logger logger = LoggerFactory.getLogger(NetUtil.class);
/**
* find avaliable port
*
* @param defaultPort
* @return
*/
public static int findAvailablePort(int defaultPort) {
int portTmp = defaultPort;
while (portTmp < 65535) {
if (!isPortUsed(portTmp)) {
return portTmp;
} else {
portTmp++;
}
}
portTmp = defaultPort--;
while (portTmp > 0) {
if (!isPortUsed(portTmp)) {
return portTmp;
} else {
portTmp--;
}
}
throw new IllegalStateException("no available port.");
}
/**
* check port used
*
* @param port
* @return
*/
public static boolean isPortUsed(int port) {
boolean used = false;
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(port);
used = false;
} catch (IOException e) {
logger.info(">>>>>>>>>>> xxl-job, port[{}] is in use.", port);
used = true;
} finally {
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
logger.info("");
}
}
}
return used;
}
}
Loading…
Cancel
Save