5.2.2. Message Acquisition and Acceptance on an Unreliable Link
The default link between a receiver and the broker is a reliable link (technically known as a link with at-least-once reliability). This link uses a two-phase acquire and acknowlege behavior to ensure that the responsibility for a message is explicitly accepted by a consumer before the broker deletes it from the queue.
You can also request an unreliable link between the receiver and the broker. Over an unreliable link, messages are considered acknowledged and acquired as soon as the consumer fetches them from the queue. There is no acquired phase where a message will return to the queue if the receiver does not explicitly acknowledge it. The broker considers that the consumer has acknowledged the acquisition and deletes the message when the consumer fetches it, without waiting for an acquisition acknowledgement. This link has reduced reliability, but can result in increased throughput. It is useful when you can afford to lose messages in the event of consumer failure.
To request an unreliable link, specify
link: {reliability: unreliable}
in the address. For example, to create a receiver with an unreliable link to a queue named "browse-acquire-demo":
- Python
rxacquire = session.receiver("browse-acquire-demo; {link:{reliability: unreliable}")
The following program demonstrates the use and behavior of receivers using an unreliable link:
- Python
import sys from qpid.messaging import * def msgfetch(rx): try: msg = rx.fetch(timeout=1) except MessagingError, m: msg = m return msg linktype="" while linktype != "R" and linktype !="U": response = raw_input("Use (R)eliable or (U)nreliable link [R/U]?") linktype = response.upper() connection = Connection("localhost:5672") connection.open() try: session = connection.session() tx = session.sender("browse-acquire-demo;{create: always}") rxbrowse1 = session.receiver("browse-acquire-demo;{mode:browse}") rxbrowse2 = session.receiver("browse-acquire-demo;{mode:browse}") rxbrowse3 = session.receiver("browse-acquire-demo;{mode:browse}") if linktype == "R": rxacquire = session.receiver("browse-acquire-demo") else: rxacquire = session.receiver("browse-acquire-demo; {link:{reliability:unreliable}}") tx.send("Hello World") print "\nBrowser 1 saw message:" print msgfetch(rxbrowse1) print "Browser 1 then saw message:" print msgfetch(rxbrowse1) print "\nBrowser 2 saw message:" print msgfetch(rxbrowse2) print "Browser 2 then saw message:" print msgfetch(rxbrowse2) print "\nAcquired message:" print msgfetch(rxacquire) rxacquire.close() print "\nBrowser 3 saw message:" print msgfetch(rxbrowse3) except MessagingError, m: print m finally: connection.close() connection.open() try: session=connection.session() rxacquire2 = session.receiver("browse-acquire-demo") print "\nAcquirer 2 saw message:" print msgfetch(rxacquire2) except MessagingError, m: print m finally: session.acknowledge() connection.close()
When you select a reliable link for the demonstration, Acquirer 2 sees a redelivered message:
Acquirer 2 saw message: Message(redelivered=True, properties={'x-amqp-0-10.routing-key': u'browse-acquire-demo'}, content='Hello World')
Because the first acquirer did not acknowledge the message acquisition before disconnecting, the broker has returned the message to the queue for redelivery.
When you select an unreliable link for the demonstration, Acquirer 2 does not see any message:
Acquirer 2 saw message: None
On an unreliable link, even though the first acquirer did not explicitly accept responsibility for the message by acknowledging acquisition, the broker has deleted the message from the queue. That's the meaning of
unreliable
.
Releasing and Rejecting messages over an unreliable link
It is not possible to release or reject messages acquired over an unreliable link. Over an unreliable link messages are implicitly acknowledged when they are fetched.