Producer code unchanged, consumers:
package com.toov5.Consumer; import java.io.IOException; import java.util.concurrent.TimeoutException; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.DefaultConsumer; import com.rabbitmq.client.Envelope; import com.rabbitmq.client.AMQP.BasicProperties; import com.toov5.utils.MQConnectionUtils; public class Consumer { //队列名称 private static final String QUEUE_NAME = "test_queue"; public static voidmain (String [] args) throws IOException, TimeoutException { System.out.println ( "consumers start .........." ); // create a new connection Connection Connection = MQConnectionUtils.newConnection (); // Create Channel Channel Channel = connection.createChannel (); // the consumer associated queue channel.queueDeclare (queue_name, to false , to false , to false , null ); DefaultConsumer defaultConsumerr = new new DefaultConsumer (Channel) { // listens Get message @Override public void handleDelivery (String consumerTag, Envelope Envelope, BasicProperties Properties, byte [] body) throws IOException { String MSG = new new String (body, "UTF-. 8" ); System.out.println ( " Manufacturer consumer access message: "+ MSG); } }; // hand default mode setting automatic answer mode true: automatic-answer mode channel.basicConsume (queue_name, to false , defaultConsumerr); // fanse manual answer // // close the passage and connected // channel.close (); // Connection.close (); } }
Manual answer. At this point message queue has not been cleared
Producers make the following modifications on OK:
package com.toov5.Consumer; import java.io.IOException; import java.util.concurrent.TimeoutException; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.DefaultConsumer; import com.rabbitmq.client.Envelope; import com.rabbitmq.client.AMQP.BasicProperties; import com.toov5.utils.MQConnectionUtils; public class Consumer { //队列名称 private static final String QUEUE_NAME = "test_queue"; public static voidmain (String [] args) throws IOException, TimeoutException { System.out.println ( "consumers start .........." ); // create a new connection Connection Connection = MQConnectionUtils.newConnection (); // Create Channel Final Channel Channel = connection.createChannel (); // the consumer associated queue channel.queueDeclare (queue_name, to false , to false , to false , null ); DefaultConsumer defaultConsumerr = new new DefaultConsumer (Channel) { // listens Get message @Override public void handleDelivery (String consumerTag, Envelope Envelope, BasicProperties Properties, byte [] body) throws IOException { String MSG = new new String (body, "UTF-. 8" ); System.out.println ( " consumers get news producer: "+ msg); channel.basicAck (envelope.getDeliveryTag (), false ); // answered manually tell the message queue server consumer success } }; // in hand to set the default mode Auto answer mode true: Auto answer mode channel.basicConsume (queue_name, to false , defaultConsumerr); // fanse manual answer // // close the passage and connected // channel.close (); // Connection.close (); } }