By default, once the RABBITMQ is restarted, the Exchange and queue we define will disappear, and the messages in the queue naturally disappear without a trace. This is certainly unreasonable, after all, no one can guarantee that the RABBITMQ server will never restart, and will never fail. So how do you guarantee that the message will not be lost after the RABBITMQ server restarts? We're going to use RABBITMQ's persistence here.
There are three steps required to complete the persistence of a message:
Set Exchange's durable property to True
Set the durable property of the queue to True
Set the DeliveryMode of the message to 2.
The first and second steps are to set Exchange and queue to persistent, and the third is to persist the message. Three steps are indispensable, if you do not do the third step, messages will disappear after a reboot, but exchange and queue will not. Let's take a look at the specific code:
package com.jaeger.persistence;import java.io.ioexception;import java.util.concurrent.timeoutexception;import org.junit.test;import Com.rabbitmq.client.amqp.basicproperties;import com.rabbitmq.client.amqp.basicproperties.builder;import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import Com.rabbitmq.client.connectionfactory;public class producer {private static final String MY_EXCHANGE_NAME = "Myexchange";p rivate static final string my _routing_key = "Myroutingkey";p rivate static final string my_queue_name = "Myqueue";p rivate static final string direct = "DIRECT";p rivate static final string host = "172.19.64.28";p rivate static final string USER = "Jaeger";p rivate static final string password = "root";private static final int port = 5672; @Testpublic void Createexchangeandqueue () throws IOException, TimeoutException {ConnectionFactory Connectionfactory = new connectionfactory (); Connectionfactory.sethost (HOST); Connectionfactory.setusername (USER); Connectionfactory.setpassword (PASSWORD); Connectionfactory.setport (PORT); Connection connection = connectionfactory.newconnection (); Channel channel = connection.createchannel ();//Set the durable property of Exchange to Truechannel.exchangedeclare (my_exchange_name, direct, true);//Set the durable property of the QUEUE to Truechannel.queuedeclare (my_queue_name, True, false, false, null); Channel.queuebind (My_queue_name, my_exchange_name, my_ Routing_key); Channel.close (); Connection.close ();} @Testpublic void produce () throws ioexception, timeoutexception { connectionfactory connectionfactory = new ConnectionFactory (); Connectionfactory.sethost (HOST); Connectionfactory.setusername (USER); Connectionfactory.setpassword (PASSWORD); Connectionfactory.setport (PORT); Connection connection = connectionfactory.newconnection (); Channel channel = connection.createchannel (); string message = "hello world!"; /Set the DeliveryMode of the message to 2basicproperties props = new basicproperties.builder (). DeliveryMode (2). Build (); Channel.basicpublish (My_exchange_name, my_routing_key, props, message.getbytes ("Utf-8") ); System.out.println ("sent " + message + "'"); Channel.close (); Connection.close ();}}
This article is from the "Bronze Gong" blog, please be sure to keep this source http://jaeger.blog.51cto.com/11064196/1765289
RABBITMQ Introduction (vi)--persistence