RABBITMQ Introduction (vi)--persistence

Source: Internet
Author: User

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:

    1. Set Exchange's durable property to True

    2. Set the durable property of the queue to True

    3. 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

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.