Skip to content

Commit 0db39d3

Browse files
committed
Change the class name for the consumer
add documentation Signed-off-by: Gabriele Santomaggio <[email protected]>
1 parent 2983576 commit 0db39d3

File tree

6 files changed

+96
-24
lines changed

6 files changed

+96
-24
lines changed

examples/getting_started/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
AddressHelper,
66
BindingSpecification,
77
Connection,
8-
DeliveryConsumerHandler,
8+
AMQPMessagingHandler,
99
Event,
1010
ExchangeSpecification,
1111
Message,
1212
QuorumQueueSpecification,
1313
)
1414

1515

16-
class MyMessageHandler(DeliveryConsumerHandler):
16+
class MyMessageHandler(AMQPMessagingHandler):
1717

1818
def __init__(self):
1919
super().__init__()

rabbitmq_amqp_python_client/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
from .common import ExchangeType, QueueType
55
from .connection import Connection
66
from .consumer import Consumer
7-
from .delivery_consumer_handler import (
8-
DeliveryConsumerHandler,
7+
from .amqp_consumer_handler import (
8+
AMQPMessagingHandler,
99
)
1010
from .entities import (
1111
BindingSpecification,
@@ -51,5 +51,5 @@
5151
"symbol",
5252
"ExchangeType",
5353
"AddressHelper",
54-
"DeliveryConsumerHandler",
54+
"AMQPMessagingHandler",
5555
]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from .delivery_context import DeliveryContext
2+
from .qpid.proton.handlers import MessagingHandler
3+
4+
5+
'''
6+
AMQPMessagingHandler extends the QPID MessagingHandler.
7+
It is an helper to set the default values needed for manually accepting and settling messages.
8+
9+
self.delivery_context is an instance of DeliveryContext, which is used to accept, reject,
10+
requeue or requeue with annotations a message.
11+
12+
It is not mandatory to use this class, but it is a good practice to use it.
13+
'''
14+
class AMQPMessagingHandler(MessagingHandler): # type: ignore
15+
16+
def __init__(self, auto_accept: bool = False, auto_settle: bool = True):
17+
"""
18+
:param auto_accept: if True, the message is automatically accepted
19+
by default is false, so the user has to manually accept the message and decide with the
20+
different methods of the delivery_context what to do with the message
21+
"""
22+
super().__init__(auto_accept=auto_accept, auto_settle=auto_settle)
23+
self.delivery_context: DeliveryContext = DeliveryContext()

rabbitmq_amqp_python_client/delivery_consumer_handler.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

rabbitmq_amqp_python_client/delivery_context.py

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,54 @@
44
from .qpid.proton._delivery import Delivery
55
from .qpid.proton._events import Event
66

7+
'''
8+
DeliveryContext is a class that is used to accept, reject, requeue or requeue with annotations a message.
9+
It is an helper to set the default values needed for manually accepting and settling messages.
10+
'''
11+
712

813
class DeliveryContext:
14+
"""
15+
Accept the message (AMQP 1.0 <code>accepted</code> outcome).
16+
17+
This means the message has been processed and the broker can delete it.
18+
"""
919

1020
def accept(self, event: Event) -> None:
1121
dlv = event.delivery
1222
dlv.update(Delivery.ACCEPTED)
1323
dlv.settle()
1424

25+
"""
26+
Reject the message (AMQP 1.0 <code>rejected</code> outcome).
27+
This means the message cannot be processed because it is invalid, the broker can drop it
28+
or dead-letter it if it is configured.
29+
"""
30+
1531
def discard(self, event: Event) -> None:
1632
dlv = event.delivery
1733
dlv.update(Delivery.REJECTED)
1834
dlv.settle()
1935

36+
"""
37+
Discard the message with annotations to combine with the existing message annotations.
38+
This means the message cannot be processed because it is invalid, the broker can drop it
39+
or dead-letter it if it is configured.
40+
Application-specific annotation keys must start with the <code>x-opt-</code> prefix.
41+
Annotation keys the broker understands starts with <code>x-</code>, but not with <code>x-opt-
42+
43+
This maps to the AMQP 1.0
44+
modified{delivery-failed = true, undeliverable-here = true}</code> outcome.
45+
<param name="annotations"> annotations message annotations to combine with existing ones </param>
46+
<a
47+
href="https://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-messaging-v1.0-os.html#type-modified">AMQP
48+
1.0 <code>modified</code> outcome</a>
49+
50+
The annotations can be used only with Quorum queues, see https://www.rabbitmq.com/docs/amqp#modified-outcome
51+
"""
52+
2053
def discard_with_annotations(
21-
self, event: Event, annotations: Dict[str, "PythonAMQPData"]
54+
self, event: Event, annotations: Dict[str, "PythonAMQPData"]
2255
) -> None:
2356
dlv = event.delivery
2457
dlv.local.annotations = annotations
@@ -28,13 +61,39 @@ def discard_with_annotations(
2861
dlv.update(Delivery.MODIFIED)
2962
dlv.settle()
3063

64+
"""
65+
Requeue the message (AMQP 1.0 <code>released</code> outcome).
66+
This means the message has not been processed and the broker can requeue it and deliver it
67+
to the same or a different consumer.
68+
"""
69+
3170
def requeue(self, event: Event) -> None:
3271
dlv = event.delivery
3372
dlv.update(Delivery.RELEASED)
3473
dlv.settle()
3574

75+
"""
76+
Requeue the message with annotations to combine with the existing message annotations.
77+
78+
This means the message has not been processed and the broker can requeue it and deliver it
79+
to the same or a different consumer.
80+
Application-specific annotation keys must start with the <code>x-opt-</code> prefix.
81+
Annotation keys the broker understands starts with <code>x-</code>, but not with <code>x-opt-
82+
</code>.
83+
84+
This maps to the AMQP 1.0 <code>
85+
modified{delivery-failed = false, undeliverable-here = false}</code> outcome.
86+
87+
<param name="annotations"> annotations message annotations to combine with existing ones </param>
88+
<a
89+
href="https://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-messaging-v1.0-os.html#type-modified">AMQP
90+
1.0 <code>modified</code> outcome</a>
91+
92+
The annotations can be used only with Quorum queues, see https://www.rabbitmq.com/docs/amqp#modified-outcome
93+
"""
94+
3695
def requeue_with_annotations(
37-
self, event: Event, annotations: Dict[str, "PythonAMQPData"]
96+
self, event: Event, annotations: Dict[str, "PythonAMQPData"]
3897
) -> None:
3998
dlv = event.delivery
4099
dlv.local.annotations = annotations

tests/conftest.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from rabbitmq_amqp_python_client import (
44
AddressHelper,
55
Connection,
6-
DeliveryConsumerHandler,
6+
AMQPMessagingHandler,
77
Event,
88
symbol,
99
)
@@ -57,7 +57,7 @@ def __str__(self) -> str:
5757
return repr(self.msg)
5858

5959

60-
class MyMessageHandlerAccept(DeliveryConsumerHandler):
60+
class MyMessageHandlerAccept(AMQPMessagingHandler):
6161

6262
def __init__(self):
6363
super().__init__()
@@ -71,7 +71,7 @@ def on_message(self, event: Event):
7171
raise ConsumerTestException("consumed")
7272

7373

74-
class MyMessageHandlerNoack(DeliveryConsumerHandler):
74+
class MyMessageHandlerNoack(AMQPMessagingHandler):
7575

7676
def __init__(self):
7777
super().__init__(auto_settle=False)
@@ -86,7 +86,7 @@ def on_message(self, event: Event):
8686
raise ConsumerTestException("consumed")
8787

8888

89-
class MyMessageHandlerDiscard(DeliveryConsumerHandler):
89+
class MyMessageHandlerDiscard(AMQPMessagingHandler):
9090

9191
def __init__(self):
9292
super().__init__()
@@ -100,7 +100,7 @@ def on_message(self, event: Event):
100100
raise ConsumerTestException("consumed")
101101

102102

103-
class MyMessageHandlerDiscardWithAnnotations(DeliveryConsumerHandler):
103+
class MyMessageHandlerDiscardWithAnnotations(AMQPMessagingHandler):
104104

105105
def __init__(self):
106106
super().__init__()
@@ -116,7 +116,7 @@ def on_message(self, event: Event):
116116
raise ConsumerTestException("consumed")
117117

118118

119-
class MyMessageHandlerRequeue(DeliveryConsumerHandler):
119+
class MyMessageHandlerRequeue(AMQPMessagingHandler):
120120

121121
def __init__(self):
122122
super().__init__()
@@ -130,7 +130,7 @@ def on_message(self, event: Event):
130130
raise ConsumerTestException("consumed")
131131

132132

133-
class MyMessageHandlerRequeueWithAnnotations(DeliveryConsumerHandler):
133+
class MyMessageHandlerRequeueWithAnnotations(AMQPMessagingHandler):
134134

135135
def __init__(self):
136136
super().__init__()

0 commit comments

Comments
 (0)