Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of MessageQueueSelector in rocketmq

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about the role of MessageQueueSelector in rocketmq. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

MessageQueueSelector

RocketmqlichclientMube 4.5.2Mutual sources.jarpickUniverse orgUniverse apacheamprocketmqplicclientplicproducerUniverse MessageQueueSelector.java

Public interface MessageQueueSelector {MessageQueue select (final List mqs, final Message msg, final Object arg);}

The MessageQueueSelector interface defines the select method and returns MessageQueue;. It has several implementation classes, namely SelectMessageQueueByHash, SelectMessageQueueByRandom, and SelectMessageQueueByMachineRoom.

SelectMessageQueueByHash

RocketmqLYCLENTPUR 4.5.2lur sources.jarCUPUR orgUnix apacheUnix RocketmqqClipSelectMessageQueueByHash.java

Public class SelectMessageQueueByHash implements MessageQueueSelector {@ Override public MessageQueue select (List mqs, Message msg, Object arg) {int value = arg.hashCode (); if (value < 0) {value = Math.abs (value);} value = value% mqs.size (); return mqs.get (value);}}

SelectMessageQueueByHash implements the MessageQueueSelector interface. Its select method takes the absolute value of the hashcode of the arg parameter, and then takes the remainder of mqs.size () to get the subscript of the target queue in mqs.

SelectMessageQueueByRandom

RocketmqlichclientPU4.5.2mursources.jarchangeorgUnitableCrocketmqUnitionProducerClearSelectMessageQueueByRandom.java

Public class SelectMessageQueueByRandom implements MessageQueueSelector {private Random random = new Random (System.currentTimeMillis ()); @ Override public MessageQueue select (List mqs, Message msg, Object arg) {int value = random.nextInt (mqs.size ()); return mqs.get (value);}}

SelectMessageQueueByRandom implements the MessageQueueSelector interface, and its select method takes a random value directly according to mqs.size () as the subscript of the target queue in mqs.

SelectMessageQueueByMachineRoom

RocketmqluclientMube 4.5.2lur sources.jarroomUnix orgapacheUnix rocketmqqqqclientSelectMessageQueueByMachineRoom.java

Public class SelectMessageQueueByMachineRoom implements MessageQueueSelector {private Set consumeridcs; @ Override public MessageQueue select (List mqs, Message msg, Object arg) {return null;} public Set getConsumeridcs () {return consumeridcs;} public void setConsumeridcs (Set consumeridcs) {this.consumeridcs = consumeridcs;}}

SelectMessageQueueByMachineRoom implements the MessageQueueSelector interface, and its select method currently returns null

RocketMQTemplate

RocketmqFIQspringMusbootMutel 2.0.3Methods sources.jarAcheUnixorgUnixapacheUnixRocketmqqqSpringUniqqSpringUnix coreMQTemplate.java

Public class RocketMQTemplate extends AbstractMessageSendingTemplate implements InitializingBean, DisposableBean {private static final Logger log = LoggerFactory.getLogger (RocketMQTemplate.class); private DefaultMQProducer producer; private ObjectMapper objectMapper; private String charset = "UTF-8"; private MessageQueueSelector messageQueueSelector = new SelectMessageQueueByHash (); private final Map cache = new ConcurrentHashMap (); / / only put TransactionMQProducer by now available! Public DefaultMQProducer getProducer () {return producer;} public void setProducer (DefaultMQProducer producer) {this.producer = producer;} public ObjectMapper getObjectMapper () {return objectMapper;} public void setObjectMapper (ObjectMapper objectMapper) {this.objectMapper = objectMapper;} public String getCharset () {return charset;} public void setCharset (String charset) {this.charset = charset } public MessageQueueSelector getMessageQueueSelector () {return messageQueueSelector;} public void setMessageQueueSelector (MessageQueueSelector messageQueueSelector) {this.messageQueueSelector = messageQueueSelector;} / /.}

The MessageQueueSelector created by RocketMQTemplate by default is SelectMessageQueueByHash.

After reading the above, do you have any further understanding of the role of MessageQueueSelector in rocketmq? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report