RabbitMQ (ii) queue and message persistence

Source: Internet
Author: User

When multiple consumers receive messages at the same time and each consumer needs to do other things while receiving the messages, it will take a long time, and some exceptions may occur during this process, for example, when half of a message is received, a consumer goes down. At this time, the message receiving confirmation mechanism is used to allow other consumers to re-execute the tasks that were not completed by the consumer just now. In addition, by default, the created message queue and the messages stored in the queue are non-persistent. That is to say, when RabbitMQ is down or restarted, the created message queues and messages are not saved. To solve this problem and ensure the reliability of message transmission, we can use the Message Queue persistence mechanism provided by RabbitMQ.

 

 

Producer:

1 import com. rabbitmq. client. ConnectionFactory;
2 import com. rabbitmq. client. Connection;
3 import com. rabbitmq. client. Channel;
4 import com. rabbitmq. client. MessageProperties;
5 public class ClientSend1 {
6 public static final String queue_name = "my_queue ";
7 public static final boolean durable = true; // Message Queue persistence
8 public static void main (String [] args)
9 throws java. io. IOException {
10 ConnectionFactory factory = new ConnectionFactory (); // create a connection factory
11 factory. setHost ("localhost ");
12 factory. setVirtualHost ("my_mq ");
13 factory. setUsername ("zhxia ");
14 factory. setPassword ("123456 ");
15 Connection connection = factory. newConnection (); // create a Connection
16 Channel channel = connection. createChannel (); // create a Channel
17 channel. queueDeclare (queue_name, durable, false, false, null); // declares the message queue and is persistent
18 String message = "Hello world" + Math. random ();
19 // After the queue is set to persistent, you also need to set the message to persistent. MessageProperties. PERSISTENT_TEXT_PLAIN
20 channel. basicPublish ("", queue_name, MessageProperties. PERSISTENT_TEXT_PLAIN, message. getBytes ());
21 System. out. println ("Send message:" + message );
22 channel. close ();
23 connection. close ();
24}
25
26}

 

Note:

Row 17 and row 20 must be set at the same time, that is, after the queue is set to persistent, you also need to set the sent message to persistent to ensure that the queue and message always exist.

Consumer:

1 import com. rabbitmq. client. ConnectionFactory;
2 import com. rabbitmq. client. Connection;
3 import com. rabbitmq. client. Channel;
4 import com. rabbitmq. client. QueueingConsumer;
5 public class ClientReceive1 {
6 public static final String queue_name = "my_queue ";
7 public static final boolean autoAck = false;
8 public static final boolean durable = true;
9 public static void main (String [] args)
10 throws java. io. IOException, java. lang. InterruptedException {
11 ConnectionFactory factory = new ConnectionFactory ();
12 factory. setHost ("localhost ");
13 factory. setVirtualHost ("my_mq ");
14 factory. setUsername ("zhxia ");
15 factory. setPassword ("123456 ");
16 Connection connection = factory. newConnection ();
17 Channel channel = connection. createChannel ();
18 channel. queueDeclare (queue_name, durable, false, false, null );
19 System. out. println ("Wait for message ");
20 channel. basicQos (1); // message distribution processing
21 QueueingConsumer consumer = new QueueingConsumer (channel );
22 channel. basicConsume (queue_name, autoAck, consumer );
23 while (true ){
24 Thread. sleep (500 );
25 QueueingConsumer. Delivery deliver = consumer. nextDelivery ();
26 String message = new String (deliver. getBody ());
27 System. out. println ("Message received ed:" + message );
28 channel. basicAck (deliver. getEnvelope (). getDeliveryTag (), false );
29}
30}
31}

Note:

Row 22: sets the RabbitMQ Scheduling Method for sending and distributing messages, that is, to tell RabbitMQ to process only one message for the consumer each time, that is, after the consumer completes processing and has confirmed the message just processed, to prevent consumers from being too busy. As shown in:

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.