Search in sources :

Example 1 with SubscriptionData

use of com.alibaba.rocketmq.common.protocol.heartbeat.SubscriptionData in project LinkAgent by shulieTech.

the class ConsumerRegistry method buildMQPushConsumer.

/**
 * 构建 DefaultMQPushConsumer
 * 如果后续支持影子 server 模式,则直接修改此方法即可
 *
 * @param businessConsumer 业务消费者
 * @return 返回注册的影子消费者,如果初始化失败会返回 null
 */
private static synchronized DefaultMQPushConsumer buildMQPushConsumer(DefaultMQPushConsumer businessConsumer) {
    Map<String, SubscriptionData> topicsInWhiteList = new HashMap<String, SubscriptionData>();
    ConcurrentMap<String, SubscriptionData> map;
    try {
        map = businessConsumer.getDefaultMQPushConsumerImpl().getSubscriptionInner();
    } catch (NoSuchMethodError e) {
        try {
            map = Reflect.on(businessConsumer.getDefaultMQPushConsumerImpl()).call("getSubscriptionInner").get();
        } catch (ReflectException t) {
            logger.error("buildMQPushConsumer getSubscriptionInner error.", e);
            return null;
        }
    }
    if (map != null) {
        Long lastWhitelistWarnTime = lastWhitelistWarnTimes.get(businessConsumer);
        if (lastWhitelistWarnTime == null) {
            lastWhitelistWarnTime = 0L;
        }
        long now = System.currentTimeMillis();
        long passTime = now - lastWhitelistWarnTime;
        for (Map.Entry<String, SubscriptionData> entry : map.entrySet()) {
            String topic = entry.getKey();
            if (!isPermitInitConsumer(businessConsumer, topic)) {
                if (passTime > 5000) {
                    logger.warn("Alibaba-RocketMQ topic : {} is not in whitelist!", topic);
                    lastWhitelistWarnTimes.put(businessConsumer, now);
                }
                continue;
            }
            topicsInWhiteList.put(entry.getKey(), entry.getValue());
        }
    }
    if (topicsInWhiteList.isEmpty()) {
        return null;
    }
    DefaultMQPushConsumer defaultMQPushConsumer = new DefaultMQPushConsumer();
    defaultMQPushConsumer.setNamesrvAddr(businessConsumer.getNamesrvAddr());
    defaultMQPushConsumer.setConsumerGroup(Pradar.addClusterTestPrefix(businessConsumer.getConsumerGroup()));
    defaultMQPushConsumer.setConsumeFromWhere(businessConsumer.getConsumeFromWhere());
    defaultMQPushConsumer.setPullThresholdForQueue(businessConsumer.getPullThresholdForQueue());
    defaultMQPushConsumer.setPullBatchSize(businessConsumer.getPullBatchSize());
    defaultMQPushConsumer.setConsumeMessageBatchMaxSize(businessConsumer.getConsumeMessageBatchMaxSize());
    defaultMQPushConsumer.setConsumeThreadMax(businessConsumer.getConsumeThreadMax());
    defaultMQPushConsumer.setConsumeThreadMin(businessConsumer.getConsumeThreadMin());
    String instanceName = getInstanceName();
    if (instanceName != null && !instanceName.equals("DEFAULT")) {
        defaultMQPushConsumer.setInstanceName(Pradar.CLUSTER_TEST_PREFIX + instanceName);
    } else {
        defaultMQPushConsumer.setInstanceName(Pradar.addClusterTestPrefix(businessConsumer.getConsumerGroup() + instanceName));
    }
    try {
        defaultMQPushConsumer.setAdjustThreadPoolNumsThreshold(businessConsumer.getAdjustThreadPoolNumsThreshold());
    } catch (AbstractMethodError e) {
    } catch (NoSuchMethodError e) {
    }
    try {
        defaultMQPushConsumer.setAllocateMessageQueueStrategy(businessConsumer.getAllocateMessageQueueStrategy());
    } catch (AbstractMethodError e) {
    } catch (NoSuchMethodError e) {
    }
    defaultMQPushConsumer.setConsumeConcurrentlyMaxSpan(businessConsumer.getConsumeConcurrentlyMaxSpan());
    defaultMQPushConsumer.setConsumeTimestamp(businessConsumer.getConsumeTimestamp());
    defaultMQPushConsumer.setMessageModel(businessConsumer.getMessageModel());
    defaultMQPushConsumer.setMessageListener(businessConsumer.getMessageListener());
    defaultMQPushConsumer.setPostSubscriptionWhenPull(businessConsumer.isPostSubscriptionWhenPull());
    defaultMQPushConsumer.setPullInterval(businessConsumer.getPullInterval());
    defaultMQPushConsumer.setSubscription(businessConsumer.getSubscription());
    defaultMQPushConsumer.setUnitMode(businessConsumer.isUnitMode());
    try {
        defaultMQPushConsumer.setClientCallbackExecutorThreads(businessConsumer.getClientCallbackExecutorThreads());
    } catch (AbstractMethodError e) {
    } catch (NoSuchMethodError e) {
    }
    try {
        defaultMQPushConsumer.setClientIP(businessConsumer.getClientIP());
    } catch (AbstractMethodError e) {
    } catch (NoSuchMethodError e) {
    }
    try {
        defaultMQPushConsumer.setHeartbeatBrokerInterval(businessConsumer.getHeartbeatBrokerInterval());
    } catch (AbstractMethodError e) {
    } catch (NoSuchMethodError e) {
    }
    try {
        defaultMQPushConsumer.setPersistConsumerOffsetInterval(businessConsumer.getPersistConsumerOffsetInterval());
    } catch (AbstractMethodError e) {
    } catch (NoSuchMethodError e) {
    }
    try {
        defaultMQPushConsumer.setPollNameServerInteval(businessConsumer.getPollNameServerInteval());
    } catch (AbstractMethodError e) {
    } catch (NoSuchMethodError e) {
    }
    try {
        defaultMQPushConsumer.setUnitName(businessConsumer.getUnitName());
    } catch (AbstractMethodError e) {
    } catch (NoSuchMethodError e) {
    }
    try {
        /**
         * 高版本才有,低版本无此方法,所以需要做一下此方法的兼容
         */
        defaultMQPushConsumer.setVipChannelEnabled(businessConsumer.isVipChannelEnabled());
    } catch (AbstractMethodError e) {
    } catch (NoSuchMethodError e) {
    }
    MessageListener messageListener = businessConsumer.getMessageListener();
    if (messageListener != null) {
        /**
         * 低版本只有registerMessageListener(com.alibaba.rocketmq.client.consumer.listener.MessageListener)
         */
        if (messageListener instanceof MessageListenerConcurrently) {
            try {
                defaultMQPushConsumer.registerMessageListener((MessageListenerConcurrently) messageListener);
            } catch (NoSuchMethodError e) {
                defaultMQPushConsumer.registerMessageListener(messageListener);
            }
        } else if (messageListener instanceof MessageListenerOrderly) {
            try {
                defaultMQPushConsumer.registerMessageListener((MessageListenerOrderly) messageListener);
            } catch (NoSuchMethodError e) {
                defaultMQPushConsumer.registerMessageListener(messageListener);
            }
        }
    }
    for (Map.Entry<String, SubscriptionData> entry : topicsInWhiteList.entrySet()) {
        SubscriptionData subscriptionData = entry.getValue();
        String topic = entry.getKey();
        String subString = subscriptionData.getSubString();
        String filterClassSource = null;
        try {
            /**
             * 高版本才有这个方法
             */
            filterClassSource = subscriptionData.getFilterClassSource();
        } catch (AbstractMethodError e) {
        } catch (NoSuchMethodError t) {
        }
        if (filterClassSource != null) {
            try {
                defaultMQPushConsumer.subscribe(Pradar.addClusterTestPrefix(topic), subString, filterClassSource);
                logger.info("Alibaba-RocketMQ shadow consumer subscribe topic : {} subString : {} filterClassSource : {}", Pradar.addClusterTestPrefix(topic), subString, filterClassSource);
            } catch (MQClientException e) {
                ErrorReporter.buildError().setErrorType(ErrorTypeEnum.MQ).setErrorCode("MQ-0001").setMessage("Alibaba-RocketMQ消费端subscribe失败!").setDetail("topic:" + topic + " fullClassName:" + subString + " filterClassSource:" + filterClassSource + "||" + e.getMessage()).report();
                logger.error("Alibaba-RocketMQ: subscribe shadow DefaultMQPushConsumer err! topic:{} fullClassName:{} " + "filterClassSource:{}", topic, subString, filterClassSource, e);
                return null;
            }
        } else {
            try {
                defaultMQPushConsumer.subscribe(Pradar.addClusterTestPrefix(topic), subString);
                logger.info("Alibaba-RocketMQ shadow consumer subscribe topic : {} subString : {}", Pradar.addClusterTestPrefix(topic), subString);
            } catch (MQClientException e) {
                ErrorReporter.buildError().setErrorType(ErrorTypeEnum.MQ).setErrorCode("MQ-0001").setMessage("Alibaba-RocketMQ消费端subscribe失败!").setDetail("topic:" + topic + " subExpression:" + subString + "||" + e.getMessage()).report();
                logger.error("Alibaba-RocketMQ: subscribe shadow DefaultMQPushConsumer err! topic:{} subExpression:{}", topic, subString, e);
                return null;
            }
        }
    }
    return defaultMQPushConsumer;
}
Also used : MessageListenerConcurrently(com.alibaba.rocketmq.client.consumer.listener.MessageListenerConcurrently) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentWeakHashMap(com.shulie.instrument.simulator.message.ConcurrentWeakHashMap) MessageListenerOrderly(com.alibaba.rocketmq.client.consumer.listener.MessageListenerOrderly) MessageListener(com.alibaba.rocketmq.client.consumer.listener.MessageListener) DefaultMQPushConsumer(com.alibaba.rocketmq.client.consumer.DefaultMQPushConsumer) ReflectException(com.shulie.instrument.simulator.api.reflect.ReflectException) SubscriptionData(com.alibaba.rocketmq.common.protocol.heartbeat.SubscriptionData) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentWeakHashMap(com.shulie.instrument.simulator.message.ConcurrentWeakHashMap) MQClientException(com.alibaba.rocketmq.client.exception.MQClientException)

Aggregations

DefaultMQPushConsumer (com.alibaba.rocketmq.client.consumer.DefaultMQPushConsumer)1 MessageListener (com.alibaba.rocketmq.client.consumer.listener.MessageListener)1 MessageListenerConcurrently (com.alibaba.rocketmq.client.consumer.listener.MessageListenerConcurrently)1 MessageListenerOrderly (com.alibaba.rocketmq.client.consumer.listener.MessageListenerOrderly)1 MQClientException (com.alibaba.rocketmq.client.exception.MQClientException)1 SubscriptionData (com.alibaba.rocketmq.common.protocol.heartbeat.SubscriptionData)1 ReflectException (com.shulie.instrument.simulator.api.reflect.ReflectException)1 ConcurrentWeakHashMap (com.shulie.instrument.simulator.message.ConcurrentWeakHashMap)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1