fix:使用模板引擎消息模板

This commit is contained in:
nieziyan 2024-01-05 11:14:52 +08:00
parent 9c07c72421
commit 20163e1fba
22 changed files with 269 additions and 88 deletions

View File

@ -121,6 +121,17 @@ public class MessageDTO implements Serializable {
this.toUser = toUser;
}
public MessageDTO(String title, String template, Map<String, Object> data) {
this.title = title;
this.templateCode = template;
this.data = data;
}
public MessageDTO(String template, Map<String, Object> data) {
this.templateCode = template;
this.data = data;
}
public boolean isMarkdown() {
return this.isMarkdown;
}

View File

@ -38,4 +38,6 @@ public interface RedisConstant {
String MANAGE_TOKEN = "Token:Manage"; // 运管系统Token
String QIYE_EMAIL_TOKEN = "Token:QiyeEmail"; // 网易企业邮箱Token
String PREFIX_TEMPLATE = "Template:"; // 消息模板
}

View File

@ -0,0 +1,30 @@
package org.jeecg.common.util;
import java.util.HashMap;
import java.util.Map;
public class DataTool {
private int counter = 1;
private final Map<String, Object> data = new HashMap<>();
public DataTool put(Object value) {
data.put(String.format("${%d}", counter), value);
counter++;
return this;
}
public DataTool put(String key, Object value) {
data.put(String.format("${%s}", key), value);
return this;
}
public Map<String, Object> get(){
return data;
}
public static DataTool getInstance(){
return new DataTool();
}
}

View File

@ -0,0 +1,63 @@
package org.jeecg.common.util;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import org.jeecg.common.api.dto.message.MessageDTO;
import org.jeecg.common.constant.RedisConstant;
import org.jeecg.common.util.dynamic.db.FreemarkerParseFactory;
import org.jeecg.modules.base.entity.postgre.SysMessageTemplate;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.DefaultTemplateResolver;
import org.thymeleaf.templateresolver.StringTemplateResolver;
import java.util.Map;
/*
* 模板工具类
* */
public class TemplateUtil {
private static final RedisUtil redisUtil;
static {
redisUtil = SpringContextUtils.getBean(RedisUtil.class);
}
public static MessageDTO parse(String code, Map<String, Object> data){
String key = RedisConstant.PREFIX_TEMPLATE + code;
MessageDTO messageDTO = new MessageDTO();
if (!redisUtil.hasKey(key))
return messageDTO;
SysMessageTemplate template = (SysMessageTemplate) redisUtil.get(key);
String templateName = template.getTemplateName();
String templateContent = template.getTemplateContent();
messageDTO.setTitle(templateName);
if (MapUtil.isEmpty(data))
return messageDTO;
String content = FreemarkerParseFactory
.parseTemplateContent(templateContent, data, true);
messageDTO.setContent(content);
return messageDTO;
}
public static MessageDTO parse(String title, String code, Map<String, Object> data){
String key = RedisConstant.PREFIX_TEMPLATE + code;
MessageDTO messageDTO = new MessageDTO();
if (!redisUtil.hasKey(key))
return messageDTO;
SysMessageTemplate template = (SysMessageTemplate) redisUtil.get(key);
String templateName = template.getTemplateName();
String templateContent = template.getTemplateContent();
messageDTO.setTitle(StrUtil.isBlank(title) ? templateName : title);
if (MapUtil.isEmpty(data))
return messageDTO;
String content = FreemarkerParseFactory
.parseTemplateContent(templateContent, data, true);
messageDTO.setContent(content);
return messageDTO;
}
}

View File

@ -1,5 +1,6 @@
package org.jeecg.modules.base.entity;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
@ -15,4 +16,12 @@ public class Rule implements Serializable {
private Double threshold; // 阈值
private String units; // 单位
public String joint(){
StringBuilder sb = new StringBuilder();
units = StrUtil.isBlank(units) ? "" : units;
sb.append(name).append(StrUtil.SPACE).append(operator)
.append(StrUtil.SPACE).append(threshold).append(units);
return sb.toString();
}
}

