This article is a mirror article of machine translation, please click here to jump to the original article.

View: 44647|Reply: 8

[WinForm] rabbitMQ Queue Queue Message Persistence [with source code]

[Copy link]
Posted on 4/9/2018 10:23:21 AM | | | |
By default, rabbitMQ queue messages are not persisted to the hard disk, which means that once the rabbitMQ service is restarted, the messages will be lost.

Persistence of queues

For example, the persistence of the queue is identifieddurableis set to true, which means it is a persistent queue, then after the service is restarted, it will also exist, because the service will store the persisted queue on the hard disk, and when the service is restarted, it will re-establish what was previously persisted queue. The queue can be persisted, but whether the messages inside are persistent depends on the persistence settings of the message. In other words, if there is no message sent in the queue before the restart, whether the original message still exists in the queue after the restart depends on the message settings that occurred when the message was sent.
If you want to keep messages persistent after a restart, you must set the identity that the message is persisted.

Set up queue persistence:




The fourth parameter of the method, autoDelete, is usually entered false. The documentation describes this parameter if true, which means that if the queue is no longer used (not subscribed), the server will delete it. During my testing, as long as all recipients of the connection change queue are disconnected, the queue is deleted, even if there are still unprocessed messages in it. RabbitMQ restarts will also remove them. If false is entered, the service will not delete the queue and the messages in the queue will exist if all the clients connected to it are disconnected. The sender can also put messages into the change queue when there is no client connection, and when the client comes up, it will get these messages. However, if the RabbitMQ service is restarted, the queue will be gone, and the messages in it will naturally disappear.

The third parameter is exclusive, and the documentation states that if true, then the connection of the queue is broken, then the queue is deleted, including the messages inside.

The second parameter, durable, is described in the documentation as saying that if true, it represents a persistent queue, which will also exist after the service restarts. Because the service will store the persistent queue on the hard disk, and when the service is restarted, it will reaffirm this queue. Of course, it must be when both autoDelete and exclusive are false. The queue can be persisted, but whether the messages inside are persistent depends on the persistence settings of the message. In other words, if there are still messages sent in the queue before the restart, whether the original message still exists in the queue after the restart depends on the sender's settings for the message when sending the message.


After we modify the code, we try to run it, and the error will be as follows:

Unhandled exception: RabbitMQ.Client.Exceptions.OperationInterruptedException: The AMQP operation was interrupted: AMQP close-reason, initiated by Peer, code=406, text="PRECONDITION_FAILED - inequivalent arg 'durable' for queue 'hello' in vhost 'myserver': received 'true' but current is 'false'", classId=50, methodId=10, cause=



Because we've defined an unpersisted queue called hello. RabbitMQ does not allow redefining existing queues with different parameter settings.

There are two solutions:

1: Redeclare a queue with a different name, such as my_queue
2: Delete the defined "hello" queue with the address of http://localhost:15672 and log in with the username and password. The default password and username for RabbitMQ are guest. Click on the "queue" column to see the queue list, click on the "hello" queue to expand the queue details. Pull the page to the end, there is an item "Delete", click on it, click the "Delete Queue" button, and you can delete the queue. Then when the code is run, a hello queue is created that supports persistence.



Persistence of messages

If you want to keep the message persistent after a restart, you must set the message to persist. The setting is when the sender sends it, which is relatively simple, and the code is as follows:


DeliveryMode defaults to 1, non-persistent, and setting to 2 means the message is persistent

After we modify the code, we try to only open the producer program to send messages, and then restart the rabbitMQ service, open the consumer again, and find that the message is not lost.

(End)

Attached is the C# source code:

Tourists, if you want to see the hidden content of this post, pleaseReply





Previous:Exception message: "StrongTypingException: IsPrima...
Next:Introduction to C# delegates (delegate, Action, Func, predicate)
Posted on 4/9/2018 1:17:51 PM |
Learn to learn
Posted on 6/25/2019 11:22:47 PM |
Learn to learn
Posted on 6/29/2019 9:36:23 AM |
Why didn't I see the demo, I must reply to see it
Posted on 7/9/2019 5:34:42 PM |
I want C# source code
Posted on 7/24/2019 2:21:51 PM |
It's good, it's exactly what I need
Posted on 4/11/2020 2:34:54 PM |
Learn it
Posted on 1/1/2022 2:45:24 PM |
1111111111111111
 Landlord| Posted on 5/3/2023 10:12:22 PM |
Parameters of the Queue of RabbitMQ and their meanings


/**
* Construct a new queue, given a name, durability flag, and auto-delete flag, and arguments.
* @param name the name of the queue - must not be null; set to "" to have the broker generate the name.
* @param durable true if we are declaring a durable queue (the queue will survive a server restart)
* @param exclusive true if we are declaring an exclusive queue (the queue will only be used by the declarer's
* connection)
* @param autoDelete true if the server should delete the queue when it is no longer in use
* @param arguments the arguments used to declare the queue
*/
public Queue(String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments) {
   Assert.notNull(name, "'name' cannot be null");
   this.name = name;
   this.actualName = StringUtils.hasText(name) ? name
         : (Base64UrlNamingStrategy.DEFAULT.generateName() + "_awaiting_declaration");
   this.durable = durable;
   this.exclusive = exclusive;
   this.autoDelete = autoDelete;
   this.arguments = arguments != null ? arguments : new HashMap<>();
}

Parameter introduction:
1. name: the name of the queue;
2. actualName: The real name of the queue, the name parameter is used by default, if the name is empty, one is generated according to the rules;
3. durable: whether it is persistent;
4. Exclusive: whether it is exclusive or exclusive;
5. autoDelete: whether to delete automatically;
6. Arguments: Other attribute parameters of the queue have the following options, see Arguments in Figure 2:
(1) x-message-ttl: the expiration time of the message, in milliseconds;
(2) x-expires: queue expiration time, how long the queue will be deleted if it is not accessed, unit: milliseconds;
(3) x-max-length: the maximum length of the queue, if it exceeds the maximum value, the message will be deleted from the queue header;
(4) x-max-length-bytes: the queue message content occupies the maximum space, limited by the memory size, and if it exceeds this threshold, the message will be deleted from the queue header;
(5) x-overflow: Set the queue overflow behavior. This determines what happens to the message when the maximum length of the queue is reached. Valid values are drop-head, reject-publish, or reject-publish-dlx. Quorum queue types only support drop-head;
(6) x-dead-letter-exchange: the name of the dead-letter exchange, and the messages that have expired or been deleted (due to the long queue length or the space exceeding the threshold) can be specified to be sent to the exchange;
(7) x-dead-letter-routing-key: The routing key of the dead-letter message, which will be used when the message is sent to the dead-letter exchanger, if not set, the original routing key value of the message will be used
(8) x-single-active-consumer: indicates whether the queue is a single active consumer, if true, only one consumer in the registered consumer group consumes messages, the others are ignored, and false when the message is distributed to all consumers in a loop (default false)
(9) x-max-priority: the maximum number of priorities to be supported by the queue; If not set, the queue will not support message priority;
(10) x-queue-mode(Lazy mode): Set the queue to delay mode, keep as many messages as possible on the disk to reduce RAM usage; If not set, the queue will keep a memory cache to deliver messages as quickly as possible;
(11) x-queue-master-locator: Set the master node information of the mirror queue in cluster mode.


Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com