|
@@ -1,14 +1,17 @@
|
|
|
package com.persagy.apm.energyalarmstarter.alarmengine.jms;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.persagy.apm.energyalarmstarter.alarmengine.jms.model.DmpMessage;
|
|
|
import com.rabbitmq.client.Channel;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
import org.springframework.amqp.core.*;
|
|
|
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
|
|
|
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
|
|
/**
|
|
|
* @description:报警定义消息通知
|
|
@@ -27,6 +30,9 @@ public class JmsConfig {
|
|
|
@Autowired
|
|
|
private AlarmEngineMsgHandler msgHandler;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private RedisTemplate redisTemplate;
|
|
|
+
|
|
|
/**
|
|
|
* 交换机
|
|
|
*/
|
|
@@ -43,6 +49,11 @@ public class JmsConfig {
|
|
|
private final String objConditionRelRoutingKey = "obj-condition-rel-routing-key";
|
|
|
|
|
|
/**
|
|
|
+ * 报警记录 路由键
|
|
|
+ */
|
|
|
+ private final String alarmRecordRoutingKey = "alarm-msg-routing-key";
|
|
|
+
|
|
|
+ /**
|
|
|
* 报警条件队列
|
|
|
*/
|
|
|
private final String conditionQueue = "condition-queue";
|
|
@@ -51,6 +62,10 @@ public class JmsConfig {
|
|
|
* 设备与报警条件关联关系队列
|
|
|
*/
|
|
|
private final String objConditionRelQueue = "obj-condition-rel-queue";
|
|
|
+ /**
|
|
|
+ * 报警记录队列
|
|
|
+ */
|
|
|
+ private final String alarmRecordQueue = "alarm-msg-queue";
|
|
|
|
|
|
@Bean
|
|
|
public Queue conditionQueue() {
|
|
@@ -63,6 +78,11 @@ public class JmsConfig {
|
|
|
}
|
|
|
|
|
|
@Bean
|
|
|
+ public Queue alarmRecordQueue() {
|
|
|
+ return new Queue(alarmRecordQueue, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
public TopicExchange alarmServiceExchange() {
|
|
|
return new TopicExchange(exchange);
|
|
|
}
|
|
@@ -77,6 +97,11 @@ public class JmsConfig {
|
|
|
return BindingBuilder.bind(objConditionRelQueue()).to(alarmServiceExchange()).with(objConditionRelRoutingKey);
|
|
|
}
|
|
|
|
|
|
+ @Bean
|
|
|
+ public Binding alarmRecordBinding() {
|
|
|
+ return BindingBuilder.bind(alarmRecordQueue()).to(alarmServiceExchange()).with(alarmRecordRoutingKey);
|
|
|
+ }
|
|
|
+
|
|
|
@RabbitHandler
|
|
|
@RabbitListener(queues = {conditionQueue, objConditionRelQueue})
|
|
|
public void dealMsg(DmpMessage msg, Channel channel, Message message) throws Exception {
|
|
@@ -98,8 +123,43 @@ public class JmsConfig {
|
|
|
msgHandler.syncDeletedObjConditionRelList(msg);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
- log.error("消息消费失败,{}", e.getMessage());
|
|
|
+ log.error("报警条件消息消费失败,{}", e.getMessage());
|
|
|
}
|
|
|
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
|
|
|
}
|
|
|
+
|
|
|
+ @RabbitHandler
|
|
|
+ @RabbitListener(queues = {alarmRecordQueue})
|
|
|
+ public void setAlarmRecordMsg(DmpMessage msg, Channel channel, Message message) throws Exception {
|
|
|
+ try {
|
|
|
+ log.debug("============================== Receive:" + msg);
|
|
|
+ if (JmsAlarmRecordEnum.NEW_ALARM_RECORD.getType().equals(msg.getType())) {
|
|
|
+ msgHandler.createAlarmRecordAndSendRecordId(msg);
|
|
|
+ }
|
|
|
+ if (JmsAlarmRecordEnum.UPDATE_ALARM_RECORD.getType().equals(msg.getType())) {
|
|
|
+ msgHandler.updateAlarmRecord(msg);
|
|
|
+ }
|
|
|
+ if (JmsAlarmRecordEnum.CONDITION_ALARM.getType().equals(msg.getType())) {
|
|
|
+ msgHandler.alarmContinue(msg);
|
|
|
+ }
|
|
|
+ //解锁
|
|
|
+ JSONObject data = msg.getExts();
|
|
|
+ unlock(data.getString("id"));
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("报警记录消息消费失败,{}", e.getMessage());
|
|
|
+ }
|
|
|
+ channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void unlock(String key) {
|
|
|
+ try {
|
|
|
+ String currentValue = String.valueOf(redisTemplate.opsForValue().get(key));
|
|
|
+ if (!StringUtils.isEmpty(currentValue)) {
|
|
|
+ redisTemplate.opsForValue().getOperations().delete(key);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|