All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.eworkcloud.rocket.RocketProcessor Maven / Gradle / Ivy

There is a newer version: 2.6.0
Show newest version
package com.eworkcloud.rocket;

import com.aliyun.mq.http.MQClient;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.StringUtils;

public class RocketProcessor implements BeanPostProcessor {

    private final RocketProperties properties;
    private final MQClient client;

    public RocketProcessor(RocketProperties properties, MQClient client) {
        this.properties = properties;
        this.client = client;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        Class clazz = AopUtils.getTargetClass(bean);

        if (bean instanceof ConsumeListener) {
            ConsumeListener listener = (ConsumeListener) bean;
            // 获取注解对象
            RocketListener annotation = AnnotationUtils.getAnnotation(clazz, RocketListener.class);

            // 获取主题配置
            String topic = properties.getTopic();
            if (null != annotation) {
                if (StringUtils.hasText(annotation.topic())) {
                    topic = annotation.topic();
                }
            }
            if (!StringUtils.hasText(topic)) {
                throw new IllegalStateException("Topic is null or empty.");
            }

            // 获取标签配置
            String subExpression = "";
            if (null != annotation) {
                subExpression = StringUtils.arrayToDelimitedString(annotation.tag(), "||");
            }
            if (!StringUtils.hasText(subExpression)) {
                subExpression = "*";
            }

            // 检查消费线程数
            if (null == properties.getThreads() || properties.getThreads() < 1) {
                properties.setThreads(Runtime.getRuntime().availableProcessors());
            }

            listener.startup(client.getConsumer(topic, properties.getGroupId(), subExpression), properties.getThreads());
        }
        return bean;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy