RabbitMQ (two): RabbitMQ Advanced Features

RabbitMQ is a very popular messaging middleware, whether it is the Internet or SMEs are a large number of manufacturers use. As a qualified developer, it is necessary to know the relevant knowledge, RabbitMQ (a): RabbitMQ Quick Start has Started RabbitMQ, this article describes the advanced features of RabbitMQ.

Expiration time (TTL)

Time To Live, that is, survival time, maximum survival time is a message in the queue, in milliseconds. Redis friends should understand one can understand, the two like.

RabbitMQ TTL may be provided on the messages and queues.

RabbitMQ support setting message expiration time may be specified at the time of sending the message, each message expiration time may be different.

RabbitMQ queue support setting expiration time, from the message into the queue counted until it exceeds the configured timeout queue, then the message will become a dead letter, automatically cleared.

If used in two ways, the expiration time for that value whichever is the lesser.

Of course, you can not set the TTL, do not set indicates that the message does not expire; If set to 0, it means that unless the message can now be delivered directly to the consumer, otherwise the message will be discarded immediately.

Message acknowledgment

To ensure the message from the queue reliably reach the consumer, RabbitMQ provides a message confirmation mechanism. Consumers subscribe to the queue, they can specify parameters autoAck when autoAck to true, RabbitMQ automatic confirmation mode, RabbitMQ automatically sends out a message to confirm, and then deleted from the memory or hard disk, regardless of whether consumers really consumption to these messages. When autoAck is false, RabbitMQ will wait for confirmation signal consumers reply, delete messages only from memory or disk after the acknowledgment is received.

RabbitMQ message acknowledgment mechanism is the basis for the reliability of delivery of the message, as long as the set autoAck argument is false, consumers have enough time to process the message, do not worry about the process of processing messages in the consumer process hang message loss problems.

Endurance of

Message reliability is a major feature of RabbitMQ, then RabbitMQ is how to ensure the reliability of the message it? The answer is the message persistence. Persistence can prevent data loss in exceptional circumstances. RabbitMQ persistence divided into three parts: the exchanger persistence, persistent and persistent message queues.

Persistence may switch parameter is set to true when the statement by queue durable. If the switch is not set to persist, then after RabbitMQ service restarts, the relevant exchange meta data will be lost, but the message is not lost, but can not send messages to the switch a.

Persistent queues to ensure its own metadata is not lost due to an abnormal situation, but can not guarantee the interior of the stored messages are not lost. To ensure that messages are not lost, you need to set it to persist. Queue persistence parameter can be set to true when the statement by queue durable.

Set up queues and persistent messages, RabbitMQ service after the restart, the message remains. If only persistent queue or message persistence, the message will disappear after the restart.

Of course, you can also set all of the messages are persistent, but doing so will affect the performance of RabbitMQ, because disk write speed is much slower than memory writes. For reliability is not so high persistent message may not be employed to improve overall process throughput. Fish and bear's paw can not have both, the key is to select and choose. In practice, according to the actual situation to make a tradeoff between reliability and throughput.

Dead Letter Queue

When a message in the queue into dead letter, he can be sent back to another switch, this switch becomes badmail exchanger, the exchanger is called a dead letter queue queue binding. .

Into dead letter message has the following situations:

  • The message is rejected. Or by calling basic.reject basic.nack and arranged requeue = false.
  • Message expiration
  • The maximum length of the queue

DLX is a normal switches, switches and general no difference, he can be specified in any of the above queue, the queue is actually a set of attributes. When there is a dead message queue, RabbitMQ automatically resend the message to a switch provided, in turn, is routed to another queue, we can monitor the messages in the queue corresponding processing.

Dead letter queue settings:

  • Exchange and dead letter queue queue, then bind

    • Exchange:dlx.exchange
    • Queue:dlx.queue
    • RoutingKey:#
  • Then switch the normal statement, queues, bind, but we need to add a parameter in the queue: arguments.put ( "x-dead-letter-exchange", "dlx.exchange")

Dead Letter Queue What's the use?

When an exception occurs, the message can not be normal consumer spending, it was added to the dead-letter queue. Subsequent program can analyze the contents of abnormality had occurred in the dead letter queue, and further improve and optimize system.

Delay queue

General queue, the message will be queued once consumer spending immediately. After the delay queue is to get the message queue will be delayed consumer spending, lingering objects stored in the queue is delayed news, "the message latency" refers to the future when the message is sent, waiting for a specific time, consumers can get this message to consume.

Delay queue to be delayed for a scene work. The most common usage scenarios: Taobao day or cat we have used, the user is usually the next one after 30 minutes of time for payment, if the payment is not successful within these 30 minutes, then the order will automatically be canceled. In addition to delay consumption, typical application scenarios as well as the delay queue delay and try again. For example, consumers fail to consume messages from a queue inside, can be retried after a delay.

Guess you like

Origin www.cnblogs.com/sgh1023/p/11221364.html