|
@@ -0,0 +1,88 @@
|
|
|
+package com.persagy.dmp.alarm.jms;
|
|
|
+
|
|
|
+import com.persagy.dmp.rwd.model.DmpMessage;
|
|
|
+import com.rabbitmq.client.Channel;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.amqp.core.*;
|
|
|
+import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+
|
|
|
+/**
|
|
|
+ * rabbitmq配置
|
|
|
+ *
|
|
|
+ * @author lixing
|
|
|
+ * @version V1.0 2021/10/11 7:32 下午
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Configuration
|
|
|
+public class RabbitmqConfig {
|
|
|
+ @Autowired
|
|
|
+ ConsumeMessageThreadPool consumeMessageThreadPool;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 报警交换机
|
|
|
+ */
|
|
|
+ private final String exchange = "exchange-dmp";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 报警定义路由键
|
|
|
+ */
|
|
|
+ private final String dmpAlarmConfigRoutingKey = "alarm-config-routing-key";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 报警对象队列
|
|
|
+ */
|
|
|
+ private final String alarmConfigQueue = "alarm-config-queue";
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public TopicExchange exchange() {
|
|
|
+ return new TopicExchange(exchange);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public Queue alarmConfigQueue() {
|
|
|
+ return new Queue(alarmConfigQueue, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public Binding alarmObjBinding() {
|
|
|
+ return BindingBuilder.bind(alarmConfigQueue()).to(exchange()).with(dmpAlarmConfigRoutingKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 监听消息
|
|
|
+ *
|
|
|
+ * @param message DmpMessage(中台报警消息对象)
|
|
|
+ * @param channel channel对象,用于反馈消息消费结果
|
|
|
+ * @param msg rabbitmq消息对象
|
|
|
+ * @author lixing
|
|
|
+ * @version V1.0 2021/10/11 7:40 下午
|
|
|
+ */
|
|
|
+ @RabbitListener(queues = {alarmConfigQueue})
|
|
|
+ public void process(DmpMessage message, Channel channel, Message msg) throws Exception{
|
|
|
+ // 手动确认消息已消费
|
|
|
+ try {
|
|
|
+ log.info("============================== Receive:" + message);
|
|
|
+ if (message != null) {
|
|
|
+ consumeMessageThreadPool.consumeMessage(message);
|
|
|
+ }
|
|
|
+
|
|
|
+ channel.basicAck(msg.getMessageProperties().getDeliveryTag(), false);
|
|
|
+ } catch (Exception e) {
|
|
|
+ Boolean isRedeliveredFail = msg.getMessageProperties().getRedelivered();
|
|
|
+ // true表示消息已经重复处理失败
|
|
|
+ if (isRedeliveredFail) {
|
|
|
+ // 拒绝消息,requeue=false 表示不再重新入队,如果配置了死信队列则进入死信队列
|
|
|
+ log.error("重复消费消息失败,message: {}", message);
|
|
|
+ channel.basicReject(msg.getMessageProperties().getDeliveryTag(), false);
|
|
|
+ } else {
|
|
|
+ // 如果是第一次失败则再次放入队列
|
|
|
+ // requeue为是否重新回到队列,true重新入队
|
|
|
+ channel.basicNack(msg.getMessageProperties().getDeliveryTag(), false, true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|