最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

RocketMQ?producer發(fā)送者淺析

 更新時間:2023年04月24日 11:33:09   作者:Acqierement  
RocketMQ生產(chǎn)者是一種高性能、可靠的消息發(fā)送者,能夠將消息快速、可靠地發(fā)送到RocketMQ消息隊列中。它具有多種消息發(fā)送模式和消息發(fā)送方式,可以根據(jù)不同的業(yè)務需求進行靈活配置

發(fā)送者其實比較簡單,需要做的就是首先確定往哪里發(fā)送,其次怎么讓消息發(fā)送順暢。我們就看一下具體的代碼吧。

首先調用start方法。完成各個類的初始化,啟動多個定時任務,其中一個定時任務是updateTopicRouteInfoFromNameServer,這個方法里面和nameService建立長連接,同時維護了topicRouteTable和brokerAddrTable等緩存。topicRouteTable里面維護了這個topic包括有哪些queue和broker。這樣producer才可以知道要發(fā)往哪里。

啟動的流程主要在這個方法中:

MQClientInstance#start

public void start() throws MQClientException {
    synchronized (this) {
        switch (this.serviceState) {
            case CREATE_JUST:
                this.serviceState = ServiceState.START_FAILED;
                // If not specified,looking address from name server
                if (null == this.clientConfig.getNamesrvAddr()) {
                    this.mQClientAPIImpl.fetchNameServerAddr();
                }
                // Start request-response channel
                this.mQClientAPIImpl.start();
                // Start various schedule tasks
                this.startScheduledTask();
                // Start pull service
                this.pullMessageService.start();
                // Start rebalance service
                this.rebalanceService.start();
                // Start push service
                this.defaultMQProducer.getDefaultMQProducerImpl().start(false);
                log.info("the client factory [{}] start OK", this.clientId);
                this.serviceState = ServiceState.RUNNING;
                break;
            case START_FAILED:
                throw new MQClientException("The Factory object[" + this.getClientId() + "] has been created before, and failed.", null);
            default:
                break;
        }
    }
}

其中啟動了一系列定時任務,包括org.apache.rocketmq.client.impl.factory.MQClientInstance#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) {
                        // 從nameServer獲取topciRouteData
                        topicRouteData = this.mQClientAPIImpl.getDefaultTopicRouteInfoFromNameServer(defaultMQProducer.getCreateTopicKey(),
                            clientConfig.getMqClientApiTimeout());
                        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, clientConfig.getMqClientApiTimeout());
                    }
                    if (topicRouteData != null) {
                        TopicRouteData old = this.topicRouteTable.get(topic);
                        boolean changed = topicRouteData.topicRouteDataChanged(old);
                        if (!changed) {
                            changed = this.isNeedUpdateTopicRouteInfo(topic);
                        } else {
                            log.info("the topic[{}] route info changed, old[{}] ,new[{}]", topic, old, topicRouteData);
                        }
                        if (changed) {
                            for (BrokerData bd : topicRouteData.getBrokerDatas()) {
                                this.brokerAddrTable.put(bd.getBrokerName(), bd.getBrokerAddrs());
                            }
                            // Update endpoint map
                            {
                                ConcurrentMap<MessageQueue, String> mqEndPoints = topicRouteData2EndpointsForStaticTopic(topic, topicRouteData);
                                if (!mqEndPoints.isEmpty()) {
                                    topicEndPointsTable.put(topic, mqEndPoints);
                                }
                            }
                            // Update Pub info
                            {
                                // 生成topicPublishInfo
                                TopicPublishInfo publishInfo = topicRouteData2TopicPublishInfo(topic, topicRouteData);
                                publishInfo.setHaveTopicRouterInfo(true);
                                for (Entry<String, MQProducerInner> entry : this.producerTable.entrySet()) {
                                    MQProducerInner impl = entry.getValue();
                                    if (impl != null) {
                                        // 更新 topicPublishInfo
                                        impl.updateTopicPublishInfo(topic, publishInfo);
                                    }
                                }
                            }
                            // Update sub info
                            if (!consumerTable.isEmpty()) {
                                Set<MessageQueue> subscribeInfo = topicRouteData2TopicSubscribeInfo(topic, topicRouteData);
                                for (Entry<String, MQConsumerInner> entry : this.consumerTable.entrySet()) {
                                    MQConsumerInner impl = entry.getValue();
                                    if (impl != null) {
                                        impl.updateTopicSubscribeInfo(topic, subscribeInfo);
                                    }
                                }
                            }
                            TopicRouteData cloneTopicRouteData = new TopicRouteData(topicRouteData);
                            log.info("topicRouteTable.put. Topic = {}, TopicRouteData[{}]", topic, cloneTopicRouteData);
                            this.topicRouteTable.put(topic, cloneTopicRouteData);
                            return true;
                        }
                    } else {
                        log.warn("updateTopicRouteInfoFromNameServer, getTopicRouteInfoFromNameServer return null, Topic: {}. [{}]", topic, this.clientId);
                    }
                } catch (MQClientException e) {
                    if (!topic.startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX) && !topic.equals(TopicValidator.AUTO_CREATE_TOPIC_KEY_TOPIC)) {
                        log.warn("updateTopicRouteInfoFromNameServer Exception", e);
                    }
                } catch (RemotingException e) {
                    log.error("updateTopicRouteInfoFromNameServer Exception", e);
                    throw new IllegalStateException(e);
                } finally {
                    this.lockNamesrv.unlock();
                }
            } else {
                log.warn("updateTopicRouteInfoFromNameServer tryLock timeout {}ms. [{}]", LOCK_TIMEOUT_MILLIS, this.clientId);
            }
        } catch (InterruptedException e) {
            log.warn("updateTopicRouteInfoFromNameServer Exception", e);
        }
        return false;
    }

