RABBITMQ Study Notes (iii) Confirmation of messages and fair dispatch of consumers

Source: Internet
Author: User
Tags rabbitmq

Starting from this section, sender is called producer, Recv is consumer


I. Confirmation of the message

In order to ensure that the message must be handled by the consumer, RABBITMQ provides the message confirmation function, that is, after the consumer finishes the task, gives the server a feedback, the server will delete the message, if the consumer timeout does not reward, then the server will resend the message to other consumers

The default is open, in the consumer side through the following way to open the message confirmation, the first autoack automatically confirm the shutdown, and so on after our task is completed, manually to confirm, similar to the JDBC Autocommit

Queueingconsumer consumer = new Queueingconsumer (channel);
Boolean autoack = false;
Channel.basicconsume ("Hello", Autoack, consumer);

In the previous example, Channel.basicconsume (ChannelName, true, consumer) was used; When a message is received, a message is automatically fed back to the server.
The following example tests the functionality of message confirmations.

Sender03.java

Package com.zf.rabbitmq03;

Import java.io.IOException;

Import Com.rabbitmq.client.Channel;
Import com.rabbitmq.client.Connection;
Import com.rabbitmq.client.ConnectionFactory;

/** *
 Send Message
 * @author Zhoufeng
 * *
 /Public
class Sender03 {public
	
	static void Main (string[] args) throws IOException {
		
		
		connectionfactory CONNFAC = new ConnectionFactory ();
		
		Rabbitmq-server installed in the machine, so directly with the 127.0.0.1
		connfac.sethost ("127.0.0.1");
		
		Create a connection
		Connection conn = Connfac.newconnection ();
		
		Create a channel for channels channel
		= Conn.createchannel ();
		
		Defines the queue name
		String queuename = "QUEUE01";
		
		Defines the queue's properties for the channel, QueueName is the queue name
		Channel.queuedeclare (QueueName, False, False, false, NULL);
		
		String msg = "Hello world!";
		
		Send Message
		channel.basicpublish ("", QueueName, NULL, msg.getbytes ());
		
		System.out.println ("Send message[" + msg + "] to" + QueueName + "success!");
		
		Channel.close (); 
		Conn.close (); 
		
	}

}


As with Sender01.java, there's no difference.

Recv03.java

Package com.zf.rabbitmq03;

Import java.io.IOException;
Import Com.rabbitmq.client.Channel;
Import com.rabbitmq.client.Connection;
Import Com.rabbitmq.client.ConnectionFactory;
Import com.rabbitmq.client.ConsumerCancelledException;
Import Com.rabbitmq.client.QueueingConsumer;
Import Com.rabbitmq.client.QueueingConsumer.Delivery;

Import com.rabbitmq.client.ShutdownSignalException; /** * Receive Message * @author Zhoufeng * */public class Recv03 {public static void main (string[] args) throws IOException, Shutdownsignalexception, Consumercancelledexception, interruptedexception {connectionfactory ConnFac = new Connectio
		
		Nfactory ();
		
		Connfac.sethost ("127.0.0.1");
		
		Connection conn = Connfac.newconnection ();
		
		Channel channel = Conn.createchannel ();
		
		String channelname = "Channel01";
		
		
		Channel.queuedeclare (ChannelName, False, False, false, NULL);
		

		Configure how to get messages queueingconsumer consumer = new Queueingconsumer (channel); Cancel Autoack Boolean autoack = false;
		
		Channel.basicconsume (ChannelName, autoack, consumer);
			
			Loop get Message while (TRUE) {//Get message, if no message, this step will always block Delivery Delivery = Consumer.nextdelivery ();  
			
			String msg = new String (Delivery.getbody ());
			
			Confirmation message that you have received Channel.basicack (Delivery.getenvelope (). Getdeliverytag (), false);
		System.out.println ("Received message[" + msg + "] from" + channelname);
 }
		
	}
	
}


Note: Once the autoack is closed, be sure to remember to confirm the message to the server after processing the message. Otherwise the server will always forward the message

If the above Channel.basicack (Delivery.getenvelope (). Getdeliverytag (), false) is commented out, Sender03.java only needs to run once, Recv03.java will receive a HelloWorld message each time it runs

Attention:

But this is not enough, if the rabbitmq-server suddenly hang out, then the message has not been read is still lost, so we can make the message persisted. You only need to set the persistence message when you define the queue, by doing the following:

Boolean durable = true;
Channel.queuedeclare (ChannelName, durable, false, false, NULL);
When this is set, the server receives the message and writes the message to the hard disk immediately, preventing the sudden server from hanging up and the data being lost. However, if the server has just received the message, has not yet been able to write to the hard disk, it hangs up, this still cannot avoid the loss of the message.


Second, fair dispatch

The previous example can be implemented to send a message with a message received

As you can see from the previous RECV01, a message must be processed before the next message is received. If there are many producers, then a consumer must be too busy to come. At this point, you can use multiple consumers to deal with the same channel message, and to distribute the task fairly to multiple consumers. Not part of the busy part is always free

The way to achieve fair scheduling is to have each consumer assign a task at the same time. by Channel.basicqos (1); you can set



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.