Search in sources :

Example 11 with MQConsumerInner

use of org.apache.rocketmq.client.impl.consumer.MQConsumerInner in project rocketmq by apache.

the class MQClientInstance method persistAllConsumerOffset.

private void persistAllConsumerOffset() {
    Iterator<Entry<String, MQConsumerInner>> it = this.consumerTable.entrySet().iterator();
    while (it.hasNext()) {
        Entry<String, MQConsumerInner> entry = it.next();
        MQConsumerInner impl = entry.getValue();
        impl.persistConsumerOffset();
    }
}
Also used : MQConsumerInner(org.apache.rocketmq.client.impl.consumer.MQConsumerInner) Entry(java.util.Map.Entry)

Example 12 with MQConsumerInner

use of org.apache.rocketmq.client.impl.consumer.MQConsumerInner in project rocketmq by apache.

the class MQClientInstance method consumeMessageDirectly.

public ConsumeMessageDirectlyResult consumeMessageDirectly(final MessageExt msg, final String consumerGroup, final String brokerName) {
    MQConsumerInner mqConsumerInner = this.consumerTable.get(consumerGroup);
    if (null != mqConsumerInner) {
        DefaultMQPushConsumerImpl consumer = (DefaultMQPushConsumerImpl) mqConsumerInner;
        ConsumeMessageDirectlyResult result = consumer.getConsumeMessageService().consumeMessageDirectly(msg, brokerName);
        return result;
    }
    return null;
}
Also used : MQConsumerInner(org.apache.rocketmq.client.impl.consumer.MQConsumerInner) DefaultMQPushConsumerImpl(org.apache.rocketmq.client.impl.consumer.DefaultMQPushConsumerImpl) ConsumeMessageDirectlyResult(org.apache.rocketmq.common.protocol.body.ConsumeMessageDirectlyResult)

Example 13 with MQConsumerInner

use of org.apache.rocketmq.client.impl.consumer.MQConsumerInner in project rocketmq by apache.

the class MQClientInstance method updateTopicRouteInfoFromNameServer.

public boolean updateTopicRouteInfoFromNameServer(final String topic, boolean isDefault, DefaultMQProducer defaultMQProducer) {
    try {
        if (this.lockNamesrv.tryLock(LOCK_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)) {
            try {
                TopicRouteData topicRouteData;
                if (isDefault && defaultMQProducer != null) {
                    topicRouteData = this.mQClientAPIImpl.getDefaultTopicRouteInfoFromNameServer(defaultMQProducer.getCreateTopicKey(), 1000 * 3);
                    if (topicRouteData != null) {
                        for (QueueData data : topicRouteData.getQueueDatas()) {
                            int queueNums = Math.min(defaultMQProducer.getDefaultTopicQueueNums(), data.getReadQueueNums());
                            data.setReadQueueNums(queueNums);
                            data.setWriteQueueNums(queueNums);
                        }
                    }
                } else {
                    topicRouteData = this.mQClientAPIImpl.getTopicRouteInfoFromNameServer(topic, 1000 * 3);
                }
                if (topicRouteData != null) {
                    TopicRouteData old = this.topicRouteTable.get(topic);
                    boolean changed = topicRouteDataIsChange(old, topicRouteData);
                    if (!changed) {
                        changed = this.isNeedUpdateTopicRouteInfo(topic);
                    } else {
                        log.info("the topic[{}] route info changed, old[{}] ,new[{}]", topic, old, topicRouteData);
                    }
                    if (changed) {
                        TopicRouteData cloneTopicRouteData = topicRouteData.cloneTopicRouteData();
                        for (BrokerData bd : topicRouteData.getBrokerDatas()) {
                            this.brokerAddrTable.put(bd.getBrokerName(), bd.getBrokerAddrs());
                        }
                        // Update Pub info
                        {
                            TopicPublishInfo publishInfo = topicRouteData2TopicPublishInfo(topic, topicRouteData);
                            publishInfo.setHaveTopicRouterInfo(true);
                            Iterator<Entry<String, MQProducerInner>> it = this.producerTable.entrySet().iterator();
                            while (it.hasNext()) {
                                Entry<String, MQProducerInner> entry = it.next();
                                MQProducerInner impl = entry.getValue();
                                if (impl != null) {
                                    impl.updateTopicPublishInfo(topic, publishInfo);
                                }
                            }
                        }
                        // Update sub info
                        {
                            Set<MessageQueue> subscribeInfo = topicRouteData2TopicSubscribeInfo(topic, topicRouteData);
                            Iterator<Entry<String, MQConsumerInner>> it = this.consumerTable.entrySet().iterator();
                            while (it.hasNext()) {
                                Entry<String, MQConsumerInner> entry = it.next();
                                MQConsumerInner impl = entry.getValue();
                                if (impl != null) {
                                    impl.updateTopicSubscribeInfo(topic, subscribeInfo);
                                }
                            }
                        }
                        log.info("topicRouteTable.put. Topic = {}, TopicRouteData[{}]", topic, cloneTopicRouteData);
                        this.topicRouteTable.put(topic, cloneTopicRouteData);
                        return true;
                    }
                } else {
                    log.warn("updateTopicRouteInfoFromNameServer, getTopicRouteInfoFromNameServer return null, Topic: {}", topic);
                }
            } catch (Exception e) {
                if (!topic.startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX) && !topic.equals(MixAll.DEFAULT_TOPIC)) {
                    log.warn("updateTopicRouteInfoFromNameServer Exception", e);
                }
            } finally {
                this.lockNamesrv.unlock();
            }
        } else {
            log.warn("updateTopicRouteInfoFromNameServer tryLock timeout {}ms", LOCK_TIMEOUT_MILLIS);
        }
    } catch (InterruptedException e) {
        log.warn("updateTopicRouteInfoFromNameServer Exception", e);
    }
    return false;
}
Also used : MQConsumerInner(org.apache.rocketmq.client.impl.consumer.MQConsumerInner) Set(java.util.Set) HashSet(java.util.HashSet) MQProducerInner(org.apache.rocketmq.client.impl.producer.MQProducerInner) BrokerData(org.apache.rocketmq.common.protocol.route.BrokerData) QueueData(org.apache.rocketmq.common.protocol.route.QueueData) MQClientException(org.apache.rocketmq.client.exception.MQClientException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) TopicRouteData(org.apache.rocketmq.common.protocol.route.TopicRouteData) Entry(java.util.Map.Entry) TopicPublishInfo(org.apache.rocketmq.client.impl.producer.TopicPublishInfo) Iterator(java.util.Iterator)

