|
@@ -11,8 +11,6 @@ import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
|
-import java.io.IOException;
|
|
|
-
|
|
|
/**
|
|
|
* @author: yaoll
|
|
|
* @date: 2020-10-22
|
|
@@ -21,84 +19,98 @@ import java.io.IOException;
|
|
|
@Slf4j
|
|
|
@Configuration
|
|
|
public class JmsConfig {
|
|
|
-
|
|
|
-
|
|
|
- @Value("${persagy.dmp.exchange}")
|
|
|
- private String exchange;
|
|
|
-
|
|
|
- @Value("${persagy.dmp.alarm.routingKey}")
|
|
|
- private String dmpAlarmRoutingKey;
|
|
|
-
|
|
|
- @Value("${alarm.obj.routingKey}")
|
|
|
- private String alarmObjRoutingKey;
|
|
|
-
|
|
|
- @Value("${persagy.dmp.rwd.routingKey}")
|
|
|
- private String rwdRoutingKey;
|
|
|
-
|
|
|
- @Value("${persagy.dmp.alarm.queue}")
|
|
|
- private String alarmQueue;
|
|
|
-
|
|
|
- /**
|
|
|
- * 报警对象队列
|
|
|
- */
|
|
|
- private final String alarmObjQueue = "alarm-obj-queue";
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private MessageProcesser messageProcesser;
|
|
|
-
|
|
|
- @Bean
|
|
|
- public TopicExchange exchange() {
|
|
|
- return new TopicExchange(exchange);
|
|
|
- }
|
|
|
-
|
|
|
- @Bean
|
|
|
- public Queue alarmQueue() {
|
|
|
- return new Queue(alarmQueue, true);
|
|
|
- }
|
|
|
-
|
|
|
- @Bean
|
|
|
- public Binding alarmBinding() {
|
|
|
- return BindingBuilder.bind(alarmQueue()).to(exchange()).with(dmpAlarmRoutingKey);
|
|
|
- }
|
|
|
-
|
|
|
- @Bean
|
|
|
- public Binding rwdBinding() {
|
|
|
- return BindingBuilder.bind(alarmQueue()).to(exchange()).with(rwdRoutingKey);
|
|
|
- }
|
|
|
-
|
|
|
- @Bean
|
|
|
- public Queue alarmObjQueue() {
|
|
|
- return new Queue(alarmObjQueue, true);
|
|
|
- }
|
|
|
-
|
|
|
- @Bean
|
|
|
- public Binding alarmObjBinding() {
|
|
|
- return BindingBuilder.bind(alarmObjQueue()).to(exchange()).with(alarmObjRoutingKey);
|
|
|
- }
|
|
|
-
|
|
|
- @RabbitListener(queues = {alarmObjQueue}) //监听器监听指定的Queue
|
|
|
- public void process(String message, Channel channel, Message msg) {
|
|
|
- log.info("============================== Receive:" + message);
|
|
|
- DmpMessage dmpMessage = JacksonMapper.toObject(message, DmpMessage.class);
|
|
|
- messageProcesser.listen(dmpMessage);
|
|
|
- // 手动确认消息已消费
|
|
|
- try {
|
|
|
- channel.basicAck(msg.getMessageProperties().getDeliveryTag(),false);
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("消息消费反馈失败", e);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @RabbitListener(queues = "${persagy.dmp.alarm.queue}") //监听器监听指定的Queue
|
|
|
- public void process(DmpMessage message, Channel channel, Message msg) {
|
|
|
- log.info("============================== Receive:" + JacksonMapper.toSimpleJson(message));
|
|
|
- messageProcesser.listen(message);
|
|
|
- // 手动确认消息已消费
|
|
|
- try {
|
|
|
- channel.basicAck(msg.getMessageProperties().getDeliveryTag(),false);
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("消息消费反馈失败", e);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
+ @Autowired
|
|
|
+ ConsumeMessageThreadPool consumeMessageThreadPool;
|
|
|
+
|
|
|
+ @Value("${persagy.dmp.exchange}")
|
|
|
+ private String exchange;
|
|
|
+
|
|
|
+ @Value("${persagy.dmp.alarm.routingKey}")
|
|
|
+ private String dmpAlarmRoutingKey;
|
|
|
+
|
|
|
+ @Value("${alarm.obj.routingKey}")
|
|
|
+ private String alarmObjRoutingKey;
|
|
|
+
|
|
|
+ @Value("${persagy.dmp.rwd.routingKey}")
|
|
|
+ private String rwdRoutingKey;
|
|
|
+
|
|
|
+ @Value("${persagy.dmp.alarm.queue}")
|
|
|
+ private String alarmQueue;
|
|
|
+
|
|
|
+ @Value("${persagy.dmp.alarm.rwd.queue}")
|
|
|
+ private String alarmRwdQueue;
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public Queue alarmRwdQueue() {
|
|
|
+ return new Queue(alarmRwdQueue, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public Binding alarmRwdBinding() {
|
|
|
+ return BindingBuilder.bind(alarmRwdQueue()).to(exchange()).with(rwdRoutingKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 报警对象队列
|
|
|
+ */
|
|
|
+ private final String alarmObjQueue = "alarm-obj-queue";
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MessageProcesser messageProcesser;
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public TopicExchange exchange() {
|
|
|
+ return new TopicExchange(exchange);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public Queue alarmQueue() {
|
|
|
+ return new Queue(alarmQueue, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public Binding alarmBinding() {
|
|
|
+ return BindingBuilder.bind(alarmQueue()).to(exchange()).with(dmpAlarmRoutingKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public Queue alarmObjQueue() {
|
|
|
+ return new Queue(alarmObjQueue, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public Binding alarmObjBinding() {
|
|
|
+ return BindingBuilder.bind(alarmObjQueue()).to(exchange()).with(alarmObjRoutingKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ @RabbitListener(queues = {alarmObjQueue}) //监听器监听指定的Queue
|
|
|
+ public void process(String message, Channel channel, Message msg) throws Exception{
|
|
|
+ // 手动确认消息已消费
|
|
|
+ try {
|
|
|
+ log.info("============================== Receive:" + message);
|
|
|
+ DmpMessage dmpMessage = JacksonMapper.toObject(message, DmpMessage.class);
|
|
|
+ if (dmpMessage != null) {
|
|
|
+ messageProcesser.listen(dmpMessage);
|
|
|
+ }
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @RabbitListener(queues = {"${persagy.dmp.alarm.queue}", "${persagy.dmp.alarm.rwd.queue}"}) //监听器监听指定的Queue
|
|
|
+ public void process(DmpMessage message, Channel channel, Message msg) {
|
|
|
+ consumeMessageThreadPool.consumeMessage(message, channel, msg);
|
|
|
+ }
|
|
|
}
|