更新定时任务
This commit is contained in:
@@ -38,7 +38,7 @@ public class erpController {
|
||||
/**
|
||||
* 流水交易记账
|
||||
*/
|
||||
@GetMapping("getErpTranFlow")
|
||||
@GetMapping("getTranFlow")
|
||||
public ApiResult<?> getErpTranFlow(String flowId) {
|
||||
UpdateWrapper<ErpTransactionFlow> updateWrapper = new UpdateWrapper<>();
|
||||
updateWrapper.eq("flow_id", flowId);
|
||||
|
||||
@@ -32,7 +32,7 @@ public class jobController {
|
||||
/**
|
||||
* 主机在线状态检测
|
||||
*/
|
||||
@GetMapping("getJobMonitHostStatus")
|
||||
@GetMapping("getMonitHostStatus")
|
||||
public ApiResult<?> getJobMonitHostStatus() {
|
||||
List<BizMonitorHost> monitorHosts = bizMonitorHostService.list();
|
||||
List<CompletableFuture<Void>> futures = new ArrayList<>(monitorHosts.size());
|
||||
|
||||
@@ -1,125 +1,58 @@
|
||||
package com.mini.capi.utils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.regex.Pattern;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
|
||||
/**
|
||||
* 网络连通性检测工具类(支持IPv4和域名)
|
||||
*/
|
||||
public class NetworkUtils {
|
||||
|
||||
// IPv4地址校验正则(支持带前后空白)
|
||||
private static final Pattern IPV4_PATTERN = Pattern.compile(
|
||||
"^\\s*((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\s*$"
|
||||
);
|
||||
|
||||
// Ping命令配置常量
|
||||
private static final int PING_COUNT = 2;
|
||||
private static final int WINDOWS_TIMEOUT_MS = 3000;
|
||||
private static final int LINUX_TIMEOUT_SEC = 3;
|
||||
private static final int PROCESS_WAIT_TIMEOUT_SEC = 6;
|
||||
// 默认检查端口(保持80作为默认,兼容通用场景)
|
||||
private static final int DEFAULT_PORT = 80;
|
||||
// 默认超时时间(3秒,可根据需求调整)
|
||||
private static final int DEFAULT_TIMEOUT_MS = 3000;
|
||||
|
||||
// 私有构造器:禁止实例化
|
||||
private NetworkUtils() {
|
||||
throw new AssertionError("工具类不允许实例化");
|
||||
throw new UnsupportedOperationException("工具类不允许实例化");
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测本地网络到目标IP或域名的连通性
|
||||
* 检查目标主机(IP/域名)的连通性(使用默认端口80)
|
||||
*
|
||||
* @param target 目标IP或域名(不可为null/空)
|
||||
* @return true:可达;false:不可达(含参数无效/检查失败)
|
||||
*/
|
||||
public static boolean isNetworkReachable(String target) {
|
||||
if (target == null || target.trim().isEmpty()) {
|
||||
// 调用重载方法,传入默认端口
|
||||
return isNetworkReachable(target, DEFAULT_PORT);
|
||||
}
|
||||
/**
|
||||
* 检查目标主机(IP/域名)的连通性(自定义端口)
|
||||
*/
|
||||
public static boolean isNetworkReachable(String target, int port) {
|
||||
// 参数校验:目标为空或端口无效,直接返回不可达
|
||||
if (target == null || target.trim().isEmpty() || port < 1 || port > 65535) {
|
||||
return false;
|
||||
}
|
||||
target = target.trim();
|
||||
|
||||
List<String> ipList = new ArrayList<>();
|
||||
// 区分IP和域名,解析目标为IP列表
|
||||
if (isValidIpv4(target)) {
|
||||
ipList.add(target); // 是IP,直接加入
|
||||
} else {
|
||||
// 是域名,尝试解析为IP
|
||||
try {
|
||||
InetAddress[] addresses = InetAddress.getAllByName(target);
|
||||
for (InetAddress addr : addresses) {
|
||||
ipList.add(addr.getHostAddress());
|
||||
}
|
||||
if (ipList.isEmpty()) {
|
||||
return false; // 域名解析无结果
|
||||
}
|
||||
} catch (UnknownHostException e) {
|
||||
return false; // 域名无法解析(如不存在、网络故障)
|
||||
}
|
||||
}
|
||||
|
||||
// 对每个IP执行ping检测,有一个成功则返回true
|
||||
for (String ip : ipList) {
|
||||
if (executePingCommand(buildPingCommand(ip))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验是否为合法的IPv4地址(支持带前后空白)
|
||||
*/
|
||||
private static boolean isValidIpv4(String ip) {
|
||||
return IPV4_PATTERN.matcher(ip).matches();
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建跨系统的ping命令
|
||||
*/
|
||||
private static List<String> buildPingCommand(String ip) {
|
||||
List<String> command = new ArrayList<>(5);
|
||||
command.add("ping");
|
||||
|
||||
String osName = System.getProperty("os.name", "").toLowerCase();
|
||||
if (osName.contains("windows")) {
|
||||
command.add("-n");
|
||||
command.add(String.valueOf(PING_COUNT));
|
||||
command.add("-w");
|
||||
command.add(String.valueOf(WINDOWS_TIMEOUT_MS));
|
||||
} else {
|
||||
command.add("-c");
|
||||
command.add(String.valueOf(PING_COUNT));
|
||||
command.add("-W");
|
||||
command.add(String.valueOf(LINUX_TIMEOUT_SEC));
|
||||
}
|
||||
|
||||
command.add(ip);
|
||||
return command;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行ping命令并返回执行结果
|
||||
*/
|
||||
private static boolean executePingCommand(List<String> command) {
|
||||
Process process = null;
|
||||
String normalizedTarget = target.trim();
|
||||
try {
|
||||
ProcessBuilder processBuilder = new ProcessBuilder(command);
|
||||
processBuilder.redirectErrorStream(true);
|
||||
process = processBuilder.start();
|
||||
|
||||
boolean isFinished = process.waitFor(PROCESS_WAIT_TIMEOUT_SEC, TimeUnit.SECONDS);
|
||||
if (!isFinished) {
|
||||
process.destroyForcibly();
|
||||
return false;
|
||||
// 先解析目标地址(只解析一次,避免重复操作)
|
||||
InetAddress address = InetAddress.getByName(normalizedTarget);
|
||||
// 优先尝试 Socket 连接指定端口
|
||||
try (Socket socket = new Socket()) {
|
||||
socket.connect(new InetSocketAddress(address, port), DEFAULT_TIMEOUT_MS);
|
||||
return true; // 端口连接成功,返回可达
|
||||
} catch (Exception e) {
|
||||
// Socket 连接失败,不中断流程,继续执行主机可达性检查
|
||||
}
|
||||
return process.exitValue() == 0;
|
||||
|
||||
} catch (IOException | InterruptedException e) {
|
||||
// Socket 失败后,降级检查主机基础可达性(ICMP)
|
||||
return address.isReachable(DEFAULT_TIMEOUT_MS);
|
||||
} catch (Exception e) {
|
||||
// 任何异常(地址解析失败、ICMP 检查失败等)均返回不可达
|
||||
return false;
|
||||
} finally {
|
||||
if (process != null) {
|
||||
process.destroyForcibly();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user