Queue Persistence
In the previous example, the queues we used were temporary queues, and no queues were created before the service was restarted.
The persistence of queues is determined by the second parameter when the queue is defined (false for queues without persistence)
Channel.queuedeclare (QueueName, False, False,false,null);
If the persistence flag is set to True, it represents a persistent queue, which can also exist after the service is restarted. Because the service will store the persistent queue on the hard disk, when the service restarts, it will restate the previously persisted queue. Queues can be persisted, but if the messages inside are persisted it also depends on the persistent settings of the message. That is to say, if there is any message in the queue before the reboot, the original message will be there after the reboot, depending on how the sender set the message when sending the message.
message Persistence If you want to persist message persistence after restarting, you must set the message to be a persistent flag
Channel.basicpublish ("", QueueName, Messageproperties.persistent_text_plain, Msg.getbytes ());
Sample Code
Send end:
Import java.io.IOException;
Import Com.rabbitmq.client.Channel;
Import com.rabbitmq.client.Connection;
Import Com.rabbitmq.client.ConnectionFactory;
Import com.rabbitmq.client.MessageProperties;
/** * Message Persistence * @author MENGHH * * */public class Send04 {public static void main (string[] args) throws IOException {
ConnectionFactory factory = new ConnectionFactory ();
Rabbitmq-server installed in this machine, so directly with the 127.0.0.1 factory.sethost ("127.0.0.1");
Create a connection Connection conn = Factory.newconnection ();
Create a communication channel Channel Channel = Conn.createchannel ();
Define the queue name String queuename = "QUEUE01";
Defines the properties of the queue for channel, QueueName is the queue name Channel.queuedeclare (QueueName, True, false,false,null);
String msg = "Hello world!"; Send Message/** * Test condition: 1, under the premise of the message Queue Persistence 2, receive the message party set receive way for manual receive, do not accept message to confirm * Do not adopt message persistence, restart RABBITMQ service, message queue exists, message is not received * Adopt message persistence , after restarting the RABBITMQ service, Message Queuing exists, messages can still receive messages, stating that the message is persisted/Channel.basicpublish ("", QueueName, Messageproperties.persistent_ Text_plain, Msg.getbytes ());
Channel.basicpublish ("", QueueName, NULL, msg.getbytes ());
System.out.println ("Send message[" +msg+ "] to" +queuename+ "success!");
Close the channel channel.close ();
Close connection conn.close (); }
}
receiving end (same as previous code)
Import Com.rabbitmq.client.Channel;
Import com.rabbitmq.client.Connection;
Import Com.rabbitmq.client.ConnectionFactory;
Import Com.rabbitmq.client.QueueingConsumer;
Import Com.rabbitmq.client.QueueingConsumer.Delivery; /** * Message Persistence * @author MENGHH * * */public class Recv04 {public static void main (string[] args) throws exception{C
Onnectionfactory factory = new ConnectionFactory ();
Factory.sethost ("127.0.0.1");
Connection conn = Factory.newconnection ();
Channel Channel = Conn.createchannel ();
String queuename = "QUEUE01";
Channel.queuedeclare (QueueName, True, False, false, NULL);
The above section and sender the same//configuration good access to the message way queueingconsumer consumer = new Queueingconsumer (channel);
Channel.basicconsume (QueueName, False,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, has received Channel.basicack (Delivery.getenvelope (). Getdeliverytag (), false); System.out.println ("Received message[" +msg+ "] from" +queuename); }
}
}
Test Results:After running the program, the queue exists, the queue still exists after restarting RABBITMQ server
Message Persistence test method: Comment out the code in the consumer to receive the message (mentioned earlier), start the Send message program, restart the RABBITMQ server after the message can still be received