In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about the simple use of RabbitMq. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Add dependencies to the 1.pom file
Org.springframework.bootspring-boot-starter-amqp2.3.3.RELEASE
two。 Configuration files, configuring mq
Automatic configuration information here I turn on the ACK message to confirm the server.port=8088# server configuration spring.application.name=rabbitmq-test-sending#rabbitmq connection parameters spring.rabbitmq.host=localhostspring.rabbitmq.port=5672spring.rabbitmq.username=guestspring.rabbitmq.password=guest# open send confirm spring.rabbitmq.publisher-confirms=true# open send failure return spring.rabbitmq.publisher-returns=true# open ACKspring.rabbitmq.listener.direct.acknowledge-mode=manual
3.Rabbit configuration class, using topic switches, using wildcards, one switch corresponds to multiple queue
Import org.springframework.amqp.core.*;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import java.util.HashMap;import java.util.Map;@Configurationpublic class RabbitmqConfig {/ / queue @ Bean public Queue queueTest1 () {return new Queue ("queueTest1", true) } / * * set the TTL (expiration time) of the message queue * * / @ Bean public Queue queueTest2 () {/ * the name of the queue, whether it is persistent, exclusive, exclusive, and automatically deleted. Other attribute parameters of the queue * (1) x-message-ttl: expiration time of the message (in milliseconds) * (2) x-expires: the expiration time of the queue. How long the queue has not been accessed will be deleted (in milliseconds); * (3) x-max-length: the maximum length of the queue. If it exceeds this maximum, the message will be deleted from the queue header. * (4) x-max-length-bytes: queue message content occupies the maximum space and is limited by memory size. If this threshold is exceeded, messages are deleted from the queue header; * (5) x-overflow: set queue overflow behavior. This determines what happens to the message when the maximum length of the queue is reached. Valid values are drop-head, reject-publish, or reject-publish-dlx. * (6) x-dead-letter-exchange: dead letter switch name. Messages that are expired or deleted (due to excessive queue length or space exceeding the threshold) can be specified to be sent to this switch. * (7) x-dead-letter-routing-key: dead message routing key, which is used when the message is sent to the dead letter switch. If it is not set, the original routing key value of the message is used * (8) x-single-active-consumer: indicates whether the queue is a single active consumer. In the case of true, there is only one consumer consumption message in the registered consumer group, and the other is ignored. Message looping to all consumers on false (default false) * (9) x-max-priority: the maximum number of priorities to be supported by the queue If not set, the queue will not support message priority * (10) x-queue-mode (Lazy mode): set the queue to delay mode and keep as many messages as possible on disk to reduce the use of RAM; if not set, the queue will retain the memory cache to deliver messages as quickly as possible; * (11) x-queue-master-locator: set the master node information of the mirror queue in cluster mode. * / Map arguments = new HashMap (); arguments.put ("x-message-ttl", 5000); return new Queue ("queueTest2", true,false, false, arguments);} / / switch @ Bean public TopicExchange exchangeTest () {/ / can pass exchange name, whether persistence is supported, and whether return new TopicExchange ("exchangeTest", true,false) can be deleted automatically } @ Bean public Binding bindQueueTest1AndExchange () {return BindingBuilder.bind (queueTest1 ()) .to (exchangeTest ()) .with ("phone.routing.*");} @ Bean public Binding bindQueueTest2AndExchange () {return BindingBuilder.bind (queueTest2 ()) .to (exchangeTest ()) .with ("web.routing.*");}}
4. Producer
Import org.springframework.amqp.core.Message;import org.springframework.amqp.rabbit.connection.CorrelationData;import org.springframework.amqp.rabbit.core.RabbitTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.Date;import java.util.UUID;/** producer, confirm with message * * / @ Servicepublic class PruSender implements RabbitTemplate.ReturnCallback {@ Autowired private RabbitTemplate rabbitTemplate / / routing_key, send the message to the appropriate queue public void sendMessage (String routing_key) {/ / send content String context = "Hello now" + new Date (); this.rabbitTemplate.setReturnCallback (this) / / send failed return this.rabbitTemplate.setConfirmCallback ((correlationData,ack,message)-> {/ / manually send message confirmation if (! ack) {System.out.println ("message failure" + message + correlationData.toString ());} else {System.out.println ("message sent successfully" + correlationData.toString ()) }}); CorrelationData correlationData = new CorrelationData (); correlationData.setId (UUID.randomUUID (). ToString ()); / / switch name, routingKey, content, message Id this.rabbitTemplate.convertAndSend ("exchangeTest", routing_key, context,correlationData) } @ Override public void returnedMessage (Message message, int I, String s, String S1, String S2) {System.out.println ("sender return success" + message.toString () + "= =" + I + "= =" + S1 + "= =" + S2);}}
5. Consumer
Import com.rabbitmq.client.Channel;import org.springframework.amqp.core.Message;import org.springframework.amqp.rabbit.annotation.RabbitHandler;import org.springframework.amqp.rabbit.annotation.RabbitListener;import org.springframework.stereotype.Service;import java.io.IOException / * * Consumer, confirm with message * * / @ Service@RabbitListener (queues = "queueTest") public class Receiver {/ / message content, channel, message attribute information @ RabbitHandler public void immediateProcess (String text,Channel channel,Message message) throws IOException {try {System.out.println ("Receiver" + text) / * manually confirm, notify mq that the change information has been successfully consumed, and delete the identity of the * / / message. False only confirms the receipt of the current message, and true confirms all messages obtained by consumer * / channel.basicAck (message.getMessageProperties (). GetDeliveryTag (), false). } catch (IOException e) {e.printStackTrace (); / * * failed to consume messages * whether the second parameter is applied to multiple messages, and whether the third parameter is newly added to the queue * * / channel.basicNack (message.getMessageProperties (). GetDeliveryTag (), false,true);}
Switch type:
Fanout: broadcast, delivering messages to all queues bound to the switch
Direct: directed to deliver messages to queues that match the specified routing key
Topic: wildcard character that delivers messages to queues that conform to routing pattern (routing patterns)
The above is the editor for you to share how to use RabbitMq simply, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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.