shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.walker.push.rocketmq;
 
import com.walker.infrastructure.utils.StringUtils;
import org.apache.rocketmq.spring.support.DefaultRocketMQListenerContainer;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
 
/**
 * 消息队列,环境隔离增强Bean配置,如果设置环境参数,则会自动在主题后面追加参数。
 * <pre>
 *     如:chat_topic_dev, chat_topic_prod
 * </pre>
 * @author 时克英
 * @date 2023-09-18
 */
@Deprecated
public class EnvironmentIsolationPostProcessor implements BeanPostProcessor {
 
    /**
     * 在装载Bean之前实现参数修改
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if(bean instanceof DefaultRocketMQListenerContainer){
            DefaultRocketMQListenerContainer container = (DefaultRocketMQListenerContainer) bean;
            //拼接Topic
            if(enabledIsolation && StringUtils.hasText(environmentName)){
                container.setTopic(String.join(StringUtils.STRING_UNDERLINE, container.getTopic(), environmentName));
            }
            return container;
        }
        return bean;
    }
 
    public void setEnabledIsolation(boolean enabledIsolation) {
        this.enabledIsolation = enabledIsolation;
    }
 
    public void setEnvironmentName(String environmentName) {
        this.environmentName = environmentName;
    }
 
    //    @Value("${rocketmq.enhance.enabledIsolation:true}")
    private boolean enabledIsolation = true;
//    @Value("${rocketmq.enhance.environment:''}")
    private String environmentName = StringUtils.EMPTY_STRING;
}