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
New Queueingconsumer (channel); Boolean 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
1 Packagecom.zf.rabbitmq03;2 3 Importjava.io.IOException;4 5 ImportCom.rabbitmq.client.Channel;6 Importcom.rabbitmq.client.Connection;7 Importcom.rabbitmq.client.ConnectionFactory;8 9 /**Ten * Send Message One * @authorZhoufeng A * - */ - Public classSender03 { the - Public Static voidMain (string[] args)throwsIOException { - - +ConnectionFactory CONNFAC =Newconnectionfactory (); - + //Rabbitmq-server installed on the machine, so directly with 127.0.0.1 AConnfac.sethost ("127.0.0.1"); at - //Create a connection -Connection conn =connfac.newconnection (); - - //Create a channel -Channel Channel =Conn.createchannel (); in - //define the queue name toString queuename = "QUEUE01" ; + - //defines the queue's properties for the channel, queuename the queue name theChannel.queuedeclare (QueueName,false,false,false,NULL) ; * $String msg = "Hello world!";Panax Notoginseng - //Send Message theChannel.basicpublish ("", QueueName,NULL, Msg.getbytes ()); + ASystem.out.println ("Send message[" + msg + "] to" + QueueName + "success!"); the + channel.close (); - conn.close (); $ $ } - -}
As with Sender01.java, there's no difference.
Recv03.java
1 Packagecom.zf.rabbitmq03;2 3 Importjava.io.IOException;4 5 ImportCom.rabbitmq.client.Channel;6 Importcom.rabbitmq.client.Connection;7 Importcom.rabbitmq.client.ConnectionFactory;8 Importcom.rabbitmq.client.ConsumerCancelledException;9 ImportCom.rabbitmq.client.QueueingConsumer;Ten ImportCom.rabbitmq.client.QueueingConsumer.Delivery; One Importcom.rabbitmq.client.ShutdownSignalException; A - /** - * Receive Messages the * @authorZhoufeng - * - */ - Public classRecv03 { + - Public Static voidMain (string[] args)throwsIOException, Shutdownsignalexception, Consumercancelledexception, interruptedexception { + AConnectionFactory CONNFAC =Newconnectionfactory (); at -Connfac.sethost ("127.0.0.1"); - -Connection conn =connfac.newconnection (); - -Channel Channel =Conn.createchannel (); in -String channelname = "Channel01"; to +Channel.queuedeclare (ChannelName,false,false,false,NULL) ; - the * //configure how to get messages $Queueingconsumer consumer =NewQueueingconsumer (channel);Panax Notoginseng - the //Cancel Autoack + BooleanAutoack =false ; A the Channel.basicconsume (ChannelName, autoack, consumer); + - //Loop Get Message $ while(true){ $ - //gets the message, and if there is no message, this step will always block -Delivery Delivery =consumer.nextdelivery (); the -String msg =NewString (Delivery.getbody ()); Wuyi the //the confirmation message has been received - Channel.basicack (Delivery.getenvelope (). Getdeliverytag () Wu,false); - AboutSystem.out.println ("Received message[" + msg + "] from" +channelname); $ } - - } - A}
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 true falsenull);
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
RABBITMQ Study Notes (iii) Confirmation of messages and fair dispatch of consumers