自动启动项目
This commit is contained in:
@@ -18,11 +18,7 @@ import org.springframework.stereotype.Component;
|
||||
import java.net.Inet4Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.util.Date;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
|
||||
@@ -120,20 +116,29 @@ public class SchedulerConf {
|
||||
// log.info("Local HostAddress: "+addr.getHostAddress());
|
||||
// String hostname = addr.getHostName();
|
||||
// log.info("Local host name: "+hostname);
|
||||
String addr = getIpAddress();
|
||||
log.info("本机ip地址:" + addr);
|
||||
List<String> addr = getIpAddress();
|
||||
//查询
|
||||
List<TbTask> tbTaskList = tbTaskDao.selectList(null);
|
||||
for (TbTask tbTask : tbTaskList) {
|
||||
if (addr.equals(tbTask.getIp())) {
|
||||
if (addr == null){
|
||||
log.error("本机地址未获取到!!");
|
||||
break;
|
||||
}
|
||||
String ips = tbTask.getIp();
|
||||
String[] ipArray = ips.split(",");
|
||||
for (String realIp : ipArray) {
|
||||
if (addr.contains(realIp)) {
|
||||
log.info("本机ip地址:" + addr);
|
||||
this.start(tbTask.getId());
|
||||
log.info("执行了:" + tbTask.getTaskDesc());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String getIpAddress(){
|
||||
private static List<String> getIpAddress() {
|
||||
try {
|
||||
List<String> ips = new ArrayList<>();
|
||||
Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
|
||||
while (allNetInterfaces.hasMoreElements()) {
|
||||
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
|
||||
@@ -144,56 +149,17 @@ public class SchedulerConf {
|
||||
&& ip instanceof Inet4Address
|
||||
&& !ip.isLoopbackAddress() //loopback地址即本机地址,IPv4的loopback范围是127.0.0.0 ~ 127.255.255.255
|
||||
&& ip.getHostAddress().indexOf(":") == -1) {
|
||||
System.out.println("本机的IP = " + ip.getHostAddress());
|
||||
return ip.getHostAddress();
|
||||
log.info("本机的IP = " + ip.getHostAddress());
|
||||
ips.add(ip.getHostAddress());
|
||||
}
|
||||
}
|
||||
}
|
||||
return ips;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/** 获取主机地址 */
|
||||
public static String getHostIp(){
|
||||
|
||||
String realIp = null;
|
||||
|
||||
try {
|
||||
InetAddress address = InetAddress.getLocalHost();
|
||||
|
||||
// 如果是回环网卡地址, 则获取ipv4 地址
|
||||
if (address.isLoopbackAddress()) {
|
||||
address = getInet4Address();
|
||||
}
|
||||
|
||||
realIp = address.getHostAddress();
|
||||
|
||||
log.info("获取主机ip地址成功, 主机ip地址:{}", address);
|
||||
return address.getHostAddress();
|
||||
} catch (Exception e) {
|
||||
log.error("获取主机ip地址异常", e);
|
||||
}
|
||||
|
||||
return realIp;
|
||||
}
|
||||
|
||||
/** 获取IPV4网络配置 */
|
||||
private static InetAddress getInet4Address() throws SocketException {
|
||||
// 获取所有网卡信息
|
||||
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
|
||||
while (networkInterfaces.hasMoreElements()) {
|
||||
NetworkInterface netInterface = (NetworkInterface) networkInterfaces.nextElement();
|
||||
Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
|
||||
while (addresses.hasMoreElements()) {
|
||||
InetAddress ip = (InetAddress) addresses.nextElement();
|
||||
if (ip instanceof Inet4Address) {
|
||||
return ip;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新数据库动态定时任务状态
|
||||
|
||||
Reference in New Issue
Block a user