Example 14 with MQConsumerInner

use of org.apache.rocketmq.client.impl.consumer.MQConsumerInner in project rocketmq by apache.

the class MQClientInstance method adjustThreadPool.

public void adjustThreadPool() {
    Iterator<Entry<String, MQConsumerInner>> it = this.consumerTable.entrySet().iterator();
    while (it.hasNext()) {
        Entry<String, MQConsumerInner> entry = it.next();
        MQConsumerInner impl = entry.getValue();
        if (impl != null) {
            try {
                if (impl instanceof DefaultMQPushConsumerImpl) {
                    DefaultMQPushConsumerImpl dmq = (DefaultMQPushConsumerImpl) impl;
                    dmq.adjustThreadPool();
                }
            } catch (Exception e) {
            }
        }
    }
}
Also used : MQConsumerInner(org.apache.rocketmq.client.impl.consumer.MQConsumerInner) DefaultMQPushConsumerImpl(org.apache.rocketmq.client.impl.consumer.DefaultMQPushConsumerImpl) Entry(java.util.Map.Entry) MQClientException(org.apache.rocketmq.client.exception.MQClientException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException)

Example 15 with MQConsumerInner

use of org.apache.rocketmq.client.impl.consumer.MQConsumerInner in project rocketmq by apache.

the class MQClientInstance method checkClientInBroker.

public void checkClientInBroker() throws MQClientException {
    Iterator<Entry<String, MQConsumerInner>> it = this.consumerTable.entrySet().iterator();
    while (it.hasNext()) {
        Entry<String, MQConsumerInner> entry = it.next();
        Set<SubscriptionData> subscriptionInner = entry.getValue().subscriptions();
        if (subscriptionInner == null || subscriptionInner.isEmpty()) {
            return;
        }
        for (SubscriptionData subscriptionData : subscriptionInner) {
            if (ExpressionType.isTagType(subscriptionData.getExpressionType())) {
                continue;
            }
            // may need to check one broker every cluster...
            // assume that the configs of every broker in cluster are the the same.
            String addr = findBrokerAddrByTopic(subscriptionData.getTopic());
            if (addr != null) {
                try {
                    this.getMQClientAPIImpl().checkClientInBroker(addr, entry.getKey(), this.clientId, subscriptionData, 3 * 1000);
                } catch (Exception e) {
                    if (e instanceof MQClientException) {
                        throw (MQClientException) e;
                    } else {
                        throw new MQClientException("Check client in broker error, maybe because you use " + subscriptionData.getExpressionType() + " to filter message, but server has not been upgraded to support!" + "This error would not affect the launch of consumer, but may has impact on message receiving if you " + "have use the new features which are not supported by server, please check the log!", e);
                    }
                }
            }
        }
    }
}
Also used : MQConsumerInner(org.apache.rocketmq.client.impl.consumer.MQConsumerInner) Entry(java.util.Map.Entry) SubscriptionData(org.apache.rocketmq.common.protocol.heartbeat.SubscriptionData) MQClientException(org.apache.rocketmq.client.exception.MQClientException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Aggregations

MQConsumerInner (org.apache.rocketmq.client.impl.consumer.MQConsumerInner)18 UnsupportedEncodingException (java.io.UnsupportedEncodingException)10 Entry (java.util.Map.Entry)10 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)10 MQClientException (org.apache.rocketmq.client.exception.MQClientException)10 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)10 DefaultMQPushConsumerImpl (org.apache.rocketmq.client.impl.consumer.DefaultMQPushConsumerImpl)6 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 ConcurrentMap (java.util.concurrent.ConcurrentMap)4 MQProducerInner (org.apache.rocketmq.client.impl.producer.MQProducerInner)4 SubscriptionData (org.apache.rocketmq.common.protocol.heartbeat.SubscriptionData)4 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 Set (java.util.Set)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 ProcessQueue (org.apache.rocketmq.client.impl.consumer.ProcessQueue)2 TopicPublishInfo (org.apache.rocketmq.client.impl.producer.TopicPublishInfo)2 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)2