|
|
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, please Reply
|
Previous:Exception message: "StrongTypingException: IsPrima...Next:Introduction to C# delegates (delegate, Action, Func, predicate)
|