Skip to content

Commit 13eb032

Browse files
In ServiceBus receiver set max_message_count to 1 for receive_messages (Azure#14939)
* In ServiceBus receiver set max_message_count to 1 for receive_messages * Fix return type from merge conflict * Allow servicebus receive_messages' max_messages_count to be None * Set servicebus async receive_messages' max_message_count default to 1 * changelog, test and mypy update * revert the internal _receive signature Co-authored-by: Yunhao Ling <adam_ling@outlook.com>
1 parent 05c65a8 commit 13eb032

File tree

4 files changed

+11
-4
lines changed

4 files changed

+11
-4
lines changed

sdk/servicebus/azure-servicebus/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ now raise more concrete exception other than `MessageSettleFailed` and `ServiceB
5858
* `ServiceBusMessage.amqp_annotated_message` has had its type renamed from `AMQPMessage` to `AMQPAnnotatedMessage`
5959
* `AutoLockRenewer` `timeout` parameter is renamed to `max_lock_renew_duration`
6060
* Attempting to autorenew a non-renewable message, such as one received in `ReceiveAndDelete` mode, or configure auto-autorenewal on a `ReceiveAndDelete` receiver, will raise a `ValueError`.
61+
* The default value of parameter `max_message_count` on `ServiceBusReceiver.receive_messages` is now `1` instead of `None` and will raise ValueError if the given value is less than or equal to 0.
6162

6263
**BugFixes**
6364

sdk/servicebus/azure-servicebus/azure/servicebus/_servicebus_receiver.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,8 @@ def _from_connection_string(
482482
raise ValueError("Subscription name is missing for the topic. Please specify subscription_name.")
483483
return cls(**constructor_args)
484484

485-
def receive_messages(self, max_message_count=None, max_wait_time=None):
486-
# type: (int, float) -> List[ServiceBusReceivedMessage]
485+
def receive_messages(self, max_message_count=1, max_wait_time=None):
486+
# type: (Optional[int], Optional[float]) -> List[ServiceBusReceivedMessage]
487487
"""Receive a batch of messages at once.
488488
489489
This approach is optimal if you wish to process multiple messages simultaneously, or
@@ -499,6 +499,7 @@ def receive_messages(self, max_message_count=None, max_wait_time=None):
499499
500500
:param Optional[int] max_message_count: Maximum number of messages in the batch. Actual number
501501
returned will depend on prefetch_count and incoming stream rate.
502+
Setting to None will fully depend on the prefetch config. The default value is 1.
502503
:param Optional[float] max_wait_time: Maximum time to wait in seconds for the first message to arrive.
503504
If no messages arrive, and no timeout is specified, this call will not return
504505
until the connection is closed. If specified, an no messages arrive within the
@@ -518,6 +519,8 @@ def receive_messages(self, max_message_count=None, max_wait_time=None):
518519
"""
519520
if max_wait_time is not None and max_wait_time <= 0:
520521
raise ValueError("The max_wait_time must be greater than 0.")
522+
if max_message_count is not None and max_message_count <= 0:
523+
raise ValueError("The max_message_count must be greater than 0")
521524
self._check_live()
522525
messages = self._do_retryable_operation(
523526
self._receive,

sdk/servicebus/azure-servicebus/azure/servicebus/aio/_servicebus_receiver_async.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ def _from_connection_string(
481481

482482
async def receive_messages(
483483
self,
484-
max_message_count: Optional[int] = None,
484+
max_message_count: Optional[int] = 1,
485485
max_wait_time: Optional[float] = None
486486
) -> List[ServiceBusReceivedMessage]:
487487
"""Receive a batch of messages at once.
@@ -499,6 +499,7 @@ async def receive_messages(
499499
500500
:param Optional[int] max_message_count: Maximum number of messages in the batch. Actual number
501501
returned will depend on prefetch_count size and incoming stream rate.
502+
Setting to None will fully depend on the prefetch config. The default value is 1.
502503
:param Optional[float] max_wait_time: Maximum time to wait in seconds for the first message to arrive.
503504
If no messages arrive, and no timeout is specified, this call will not return
504505
until the connection is closed. If specified, and no messages arrive within the
@@ -517,6 +518,8 @@ async def receive_messages(
517518
"""
518519
if max_wait_time is not None and max_wait_time <= 0:
519520
raise ValueError("The max_wait_time must be greater than 0.")
521+
if max_message_count is not None and max_message_count <= 0:
522+
raise ValueError("The max_message_count must be greater than 0")
520523
self._check_live()
521524
messages = await self._do_retryable_operation(
522525
self._receive,

sdk/servicebus/azure-servicebus/tests/test_sessions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,7 @@ def test_session_schedule_multiple_messages(self, servicebus_namespace_connectio
852852
count = 0
853853
while len(messages) < 2 and count < 12:
854854
receiver.session.renew_lock(timeout=None)
855-
messages = receiver.receive_messages(max_wait_time=15)
855+
messages.extend(receiver.receive_messages(max_wait_time=15))
856856
time.sleep(5)
857857
count += 1
858858

0 commit comments

Comments
 (0)