Files
senmesh-zc/src/main/java/com/newyetai/mq/consumer/InitHandler.java
ran.shang 7e38b8d971 init
2025-06-11 09:42:13 +08:00

57 lines
1.9 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.newyetai.mq.consumer;
import com.google.common.collect.Lists;
import com.newyetai.common.constant.CommonConstants;
import com.newyetai.common.util.EnvironmentUtils;
import com.newyetai.mq.config.ExchangeManager;
import com.newyetai.mq.config.QueueManager;
import com.newyetai.mq.config.RabbitCustomProperties;
import com.newyetai.mq.constant.MqConstant;
import com.newyetai.mq.consumer.handler.IMqHandler;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
/**
* 初始化消息消费处理类,初始queue/binding
*
* @author sr on 2020-07-24
*/
public class InitHandler implements InitializingBean {
@Autowired(required = false)
private List<IMqHandler<?>> handlers = Lists.newArrayList();
@Autowired
private QueueManager queueManager;
@Autowired
private ExchangeManager exchangeManager;
@Autowired
private RabbitCustomProperties rabbitCustomProperties;
@Override
public void afterPropertiesSet() throws Exception {
String applicationName = EnvironmentUtils.getApplicationName();
handlers.forEach(handler -> {
String routingKey = handler.getRoutingKey();
String queueName = handler.getQueueName();
// 初始化binding
if (queueName == null) {
queueName = String.join(CommonConstants.UNDERLINE, applicationName,
EnvironmentUtils.getProfile(), routingKey);
if (handler.isBroadcast()) {
// queueName = String.join(CommonConstants.UNDERLINE, queueName,rabbitCustomProperties.getBroadcastId());
String nacosDiscoveryIp = EnvironmentUtils.getProperty(MqConstant.NACOS_DISCOVERY_IP);
queueName = String.join(CommonConstants.UNDERLINE, queueName, nacosDiscoveryIp);
}
}
queueManager.addQueueAndRoutingKey(
exchangeManager.getExchangeByType(handler.getExchangeType()), queueName, routingKey);
});
}
}