Skip to content

Commit a2e5993

Browse files
committed
use application data
Signed-off-by: Gabriele Santomaggio <[email protected]>
1 parent 3dd6f51 commit a2e5993

File tree

4 files changed

+49
-31
lines changed

4 files changed

+49
-31
lines changed

examples/getting_started/getting_started.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def __init__(self):
2424
self._count = 0
2525

2626
def on_amqp_message(self, event: Event):
27-
print("received message: " + str(event.message.body))
27+
print("received message: {} ".format(''.join(map(chr, event.message.body))))
2828

2929
# accepting
3030
self.delivery_context.accept(event)
@@ -79,7 +79,6 @@ def create_connection(environment: Environment) -> Connection:
7979

8080

8181
def main() -> None:
82-
8382
exchange_name = "test-exchange"
8483
queue_name = "example-queue"
8584
routing_key = "routing-key"
@@ -123,7 +122,7 @@ def main() -> None:
123122
# publish 10 messages
124123
for i in range(MESSAGES_TO_PUBLISH):
125124
print("publishing")
126-
status = publisher.publish(Message(body="test"))
125+
status = publisher.publish(Message(body=str.encode("test message {} ".format(i))))
127126
if status.remote_state == OutcomeState.ACCEPTED:
128127
print("message accepted")
129128
elif status.remote_state == OutcomeState.RELEASED:

rabbitmq_amqp_python_client/qpid/proton/_exceptions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ class MessageException(ProtonException):
5858

5959
pass
6060

61+
class ArgumentOutOfRangeException(MessageException):
62+
"""
63+
An exception class raised when an argument is out of range.
64+
"""
65+
66+
pass
67+
6168

6269
class DataException(ProtonException):
6370
"""

rabbitmq_amqp_python_client/qpid/proton/_message.py

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
from ._common import millis2secs, secs2millis
8585
from ._data import AnnotationDict, Data, char, symbol, ulong
8686
from ._endpoints import Link
87-
from ._exceptions import EXCEPTIONS, MessageException
87+
from ._exceptions import EXCEPTIONS, MessageException, ArgumentOutOfRangeException
8888

8989
if TYPE_CHECKING:
9090
from proton._data import Described, PythonAMQPData
@@ -110,17 +110,26 @@ class Message(object):
110110
""" Default AMQP message priority"""
111111

112112
def __init__(
113-
self,
114-
body: Union[
115-
bytes, str, dict, list, int, float, "UUID", "Described", None
116-
] = None,
117-
**kwargs
113+
self,
114+
body: Union[
115+
bytes, dict, None
116+
] = None,
117+
**kwargs
118118
) -> None:
119+
# validate the types
120+
121+
if not isinstance(body, (bytes, dict, type(None))):
122+
raise ArgumentOutOfRangeException(
123+
"Message body must be of type bytes, dict or None"
124+
)
125+
119126
self._msg = pn_message()
120127
self.instructions = None
121128
self.annotations = None
122129
self.properties = None
123130
self.body = body
131+
self.inferred = True
132+
124133
for k, v in kwargs.items():
125134
getattr(self, k) # Raise exception if it's not a valid attribute.
126135
setattr(self, k, v)
@@ -236,7 +245,8 @@ def inferred(self) -> bool:
236245
237246
:raise: :exc:`MessageException` if there is any Proton error when using the setter.
238247
"""
239-
return pn_message_is_inferred(self._msg)
248+
x = pn_message_is_inferred(self._msg)
249+
return x
240250

241251
@inferred.setter
242252
def inferred(self, value: bool) -> None:
@@ -503,7 +513,7 @@ def instructions(self) -> Optional[AnnotationDict]:
503513

504514
@instructions.setter
505515
def instructions(
506-
self, instructions: Optional[Dict[Union[str, int], "PythonAMQPData"]]
516+
self, instructions: Optional[Dict[Union[str, int], "PythonAMQPData"]]
507517
) -> None:
508518
if isinstance(instructions, dict):
509519
self.instruction_dict = AnnotationDict(instructions, raise_on_error=False)
@@ -526,7 +536,7 @@ def annotations(self) -> Optional[AnnotationDict]:
526536

527537
@annotations.setter
528538
def annotations(
529-
self, annotations: Optional[Dict[Union[str, int], "PythonAMQPData"]]
539+
self, annotations: Optional[Dict[Union[str, int], "PythonAMQPData"]]
530540
) -> None:
531541
if isinstance(annotations, dict):
532542
self.annotation_dict = AnnotationDict(annotations, raise_on_error=False)
@@ -593,7 +603,8 @@ def send(self, sender: "Sender", tag: Optional[str] = None) -> "Delivery":
593603
return dlv
594604

595605
@overload
596-
def recv(self, link: "Sender") -> None: ...
606+
def recv(self, link: "Sender") -> None:
607+
...
597608

598609
def recv(self, link: "Receiver") -> Optional["Delivery"]:
599610
"""
@@ -624,24 +635,24 @@ def recv(self, link: "Receiver") -> Optional["Delivery"]:
624635
def __repr__(self) -> str:
625636
props = []
626637
for attr in (
627-
"inferred",
628-
"address",
629-
"reply_to",
630-
"durable",
631-
"ttl",
632-
"priority",
633-
"first_acquirer",
634-
"delivery_count",
635-
"id",
636-
"correlation_id",
637-
"user_id",
638-
"group_id",
639-
"group_sequence",
640-
"reply_to_group_id",
641-
"instructions",
642-
"annotations",
643-
"properties",
644-
"body",
638+
"inferred",
639+
"address",
640+
"reply_to",
641+
"durable",
642+
"ttl",
643+
"priority",
644+
"first_acquirer",
645+
"delivery_count",
646+
"id",
647+
"correlation_id",
648+
"user_id",
649+
"group_id",
650+
"group_sequence",
651+
"reply_to_group_id",
652+
"instructions",
653+
"annotations",
654+
"properties",
655+
"body",
645656
):
646657
value = getattr(self, attr)
647658
if value:

rabbitmq_amqp_python_client/qpid/proton/_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ def send(
176176
:return: Delivery object for this message.
177177
"""
178178

179+
179180
delivery = self.link.send(msg)
180181
self.connection.wait(
181182
lambda: _is_settled(delivery),

0 commit comments

Comments
 (0)