Search in sources :

Example 1 with MQProducerInner

use of org.apache.rocketmq.client.impl.producer.MQProducerInner in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class MQClientInstance method prepareHeartbeatData.

private HeartbeatData prepareHeartbeatData() {
    HeartbeatData heartbeatData = new HeartbeatData();
    // clientID
    heartbeatData.setClientID(this.clientId);
    // Consumer
    for (Map.Entry<String, MQConsumerInner> entry : this.consumerTable.entrySet()) {
        MQConsumerInner impl = entry.getValue();
        if (impl != null) {
            ConsumerData consumerData = new ConsumerData();
            consumerData.setGroupName(impl.groupName());
            consumerData.setConsumeType(impl.consumeType());
            consumerData.setMessageModel(impl.messageModel());
            consumerData.setConsumeFromWhere(impl.consumeFromWhere());
            consumerData.getSubscriptionDataSet().addAll(impl.subscriptions());
            consumerData.setUnitMode(impl.isUnitMode());
            heartbeatData.getConsumerDataSet().add(consumerData);
        }
    }
    // Producer
    for (Map.Entry<String, MQProducerInner> /* group */
    entry : this.producerTable.entrySet()) {
        MQProducerInner impl = entry.getValue();
        if (impl != null) {
            ProducerData producerData = new ProducerData();
            producerData.setGroupName(entry.getKey());
            heartbeatData.getProducerDataSet().add(producerData);
        }
    }
    return heartbeatData;
}
Also used : MQConsumerInner(org.apache.rocketmq.client.impl.consumer.MQConsumerInner) MQProducerInner(org.apache.rocketmq.client.impl.producer.MQProducerInner) ConsumerData(org.apache.rocketmq.common.protocol.heartbeat.ConsumerData) HeartbeatData(org.apache.rocketmq.common.protocol.heartbeat.HeartbeatData) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) ProducerData(org.apache.rocketmq.common.protocol.heartbeat.ProducerData)

Example 2 with MQProducerInner

use of org.apache.rocketmq.client.impl.producer.MQProducerInner 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 3 with MQProducerInner

use of org.apache.rocketmq.client.impl.producer.MQProducerInner in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

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(), // 获取topic规则
                    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) {
                                    // 更新topic的PublishInfo
                                    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 4 with MQProducerInner

use of org.apache.rocketmq.client.impl.producer.MQProducerInner in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class ClientRemotingProcessor method checkTransactionState.

public RemotingCommand checkTransactionState(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    final CheckTransactionStateRequestHeader requestHeader = (CheckTransactionStateRequestHeader) request.decodeCommandCustomHeader(CheckTransactionStateRequestHeader.class);
    final ByteBuffer byteBuffer = ByteBuffer.wrap(request.getBody());
    final MessageExt messageExt = MessageDecoder.decode(byteBuffer);
    if (messageExt != null) {
        final String group = messageExt.getProperty(MessageConst.PROPERTY_PRODUCER_GROUP);
        if (group != null) {
            MQProducerInner producer = this.mqClientFactory.selectProducer(group);
            if (producer != null) {
                final String addr = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
                producer.checkTransactionState(addr, messageExt, requestHeader);
            } else {
                log.debug("checkTransactionState, pick producer by group[{}] failed", group);
            }
        } else {
            log.warn("checkTransactionState, pick producer group failed");
        }
    } else {
        log.warn("checkTransactionState, decode message failed");
    }
    return null;
}
Also used : MessageExt(org.apache.rocketmq.common.message.MessageExt) MQProducerInner(org.apache.rocketmq.client.impl.producer.MQProducerInner) ByteBuffer(java.nio.ByteBuffer) CheckTransactionStateRequestHeader(org.apache.rocketmq.common.protocol.header.CheckTransactionStateRequestHeader)

Example 5 with MQProducerInner

use of org.apache.rocketmq.client.impl.producer.MQProducerInner in project rocketmq by apache.

the class MQClientInstance method prepareHeartbeatData.

private HeartbeatData prepareHeartbeatData() {
    HeartbeatData heartbeatData = new HeartbeatData();
    // clientID
    heartbeatData.setClientID(this.clientId);
    // Consumer
    for (Map.Entry<String, MQConsumerInner> entry : this.consumerTable.entrySet()) {
        MQConsumerInner impl = entry.getValue();
        if (impl != null) {
            ConsumerData consumerData = new ConsumerData();
            consumerData.setGroupName(impl.groupName());
            consumerData.setConsumeType(impl.consumeType());
            consumerData.setMessageModel(impl.messageModel());
            consumerData.setConsumeFromWhere(impl.consumeFromWhere());
            consumerData.getSubscriptionDataSet().addAll(impl.subscriptions());
            consumerData.setUnitMode(impl.isUnitMode());
            heartbeatData.getConsumerDataSet().add(consumerData);
        }
    }
    // Producer
    for (Map.Entry<String, MQProducerInner> /* group */
    entry : this.producerTable.entrySet()) {
        MQProducerInner impl = entry.getValue();
        if (impl != null) {
            ProducerData producerData = new ProducerData();
            producerData.setGroupName(entry.getKey());
            heartbeatData.getProducerDataSet().add(producerData);
        }
    }
    return heartbeatData;
}
Also used : MQConsumerInner(org.apache.rocketmq.client.impl.consumer.MQConsumerInner) MQProducerInner(org.apache.rocketmq.client.impl.producer.MQProducerInner) ConsumerData(org.apache.rocketmq.common.protocol.heartbeat.ConsumerData) HeartbeatData(org.apache.rocketmq.common.protocol.heartbeat.HeartbeatData) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) ProducerData(org.apache.rocketmq.common.protocol.heartbeat.ProducerData)

Aggregations

MQProducerInner (org.apache.rocketmq.client.impl.producer.MQProducerInner)6 MQConsumerInner (org.apache.rocketmq.client.impl.consumer.MQConsumerInner)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ByteBuffer (java.nio.ByteBuffer)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2 Set (java.util.Set)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)2 MQClientException (org.apache.rocketmq.client.exception.MQClientException)2 TopicPublishInfo (org.apache.rocketmq.client.impl.producer.TopicPublishInfo)2 MessageExt (org.apache.rocketmq.common.message.MessageExt)2 CheckTransactionStateRequestHeader (org.apache.rocketmq.common.protocol.header.CheckTransactionStateRequestHeader)2 ConsumerData (org.apache.rocketmq.common.protocol.heartbeat.ConsumerData)2 HeartbeatData (org.apache.rocketmq.common.protocol.heartbeat.HeartbeatData)2 ProducerData (org.apache.rocketmq.common.protocol.heartbeat.ProducerData)2