View File

@ -1,4 +1,4 @@
package org.jeecg.modules.message.entity;
package org.jeecg.modules.base.entity.postgre;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

View File

@ -3,11 +3,15 @@ package org.jeecg.modules.base.enums;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
/*
* 模板枚举
* */
@Getter
@AllArgsConstructor
public enum Template {
IDC_DATASOURCE_STATUS("IDC_Datasource_Status");
IDC_DATASOURCE_STATUS("IDC_Datasource_Status"),
MONITOR_SERVER("Monitor_Server"), MONITOR_EMAIL("Monitor_Email"),
MONITOR_DATABASE("Monitor_Database"), ANALYSIS_NUCLIDE("Analysis_Nuclide");
private final String code;
}

View File

@ -20,8 +20,7 @@ public interface SystemClient {
Map<String, SysUser> findUserMap();
@GetMapping("/sys/sendMessage/send")
void sendMessage(@RequestParam String title,
@RequestParam String message,
void sendMessage(@RequestParam MessageDTO messageDTO,
@RequestParam String groupId,
@RequestParam String notific);

View File

@ -4,6 +4,7 @@ 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.DataTool;
import org.jeecg.common.util.JDBCUtil;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.modules.base.dto.ConnR;
@ -12,15 +13,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.Schedules;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import static org.jeecg.modules.base.enums.Template.IDC_DATASOURCE_STATUS;
@ -77,15 +74,15 @@ public class TemplateManager {
template = JDBCUtil.template(urlM, usernameM, passwordM);
this.setTemplate(template);
} else {
String info = "发送%s数据源异常信息失败: {}";
// 为当前线程设置临时Token 避免OpenFeign调用其它服务接口时401
UserTokenContext.setToken(TokenUtils.getTempToken());
// 给admin发送数据源异常消息
Map<String, Object> data = new HashMap<>();
data.put("param1", "inland");
data.put("param2", connR.getInfo());
Map<String, Object> data = DataTool.getInstance()
.put("inland").put(connR.getInfo()).get();
messageDTO.setData(data);
try { systemClient.sendTo(messageDTO); }
catch(Exception e) {log.error("发送inland数据源异常信息失败: {}" , e.getMessage());}
catch(Exception e) {log.error(String.format(info, "inland") , e.getMessage());}
connR = JDBCUtil.isConnection(urlS, usernameS, passwordS);
if (connR.isConn()){
template = JDBCUtil.template(urlS, usernameS, passwordS);
@ -93,11 +90,10 @@ public class TemplateManager {
return;
}
// 给admin发送数据源异常消息
data.put("param1", "oversea");
data.put("param2", connR.getInfo());
data = DataTool.getInstance().put("oversea").put(connR.getInfo()).get();
messageDTO.setData(data);
try { systemClient.sendTo(messageDTO); }
catch(Exception e) {log.error("发送oversea数据源异常信息失败: {}" , e.getMessage());}
catch(Exception e) {log.error(String.format(info, "oversea") , e.getMessage());}
}
}catch (Exception e){
log.error("定时检查inland/oversea数据源状态异常: {}", e.getMessage());

View File

@ -11,6 +11,7 @@ import cn.hutool.core.util.StrUtil;
import lombok.Data;
import lombok.NoArgsConstructor;
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.CommonConstant;
import org.jeecg.common.constant.SymbolConstant;
@ -199,8 +200,7 @@ public class AnalysisConsumer implements StreamListener<String, ObjectRecord<Str
// 发送报警信息
String groupId = info.getGroupId();
if (StrUtil.isNotBlank(groupId))
systemClient.sendMessage("Nuclide Analysis Warn Message",
alarmInfo.toString(), groupId, ALL.getValue());
systemClient.sendMessage(new MessageDTO(), groupId, ALL.getValue());
}
}

View File

@ -58,6 +58,8 @@ public interface ISysBaseAPI extends CommonAPI {
*/
void sendTemplateMessage(MessageDTO message);
void sendMessage(MessageDTO message);
/**
* 根据模板编码获取模板内容支持自定义推送类型
* @param templateCode

View File

@ -49,10 +49,7 @@ public class SendMessage {
* 根据联系人组id向用户推送消息
*
*/
public void send(String title, String message, String groupId, String notific){
// 封装MessageDTO消息体
MessageDTO messageDTO = new MessageDTO(title, message);
public void send(MessageDTO messageDTO, String groupId, String notific){
Map<String, String> contact = getContact(groupId);
if (StrUtil.isBlank(notific))return;
List<String> ways = ListUtil.toList(notific.split(comma));
@ -65,7 +62,7 @@ public class SendMessage {
if (StrUtil.isNotBlank(toSys)){
messageDTO.setToUser(toSys);
messageDTO.setType(XT.getType());
sysBaseAPI.sendTemplateMessage(messageDTO);
sysBaseAPI.sendMessage(messageDTO);
}
} else if (way.equals(YJ.getValue())) {// 2.推送邮箱
String toEmail = contact.get(Email);
@ -75,14 +72,14 @@ public class SendMessage {
Map<String, String> userEmail = new HashMap<>(contact);
MapUtil.removeAny(userEmail, Sms, Email, System);
messageDTO.setUserEmail(userEmail);
sysBaseAPI.sendTemplateMessage(messageDTO);
sysBaseAPI.sendMessage(messageDTO);
}
} else if (way.equals(SMS.getValue())) {// 3.推送短信
String toSms = contact.get(Sms);
if (StrUtil.isNotBlank(toSms)){
messageDTO.setToUser(toSms);
messageDTO.setType(SMS.getType());
sysBaseAPI.sendTemplateMessage(messageDTO);
sysBaseAPI.sendMessage(messageDTO);
}
}
}

View File

@ -6,6 +6,8 @@ import org.jeecg.modules.message.SendMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestController
@RequestMapping("sys/sendMessage")
public class SendMessageController {
@ -17,11 +19,10 @@ public class SendMessageController {
private ISysBaseAPI sysBaseAPI;
@GetMapping("send")
public void sendMessage(@RequestParam String title,
@RequestParam String message,
public void sendMessage(@RequestParam MessageDTO messageDTO,
@RequestParam String groupId,
@RequestParam String notific){
sendMessage.send(title, message, groupId, notific);
sendMessage.send(messageDTO, groupId, notific);
}
@PostMapping("sendTo")

View File

@ -1,5 +1,8 @@
package org.jeecg.modules.message.controller;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
@ -12,7 +15,7 @@ import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.message.entity.MsgParams;
import org.jeecg.modules.message.entity.SysMessageTemplate;
import org.jeecg.modules.base.entity.postgre.SysMessageTemplate;
import org.jeecg.modules.message.service.ISysMessageTemplateService;
import org.jeecg.modules.message.util.PushMsgUtil;
import org.springframework.beans.factory.annotation.Autowired;
@ -34,10 +37,6 @@ import java.util.Map;
@RestController
@RequestMapping("/sys/message/sysMessageTemplate")
public class SysMessageTemplateController extends JeecgController<SysMessageTemplate, ISysMessageTemplateService> {
@Autowired
private ISysMessageTemplateService sysMessageTemplateService;
@Autowired
private PushMsgUtil pushMsgUtil;
@Autowired
private ISysBaseAPI sysBaseApi;
@ -56,7 +55,7 @@ public class SysMessageTemplateController extends JeecgController<SysMessageTemp
@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
QueryWrapper<SysMessageTemplate> queryWrapper = QueryGenerator.initQueryWrapper(sysMessageTemplate, req.getParameterMap());
Page<SysMessageTemplate> page = new Page<SysMessageTemplate>(pageNo, pageSize);
IPage<SysMessageTemplate> pageList = sysMessageTemplateService.page(page, queryWrapper);
IPage<SysMessageTemplate> pageList = service.page(page, queryWrapper);
return Result.ok(pageList);
}
@ -68,8 +67,7 @@ public class SysMessageTemplateController extends JeecgController<SysMessageTemp
*/
@PostMapping(value = "/add")
public Result<?> add(@RequestBody SysMessageTemplate sysMessageTemplate) {
sysMessageTemplateService.save(sysMessageTemplate);
return Result.ok("添加成功!");
return service.addOrUpdate(sysMessageTemplate);
}
/**
@ -80,8 +78,7 @@ public class SysMessageTemplateController extends JeecgController<SysMessageTemp
*/
@PutMapping(value = "/edit")
public Result<?> edit(@RequestBody SysMessageTemplate sysMessageTemplate) {
sysMessageTemplateService.updateById(sysMessageTemplate);
return Result.ok("更新成功!");
return service.addOrUpdate(sysMessageTemplate);
}
/**
@ -91,9 +88,8 @@ public class SysMessageTemplateController extends JeecgController<SysMessageTemp
* @return
*/
@DeleteMapping(value = "/delete")
public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
sysMessageTemplateService.removeById(id);
return Result.ok("删除成功!");
public Result<?> delete(@RequestParam String id) {
return service.delById(id);
}
/**
@ -103,9 +99,9 @@ public class SysMessageTemplateController extends JeecgController<SysMessageTemp
* @return
*/
@DeleteMapping(value = "/deleteBatch")
public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
this.sysMessageTemplateService.removeByIds(Arrays.asList(ids.split(",")));
return Result.ok("批量删除成功!");
public Result<?> deleteBatch(@RequestParam String ids) {
String[] idArr = StrUtil.split(ids, StrUtil.COMMA);
return service.delByIds(ListUtil.toList(idArr));
}
/**
@ -116,7 +112,7 @@ public class SysMessageTemplateController extends JeecgController<SysMessageTemp
*/
@GetMapping(value = "/queryById")
public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
SysMessageTemplate sysMessageTemplate = sysMessageTemplateService.getById(id);
SysMessageTemplate sysMessageTemplate = service.getById(id);
return Result.ok(sysMessageTemplate);
}

View File

@ -2,7 +2,7 @@ package org.jeecg.modules.message.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;
import org.jeecg.modules.message.entity.SysMessageTemplate;
import org.jeecg.modules.base.entity.postgre.SysMessageTemplate;
import java.util.List;

View File

@ -1,7 +1,8 @@
package org.jeecg.modules.message.service;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.service.JeecgService;
import org.jeecg.modules.message.entity.SysMessageTemplate;
import org.jeecg.modules.base.entity.postgre.SysMessageTemplate;
import java.util.List;
@ -19,4 +20,10 @@ public interface ISysMessageTemplateService extends JeecgService<SysMessageTempl
* @return
*/
List<SysMessageTemplate> selectByCode(String code);
Result<?> addOrUpdate(SysMessageTemplate template);
Result<?> delById(String id);
Result<?> delByIds(List<String> ids);
}

View File

@ -1,13 +1,20 @@
package org.jeecg.modules.message.service.impl;
import cn.hutool.core.util.ArrayUtil;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.Prompt;
import org.jeecg.common.constant.RedisConstant;
import org.jeecg.common.system.base.service.impl.JeecgServiceImpl;
import org.jeecg.modules.message.entity.SysMessageTemplate;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.base.entity.postgre.SysMessageTemplate;
import org.jeecg.modules.message.mapper.SysMessageTemplateMapper;
import org.jeecg.modules.message.service.ISysMessageTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.stream.Collectors;
/**
* @Description: 消息模板
@ -19,11 +26,50 @@ import java.util.List;
public class SysMessageTemplateServiceImpl extends JeecgServiceImpl<SysMessageTemplateMapper, SysMessageTemplate> implements ISysMessageTemplateService {
@Autowired
private SysMessageTemplateMapper sysMessageTemplateMapper;
private RedisUtil redisUtil;
private final String PREFIX = RedisConstant.PREFIX_TEMPLATE;
@Override
public List<SysMessageTemplate> selectByCode(String code) {
return sysMessageTemplateMapper.selectByCode(code);
return baseMapper.selectByCode(code);
}
@Override
@Transactional
public Result<?> addOrUpdate(SysMessageTemplate template) {
boolean success = this.saveOrUpdate(template);
if (success){
String code = template.getTemplateCode();
redisUtil.set(PREFIX + code, template);
return Result.OK(Prompt.UPDATE_SUCC);
}
return Result.error(Prompt.UPDATE_ERR);
}
@Override
public Result<?> delById(String id) {
SysMessageTemplate template = this.getById(id);
String key = PREFIX + template.getTemplateCode();
boolean success = this.removeById(id);
if (success){
redisUtil.del(key);
return Result.OK(Prompt.DELETE_SUCC);
}
return Result.OK(Prompt.DELETE_ERR);
}
@Override
@Transactional
public Result<?> delByIds(List<String> ids) {
List<SysMessageTemplate> templates = this.listByIds(ids);
List<String> keys = templates.stream().map(template -> PREFIX + template.getTemplateCode())
.collect(Collectors.toList());
boolean success = this.removeByIds(ids);
if (success){
redisUtil.del(ArrayUtil.toArray(keys, String.class));
return Result.OK(Prompt.DELETE_SUCC);
}
return Result.OK(Prompt.DELETE_ERR);
}
}

View File

@ -5,7 +5,7 @@ import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.jeecg.modules.message.entity.SysMessage;
import org.jeecg.modules.message.entity.SysMessageTemplate;
import org.jeecg.modules.base.entity.postgre.SysMessageTemplate;
import org.jeecg.modules.message.handle.enums.SendMsgStatusEnum;
import org.jeecg.modules.message.service.ISysMessageService;
import org.jeecg.modules.message.service.ISysMessageTemplateService;

View File

@ -8,14 +8,12 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.dto.message.MessageDTO;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.config.mqtoken.UserTokenContext;
import org.jeecg.common.constant.DateConstant;
import org.jeecg.common.constant.RedisConstant;
import org.jeecg.common.util.JDBCUtil;
import org.jeecg.common.util.NumUtil;
import org.jeecg.common.util.RedisStreamUtil;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.common.util.*;
import org.jeecg.modules.base.dto.NameValue;
import org.jeecg.modules.base.entity.Rule;
import org.jeecg.modules.base.entity.monitor.ItemHistory;
@ -31,11 +29,14 @@ import org.quartz.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.Set;
import static org.jeecg.common.util.TokenUtils.getTempToken;
import static org.jeecg.modules.base.enums.Op.*;
import static org.jeecg.modules.base.enums.SourceType.DATABASE;
import static org.jeecg.modules.base.enums.Template.MONITOR_DATABASE;
import static org.jeecg.modules.base.enums.Template.MONITOR_SERVER;
@Data
@Slf4j
@ -98,14 +99,14 @@ public class DatabaseJob extends Monitor implements Job{
alarmLog.setRuleId(ruleId);
alarmLog.setOperator(operator);
alarmLog.setAlarmValue(StrUtil.toString(current));
String ruleName = alarmRule.getName();
StringBuilder message = new StringBuilder();
message.append(String.format("您的数据源: [%s]", databaseName));
message.append(String.format(",设定的预警规则: %s", ruleName));
message.append(String.format(",预警信息为: %s,当前值为: %s", operator, current));
alarmLog.setAlarmInfo(message.toString());
getAlarmClient().create(alarmLog);
String ruleName = alarmRule.getName();
Map<String, Object> data = DataTool.getInstance().
put(databaseName).put(ruleName).put(rule.joint()).put(current).get();
MessageDTO messageDTO = TemplateUtil.parse(MONITOR_DATABASE.getCode(), data);
alarmLog.setAlarmInfo(messageDTO.getContent());
getAlarmClient().create(alarmLog);
// 规则触发报警后,设置该规则的沉默周期(如果有)
// 沉默周期失效之前,该规则不会再次被触发
Long silenceCycle = alarmRule.getSilenceCycle();
@ -114,7 +115,7 @@ public class DatabaseJob extends Monitor implements Job{
// 发送报警信息
String groupId = alarmRule.getContactId();
String notific = alarmRule.getNotification();
getSendMessage().send("Database Monitor Warn Message", message.toString(), groupId, notific);
getSendMessage().send(messageDTO, groupId, notific);
}
} catch (JsonProcessingException e) {
log.error("Database预警规则: {}解析失败,失败原因: {}", operator, e.getMessage());

View File

@ -7,8 +7,11 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.dto.message.MessageDTO;
import org.jeecg.common.constant.RedisConstant;
import org.jeecg.common.util.DataTool;
import org.jeecg.common.util.NumUtil;
import org.jeecg.common.util.TemplateUtil;
import org.jeecg.modules.base.dto.NameValue;
import org.jeecg.modules.base.entity.Rule;
import org.jeecg.modules.base.entity.postgre.AlarmLog;
@ -18,10 +21,13 @@ import org.jeecg.modules.base.enums.Item;
import org.jeecg.modules.quartz.entity.Monitor;
import org.quartz.*;
import java.util.Map;
import java.util.Set;
import static org.jeecg.modules.base.enums.SourceType.DATABASE;
import static org.jeecg.modules.base.enums.SourceType.EMAIL;
import static org.jeecg.modules.base.enums.Template.MONITOR_DATABASE;
import static org.jeecg.modules.base.enums.Template.MONITOR_EMAIL;
@Data
@Slf4j
@ -84,14 +90,14 @@ public class EmailJob extends Monitor implements Job{
alarmLog.setRuleId(ruleId);
alarmLog.setOperator(operator);
alarmLog.setAlarmValue(StrUtil.toString(current));
String ruleName = alarmRule.getName();
StringBuilder message = new StringBuilder();
message.append(String.format("您的邮箱: [%s]", emailName));
message.append(String.format(",设定的预警规则: %s", ruleName));
message.append(String.format(",预警信息为: %s,当前值为: %s", operator, current));
alarmLog.setAlarmInfo(message.toString());
getAlarmClient().create(alarmLog);
String ruleName = alarmRule.getName();
Map<String, Object> data = DataTool.getInstance().
put(emailName).put(ruleName).put(rule.joint()).put(current).get();
MessageDTO messageDTO = TemplateUtil.parse(MONITOR_EMAIL.getCode(), data);
alarmLog.setAlarmInfo(messageDTO.getContent());
getAlarmClient().create(alarmLog);
// 规则触发报警后,设置该规则的沉默周期(如果有)
// 沉默周期失效之前,该规则不会再次被触发
Long silenceCycle = alarmRule.getSilenceCycle();
@ -100,7 +106,7 @@ public class EmailJob extends Monitor implements Job{
// 发送报警信息
String groupId = alarmRule.getContactId();
String notific = alarmRule.getNotification();
getSendMessage().send("Email Monitor Warn Message", message.toString(), groupId, notific);
getSendMessage().send(messageDTO, groupId, notific);
}
} catch (JsonProcessingException e) {
log.error("Email预警规则: {}解析失败,失败原因: {}", operator, e.getMessage());

View File

@ -8,30 +8,28 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import feign.FeignException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.dto.message.MessageDTO;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.config.mqtoken.UserTokenContext;
import org.jeecg.common.constant.DateConstant;
import org.jeecg.common.constant.RedisConstant;
import org.jeecg.common.util.DataTool;
import org.jeecg.common.util.NumUtil;
import org.jeecg.common.util.RedisStreamUtil;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.common.util.TemplateUtil;
import org.jeecg.modules.base.entity.Rule;
import org.jeecg.modules.base.entity.monitor.ItemHistory;
import org.jeecg.modules.base.entity.postgre.AlarmLog;
import org.jeecg.modules.base.entity.postgre.AlarmRule;
import org.jeecg.modules.feignclient.AbnormalAlarmClient;
import org.jeecg.modules.feignclient.ManageUtil;
import org.jeecg.modules.feignclient.MonitorSystem;
import org.jeecg.modules.message.SendMessage;
import org.jeecg.modules.quartz.entity.Monitor;
import org.quartz.*;
import static org.jeecg.common.util.TokenUtils.getTempToken;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.Set;
import static org.jeecg.modules.base.enums.Op.*;
import static org.jeecg.modules.base.enums.SourceType.SERVER;
import static org.jeecg.modules.base.enums.Template.MONITOR_SERVER;
@Data
@Slf4j
@ -107,14 +105,14 @@ public class ServerJob extends Monitor implements Job {
alarmLog.setRuleId(ruleId);
alarmLog.setOperator(operator);
alarmLog.setAlarmValue(StrUtil.toString(current));
String ruleName = alarmRule.getName();
StringBuilder message = new StringBuilder();
message.append(String.format("您的服务器: [%s]", serverName));
message.append(String.format(",设定的预警规则: %s", ruleName));
message.append(String.format(",预警信息为: %s,当前值为: %s", operator, current));
alarmLog.setAlarmInfo(message.toString());
getAlarmClient().create(alarmLog);
String ruleName = alarmRule.getName();
Map<String, Object> data = DataTool.getInstance().
put(serverName).put(ruleName).put(rule.joint()).put(current).get();
MessageDTO messageDTO = TemplateUtil.parse(MONITOR_SERVER.getCode(), data);
alarmLog.setAlarmInfo(messageDTO.getContent());
getAlarmClient().create(alarmLog);
// 规则触发报警后,设置该规则的沉默周期(如果有)
// 沉默周期失效之前,该规则不会再次被触发
Long silenceCycle = alarmRule.getSilenceCycle();
@ -123,7 +121,7 @@ public class ServerJob extends Monitor implements Job {
// 发送报警信息
String groupId = alarmRule.getContactId();
String notific = alarmRule.getNotification();
getSendMessage().send("Server Monitor Warn Message", message.toString(), groupId, notific);
getSendMessage().send(messageDTO, groupId, notific);
}
}catch (FeignException.Unauthorized e){
ManageUtil.refreshToken();

View File

@ -30,7 +30,7 @@ import org.jeecg.common.util.YouBianCodeUtil;
import org.jeecg.common.util.dynamic.db.FreemarkerParseFactory;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.base.entity.postgre.*;
import org.jeecg.modules.message.entity.SysMessageTemplate;
import org.jeecg.modules.base.entity.postgre.SysMessageTemplate;
import org.jeecg.modules.message.handle.impl.EmailPushMsgHandle;
import org.jeecg.modules.message.handle.impl.EmailSendMsgHandle;
import org.jeecg.modules.message.handle.impl.SmsSendMsgHandle;
@ -1261,6 +1261,19 @@ public class SysBaseApiImpl implements ISysBaseAPI {
}
}
@Override
public void sendMessage(MessageDTO message) {
String type = message.getType();
if(XT.getType().equals(type)){ // 站内消息
systemSendMsgHandle.sendMessage(message);
}else if(YJ.getType().equals(type)){ // 邮箱消息
emailPushMsgHandle.sendMessage(message);
} else if (SMS.getType().equals(type)) { // 手机短信
smsSendMsgHandle.sendMessage(message);
}
}
@Override
public String getTemplateContent(String code) {
List<SysMessageTemplate> list = sysMessageTemplateService.selectByCode(code);