feat:IDC Data拾取

This commit is contained in:
nieziyan 2023-12-20 11:07:13 +08:00
parent 06def98569
commit 977447bd07
4 changed files with 41 additions and 29 deletions

View File

@ -46,7 +46,7 @@ public class JDBCUtil {
try (Connection connection = dataSource.getConnection()) {
return true;
} catch (SQLException e) {
log.error("JDBCUtil.isConnection():数据源["+ url +"]连接失败: {}", e.getMessage());
log.error("数据源["+ url +"]连接失败: {}", e.getMessage());
return false;
}
}
@ -60,8 +60,7 @@ public class JDBCUtil {
try (Connection connection = dataSource.getConnection()) {
return new ConnR().setConn(true);
} catch (SQLException e) {
String message = String.format("IDC数据源["+ url +"]连接失败: %s", e.getMessage());
log.error(message);
String message = String.format("[xxx数据源]连接失败: %s", e.getMessage());
return new ConnR().setInfo(message);
}
}
@ -73,7 +72,7 @@ public class JDBCUtil {
} catch (SQLException e) {
String url = "--";
if (ObjectUtil.isNotNull(dataSource)) url = dataSource.getUrl();
log.error("JDBCUtil.isConnection():数据源["+ url +"]连接失败: {}", e.getMessage());
log.error("数据源["+ url +"]连接失败: {}", e.getMessage());
return false;
}
}

View File

@ -6,10 +6,7 @@ import org.jeecg.common.system.vo.DictModel;
import org.jeecg.modules.base.entity.postgre.SysUser;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.*;
import java.util.Collection;
import java.util.List;
@ -28,7 +25,7 @@ public interface SystemClient {
@RequestParam String groupId,
@RequestParam String notific);
@GetMapping("/sys/sendMessage/sendTo")
@PostMapping("/sys/sendMessage/sendTo")
void sendTo(@RequestBody MessageDTO messageDTO);
/* SysDictController下相关接口 */

View File

@ -1,8 +1,10 @@
package org.jeecg.modules.idc;
import cn.hutool.core.util.StrUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.dto.message.MessageDTO;
import org.jeecg.common.config.mqtoken.UserTokenContext;
import org.jeecg.common.constant.enums.MessageTypeEnum;
import org.jeecg.common.util.JDBCUtil;
import org.jeecg.modules.base.dto.ConnR;
@ -17,26 +19,28 @@ import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.TimeUnit;
import static org.jeecg.common.util.TokenUtils.getTempToken;
@Component
@Slf4j
public class IDCDataFetch {
@Value("inland.url")
@Value("${inland.url}")
private String urlM; // 本地数据源url即为主数据源
@Value("inland.username")
@Value("${inland.username}")
private String usernameM;
@Value("inland.password")
@Value("${inland.password}")
private String passwordM;
@Value("oversea.url")
@Value("${oversea.url}")
private String urlS; // 国外数据源url即为从数据源
@Value("oversea.username")
@Value("${oversea.username}")
private String usernameS;
@Value("oversea.password")
@Value("${oversea.password}")
private String passwordS;
@Autowired
@ -44,39 +48,51 @@ public class IDCDataFetch {
// 定时拾取IDC数据
@Scheduled(fixedDelayString = "${request-interval}", timeUnit = TimeUnit.SECONDS)
private void fetch() {
ConnR connR;
public void fetch() {
JdbcTemplate template;
MessageDTO messageDTO = new MessageDTO("IDC数据源异常", null, "admin");
messageDTO.setType(MessageTypeEnum.XT.getType());
connR = JDBCUtil.isConnection(urlM, usernameM, passwordM);
ConnR connR = JDBCUtil.isConnection(urlM, usernameM, passwordM);
if (connR.isConn()) {
try {
template = JDBCUtil.template(urlM, usernameM, passwordM);
// 查询IDC Data
System.out.println("[inland数据源]IDC数据查询");
} catch (Exception e) {
log.error("[inland数据源]IDC数据查询异常: {}", e.getMessage());
log.error("[inland数据源]IDC数据处理异常: {}", e.getMessage());
}
return;
}
// 给管理员发送预警信息
messageDTO.setContent(connR.getInfo());
systemClient.sendTo(messageDTO);
UserTokenContext.setToken(getTempToken());
// 对发送警告消息时可能出现的异常进行捕获(503) 防止影响后续代码执行
try {
// 给管理员发送预警信息
String message = StrUtil.replace(connR.getInfo(), "xxx", "inland");
messageDTO.setContent(message);
systemClient.sendTo(messageDTO);
}catch (Exception e){
log.error("发送inland数据源异常信息失败: {}", e.getMessage());
}
// 使用备用数据源
connR = JDBCUtil.isConnection(urlS, usernameS, passwordS);
if (connR.isConn()) {
try {
template = JDBCUtil.template(urlS, usernameS, passwordS);
// 查询IDC Data
System.out.println("[oversea数据源]IDC数据查询");
} catch (Exception e) {
log.error("[oversea数据源]IDC数据查询异常: {}", e.getMessage());
log.error("[oversea数据源]IDC数据处理异常: {}", e.getMessage());
}
return;
}
// 给管理员发送预警信息
messageDTO.setContent(connR.getInfo());
systemClient.sendTo(messageDTO);
try {
// 给管理员发送预警信息
String message = StrUtil.replace(connR.getInfo(), "xxx", "oversea");
messageDTO.setContent(message);
systemClient.sendTo(messageDTO);
}catch (Exception e){
log.error("发送oversea数据源异常信息失败: {}", e.getMessage());
}
UserTokenContext.remove();
}
}

View File

@ -24,7 +24,7 @@ public class SendMessageController {
sendMessage.send(title, message, groupId, notific);
}
@GetMapping("sendTo")
@PostMapping("sendTo")
public void sendTo(@RequestBody MessageDTO messageDTO){
sysBaseAPI.sendTemplateMessage(messageDTO);
}