Commit 4d907fdc LN

麦康尼料仓离线时不发任务。备份数据库增加判断条件。

1 个父辈 e6019de3
...@@ -7,7 +7,9 @@ import com.mongodb.client.MongoClients; ...@@ -7,7 +7,9 @@ import com.mongodb.client.MongoClients;
import com.mongodb.connection.ClusterConnectionMode; import com.mongodb.connection.ClusterConnectionMode;
import com.mongodb.connection.ClusterType; import com.mongodb.connection.ClusterType;
import com.neotel.smfcore.common.config.mongodb.bean.CustomMongoProperties; import com.neotel.smfcore.common.config.mongodb.bean.CustomMongoProperties;
import com.neotel.smfcore.common.utils.NetwrokUtils;
import com.neotel.smfcore.common.utils.StringUtils; import com.neotel.smfcore.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
...@@ -22,6 +24,7 @@ import java.util.ArrayList; ...@@ -22,6 +24,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
@Configuration @Configuration
@Slf4j
public class MongoDbConfig { public class MongoDbConfig {
private static final String NO_HOST = "none"; private static final String NO_HOST = "none";
private static final Integer NO_PORT = 0; private static final Integer NO_PORT = 0;
...@@ -48,11 +51,31 @@ public class MongoDbConfig { ...@@ -48,11 +51,31 @@ public class MongoDbConfig {
@Bean(name = "backUpMongoTemplate") @Bean(name = "backUpMongoTemplate")
public MongoTemplate backUpMongoTemplate(@Qualifier("backUpMongoProperties") CustomMongoProperties mongoProperties) { public MongoTemplate backUpMongoTemplate(@Qualifier("backUpMongoProperties") CustomMongoProperties mongoProperties) {
//如果配置的备份为本机,不需要备份
List<String> ipList = NetwrokUtils.getIpList();
String baseDatabase = masterMongoProperties().getDatabase();
//如果没有配置的话,则模板返回为空 //如果没有配置的话,则模板返回为空
if (StringUtils.isBlank(mongoProperties.getHost())) { if (StringUtils.isBlank(mongoProperties.getHost())) {
mongoProperties.setHost(NO_HOST); mongoProperties.setHost(NO_HOST);
mongoProperties.setPort(NO_PORT); mongoProperties.setPort(NO_PORT);
mongoProperties.setDatabase(NO_DATABASE); mongoProperties.setDatabase(NO_DATABASE);
log.warn("backUpMongoTemplate no Config");
} else if (ipList.contains(mongoProperties.getHost()) && mongoProperties.getDatabase().equals(baseDatabase)) {
log.warn("backUpMongoTemplate 不可用,配置的是本机: ip[" + mongoProperties.getHost() + "] db [" + mongoProperties.getDatabase() + "] baseDb[" + baseDatabase + "] ");
mongoProperties.setHost(NO_HOST);
mongoProperties.setPort(NO_PORT);
mongoProperties.setDatabase(NO_DATABASE);
}
//如果配置的IP 不通,也不需要备份
else if (!NetwrokUtils.pingIPAddress(mongoProperties.getHost())) {
log.warn("backUpMongoTemplate 不可用,地址ping不通 ip[" + mongoProperties.getHost() + "] ");
mongoProperties.setHost(NO_HOST);
mongoProperties.setPort(NO_PORT);
mongoProperties.setDatabase(NO_DATABASE);
} else {
log.info("backUpMongoTemplate 可用:ip[" + mongoProperties.getHost() + "] db [" + mongoProperties.getDatabase() + "] ");
} }
return new MongoTemplate(mongoDbFactory(mongoProperties)); return new MongoTemplate(mongoDbFactory(mongoProperties));
} }
......
package com.neotel.smfcore.common.utils;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
@Slf4j
public class NetwrokUtils {
public static List<String> getIpList() {
List<String> ipList = new ArrayList<>();
try {
Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
continue;
} else {
Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
ip = addresses.nextElement();
if (ip != null && ip.getHostAddress().indexOf(":") == -1) {
log.info("获取到本机IP地址:" + ip.getHostAddress());
ipList.add(ip.getHostAddress());
}
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return ipList;
}
public static boolean pingIPAddress(String ipAddress) {
try {
InetAddress inet = InetAddress.getByName(ipAddress);
boolean status = inet.isReachable(2000); // 判断是否能够ping通,超时时间为5秒
if (status) {
log.info(ipAddress + " is reachable.");
return true;
} else {
log.info(ipAddress + " is not reachable.");
}
} catch (IOException e) {
log.info("pingIPAddress " + ipAddress + " error :" + e.toString());
}
return false;
}
}
...@@ -421,8 +421,8 @@ public class StatusBean { ...@@ -421,8 +421,8 @@ public class StatusBean {
// } // }
public boolean isAvailable(){ public boolean isAvailable(){
if(!timeOut()){ if(!timeOut()) {
return status != BOX_STATUS.EMERGENCY && status != BOX_STATUS.PROBLEM && status != BOX_STATUS.DEBUG; return status != BOX_STATUS.OFFLINE && status != BOX_STATUS.EMERGENCY && status != BOX_STATUS.PROBLEM && status != BOX_STATUS.DEBUG;
} }
return false; return false;
} }
......
...@@ -4,6 +4,7 @@ package com.neotel.smfcore.core.device.enums; ...@@ -4,6 +4,7 @@ package com.neotel.smfcore.core.device.enums;
* Created by sunke on 2021/7/13. * Created by sunke on 2021/7/13.
*/ */
public class BOX_STATUS { public class BOX_STATUS {
public final static int OFFLINE=0;
/** /**
* 1=准备就绪 * 1=准备就绪
*/ */
......
...@@ -4,6 +4,7 @@ import cn.hutool.core.util.ObjectUtil; ...@@ -4,6 +4,7 @@ import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.neotel.smfcore.common.utils.SecurityUtils; import com.neotel.smfcore.common.utils.SecurityUtils;
import com.neotel.smfcore.core.device.bean.StatusBean; import com.neotel.smfcore.core.device.bean.StatusBean;
import com.neotel.smfcore.core.device.enums.BOX_STATUS;
import com.neotel.smfcore.core.message.enums.MessageType; import com.neotel.smfcore.core.message.enums.MessageType;
import com.neotel.smfcore.core.message.util.DeviceMessageUtil; import com.neotel.smfcore.core.message.util.DeviceMessageUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -211,7 +212,7 @@ public class DevicesStatusUtil { ...@@ -211,7 +212,7 @@ public class DevicesStatusUtil {
cids) { cids) {
StatusBean bean = getStatusBean(cid); StatusBean bean = getStatusBean(cid);
if (bean != null) { if (bean != null) {
if (bean.getOfflineTime() == -1 && bean.timeOut()) { if (bean.getOfflineTime() == -1 && (bean.timeOut()||bean.getStatus()== BOX_STATUS.OFFLINE)) {
bean.setOfflineTime(System.currentTimeMillis()); bean.setOfflineTime(System.currentTimeMillis());
DeviceMessageUtil.addOfflineMessage(bean.getCid(), ""); DeviceMessageUtil.addOfflineMessage(bean.getCid(), "");
} }
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!