In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the meaning of Rabbit MQ broadcast mode". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what does the broadcasting mode of Rabbit MQ mean?"
Recently, we have made a demand that we need an interface to send messages to more than 30,000 people. Because of the large number of people, it is certainly not possible to send synchronously with one thread. If it is a thread, even if there is a problem with a piece of data, it will lead to the failure of this transmission. Therefore, I thought of using the broadcast mode of Rabbit MQ to do it.
The broadcast mode is to put the message you want to send into the "switch", and then the corresponding queue goes to the switch to get the message to the consumer (the end of the receiving message) for consumption. As shown in the following figure:
(I found the emmm picture on the Internet. I drew it too ugly.)
In the above process, the main thread can return after the first step, which is an asynchronous process. There is no need to wait for the queue to fetch data from the switch. The two processes of "main thread return" and "queue fetching data to consumers" are carried out at the same time. In this way, you don't have to wait for more than 30,000 messages to be sent before you can move on to the next step.
The function is briefly introduced, and then there is the code. I will only make a simple record.
The first is a configuration class
Package com.apps.rabbit.FanoutMessage;import org.springframework.amqp.core.Binding;import org.springframework.amqp.core.BindingBuilder;import org.springframework.amqp.core.FanoutExchange;import org.springframework.amqp.core.Queue;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration @ Configurationpublic class TestExchangeConfiguration {/ * create a broadcast switch * * @ return switch instance * / @ Bean public FanoutExchange fanoutExchanges () {System.out.println ("[switch instance created successfully]"); return new FanoutExchange (Constants.FANOUT_EXCHANGE_NAME) } / * Test queue one * * @ return queue instance * / @ Bean public Queue queue1 () {System.out.println ("[test queue instance created successfully]"); return new Queue (Constants.TEST_QUEUE1_NAME) } / * * bind queue one to switch * * @ return binding object * / @ Bean public Binding bingQueue1ToExchange () {System.out.println ("[bind queue one to switch successfully]"); return BindingBuilder.bind (queue1 ()) .to (fanoutExchanges ());}}
The above is used to create the switch and queue and bind the queue to the switch
The next one is the news producer.
Package com.apps.rabbit.FanoutMessage;import org.springframework.amqp.rabbit.core.RabbitTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;@Componentpublic class Sender {@ Autowired private RabbitTemplate rabbitTemplate / * send message * * @ param message message content * Note: routingKey can be specified or not specified. Here we give the queue name * / public void sendMessage (Object message) {System.out.println ("[message sender] sends a message to the fanout switch with the message: {}" + message) RabbitTemplate.convertAndSend (Constants.FANOUT_EXCHANGE_NAME, Constants.TEST_QUEUE1_NAME, message);}}
Add another helper class to store the constant (don't ask why, everybody does it)
Package com.apps.rabbit.FanoutMessage;public class Constants {/ * switch name * / public static final String FANOUT_EXCHANGE_NAME = "message.fanout.exchange"; / * Test queue name 1 * / public static final String TEST_QUEUE1_NAME = "messageReceiverQueue";}
Finally, there is the consumer of the news.
Package com.apps.rabbit.FanoutMessage;import com.alibaba.fastjson.JSONArray;import com.alibaba.fastjson.JSONException;import com.alibaba.fastjson.JSONObject;import com.apps.bcodemsg.MsgResponse;import com.apps.model.basic.message.InfoMessageUser;import com.apps.model.basic.message.InfoMessageUserVO;import com.apps.service.basic.message.InfoMessageUserService;import com.rabbitmq.client.Channel;import org.apache.commons.lang.StringUtils;import org.springframework.amqp.core.Message;import org.springframework.amqp.rabbit.annotation.RabbitHandler Import org.springframework.amqp.rabbit.annotation.RabbitListener;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import java.io.IOException;import java.util.ArrayList;import java.util.List;@Componentpublic class Receiver1 {@ Autowired private InfoMessageUserService infoMessageUserService; private static String CONTENT_KEY= "message"; private static String MESSAGE_ID_KEY= "messageId"; private static String USER_ID_KEY= "userId" RabbitListener (queues = "messageReceiverQueue") @ RabbitHandler public void receiveMessage (String messageInfo, Channel channel, Message message) throws IOException {doSomething ();}}
That's it. You just need to call the producer in your business. The message will be automatically received by the queue, and what to do with the received message is a matter of business logic.
The above is only the simplest example, and only one queue is used. If you want to add multiple queues, you need to declare and bind them in the configuration class. In addition, when calling the producer, you need to remove the second parameter of the rabbitTemplate.convertAndSend () method.
Ok!
At this point, I believe you have a deeper understanding of "what the broadcast mode of Rabbit MQ means". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.