通過方法名也知道是從nameServer獲取這個topic相關的broke數(shù)據(jù),拿到TopicRouteData數(shù)據(jù)。先更新brokerAddrTable,存儲borker具體的地址。然后在org.apache.rocketmq.client.impl.factory.MQClientInstance#topicRouteData2TopicPublishInfo里面再進一步生成TopicPublishInfo數(shù)據(jù)。TopicPublishInfo是對TopicRouteData的一個封裝,除了TopicRouteData,還有messageQueue數(shù)據(jù),messageQueue是Queue和Borker的交集,會根據(jù)配置的queue數(shù)量,生成具體的messageQueue,queueId就是0,1,2,3,4他們自己的順序。

所以有了TopicPublishInfo數(shù)據(jù),就知道往哪里發(fā)了。

發(fā)送消息的過程。

  • 先找到TopicPublishInfo。TopicPublishInfo里面有一個MessageQueue的list。
  • 從MessageQueueList里面拿到一個messageQueue。 如果沒有開啟sendLatencyFaultEnable,默認就是采用輪詢方法。具體的輪詢方式就是,TopicPublishInfo里面維護了一個序號index,每次index自增1,然后通過index去MessageQueueList里面拿一個。
  • 拿到了MessageQueue之后,里面有broker的name,根據(jù)name去找broker的ip地址,發(fā)送數(shù)據(jù)。這個ip地址就是前面提到的brokerAddrTable變量,在updateTopicRouteInfoFromNameServer方法里面維護的。

到此這篇關于RocketMQ producer發(fā)送者淺析的文章就介紹到這了,更多相關RocketMQ producer內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • RestTemplate響應中如何獲取輸入流InputStream

    RestTemplate響應中如何獲取輸入流InputStream

    這篇文章主要介紹了RestTemplate響應中如何獲取輸入流InputStream問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Java定時器Timer與TimerTask的使用詳解

    Java定時器Timer與TimerTask的使用詳解

    這篇文章主要介紹了Java定時器Timer與TimerTask的使用詳解,在JDK類庫中Timer主要負責計劃任務的功能,也就是在指定時間執(zhí)行某一任務,執(zhí)行時候會在主線程之外起一個單獨的線程執(zhí)行指定的任務,該類主要是設置任務計劃,但封裝的類是TimerTask類,需要的朋友可以參考下
    2023-10-10
  • 詳解Servlet之過濾器(Filter)

    詳解Servlet之過濾器(Filter)

    本篇文章主要介紹了Servlet——過濾器(Filter),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • java操作Redis緩存設置過期時間的方法

    java操作Redis緩存設置過期時間的方法

    這篇文章主要介紹了java操作Redis緩存設置過期時間的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-06-06
  • Java設計模式七大原則之開閉原則詳解

    Java設計模式七大原則之開閉原則詳解

    開閉原則,又稱為OCP原則,即一個軟件實體如類,模塊和函數(shù)應該對擴展開放,對修改關閉。本文將詳細介紹Java設計模式七大原則之一的開閉原則,需要的可以參考一下
    2022-02-02
  • 實戰(zhàn)分布式醫(yī)療掛號系統(tǒng)之設置微服務搭建醫(yī)院模塊

    實戰(zhàn)分布式醫(yī)療掛號系統(tǒng)之設置微服務搭建醫(yī)院模塊

    這篇文章主要為大家介紹了實戰(zhàn)分布式醫(yī)療掛號系統(tǒng)之搭建醫(yī)院設置微服務模塊,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-04-04
  • 淺析Java關鍵詞synchronized的使用

    淺析Java關鍵詞synchronized的使用

    Synchronized是java虛擬機為線程安全而引入的。這篇文章主要為大家介紹一下Java關鍵詞synchronized的使用與原理,需要的可以參考一下
    2022-12-12
  • Java?使用geotools讀取tiff數(shù)據(jù)的示例代碼

    Java?使用geotools讀取tiff數(shù)據(jù)的示例代碼

    這篇文章主要介紹了Java?通過geotools讀取tiff,一般對于tiff數(shù)據(jù)的讀取,都會借助于gdal,本文結合示例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2022-04-04
  • Java使用easyExcel導出數(shù)據(jù)及單元格多張圖片

    Java使用easyExcel導出數(shù)據(jù)及單元格多張圖片

    除了平時簡單的數(shù)據(jù)導出需求外,我們也經(jīng)常會遇到一些有固定格式或者模板要求的數(shù)據(jù)導出,下面這篇文章主要給大家介紹了關于Java使用easyExcel導出數(shù)據(jù)及單元格多張圖片的相關資料,需要的朋友可以參考下
    2023-05-05
  • java Class文件結構解析常量池字節(jié)碼

    java Class文件結構解析常量池字節(jié)碼

    這篇文章主要為大家介紹了java Class文件的整體結構解析常量池字節(jié)碼詳細講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07

最新評論

义马市| 旌德县| 宁晋县| 古田县| 方正县| 定日县| 贵溪市| 柳林县| 兴宁市| 萨迦县| 商水县| 游戏| 鹤峰县| 宜川县| 宁强县| 新营市| 呼玛县| 柳河县| 徐州市| 海城市| 长宁区| 涟水县| 安国市| 宜州市| 淮滨县| 安义县| 新疆| 通化县| 宝兴县| 南漳县| 连江县| 内江市| 奉贤区| 蓝山县| 宜兰市| 夏津县| 威信县| 乐亭县| 桦甸市| 景德镇市| 偏关县|