-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ServiceBus] Topic Sender Implementation (#10748)
* topic sender implementation * update changelog * minor update on sample and samples/readme
- Loading branch information
1 parent
7ccb9a9
commit cce69b6
Showing
13 changed files
with
350 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
sdk/servicebus/azure-servicebus/samples/async_samples/send_topic_async.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#!/usr/bin/env python | ||
|
||
# -------------------------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for license information. | ||
# -------------------------------------------------------------------------------------------- | ||
|
||
""" | ||
Example to show sending message(s) to a Service Bus Topic asynchronously. | ||
""" | ||
|
||
# pylint: disable=C0111 | ||
|
||
import os | ||
import asyncio | ||
from azure.servicebus import Message | ||
from azure.servicebus.aio import ServiceBusClient | ||
|
||
CONNECTION_STR = os.environ['SERVICE_BUS_CONNECTION_STR'] | ||
TOPIC_NAME = os.environ["SERVICE_BUS_TOPIC_NAME"] | ||
|
||
|
||
async def send_single_message(sender): | ||
message = Message("DATA" * 64) | ||
await sender.send(message) | ||
|
||
|
||
async def send_batch_message(sender): | ||
batch_message = await sender.create_batch() | ||
while True: | ||
try: | ||
batch_message.add(Message("DATA" * 256)) | ||
except ValueError: | ||
# BatchMessage object reaches max_size. | ||
# New BatchMessage object can be created here to send more data. | ||
break | ||
await sender.send(batch_message) | ||
|
||
|
||
async def main(): | ||
servicebus_client = ServiceBusClient.from_connection_string(conn_str=CONNECTION_STR, logging_enable=True) | ||
|
||
async with servicebus_client: | ||
sender = servicebus_client.get_topic_sender(topic_name=TOPIC_NAME) | ||
async with sender: | ||
await send_single_message(sender) | ||
await send_batch_message(sender) | ||
|
||
print("Send message is done.") | ||
|
||
|
||
loop = asyncio.get_event_loop() | ||
loop.run_until_complete(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
sdk/servicebus/azure-servicebus/samples/sync_samples/send_topic.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#!/usr/bin/env python | ||
|
||
# -------------------------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for license information. | ||
# -------------------------------------------------------------------------------------------- | ||
|
||
""" | ||
Example to show sending message(s) to a Service Bus Topic. | ||
""" | ||
|
||
# pylint: disable=C0111 | ||
|
||
import os | ||
from azure.servicebus import ServiceBusClient, Message | ||
|
||
CONNECTION_STR = os.environ['SERVICE_BUS_CONNECTION_STR'] | ||
TOPIC_NAME = os.environ["SERVICE_BUS_TOPIC_NAME"] | ||
|
||
|
||
def send_single_message(sender): | ||
message = Message("DATA" * 64) | ||
sender.send(message) | ||
|
||
|
||
def send_batch_message(sender): | ||
batch_message = sender.create_batch() | ||
while True: | ||
try: | ||
batch_message.add(Message("DATA" * 256)) | ||
except ValueError: | ||
# BatchMessage object reaches max_size. | ||
# New BatchMessage object can be created here to send more data. | ||
break | ||
sender.send(batch_message) | ||
|
||
|
||
servicebus_client = ServiceBusClient.from_connection_string(conn_str=CONNECTION_STR, logging_enable=True) | ||
with servicebus_client: | ||
sender = servicebus_client.get_topic_sender(topic_name=TOPIC_NAME) | ||
with sender: | ||
send_single_message(sender) | ||
send_batch_message(sender) | ||
|
||
print("Send message is done.") |
63 changes: 63 additions & 0 deletions
63
sdk/servicebus/azure-servicebus/tests/async_tests/test_topic_async.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for | ||
# license information. | ||
#-------------------------------------------------------------------------- | ||
|
||
import asyncio | ||
import logging | ||
import sys | ||
import os | ||
import pytest | ||
import time | ||
from datetime import datetime, timedelta | ||
|
||
from devtools_testutils import AzureMgmtTestCase, RandomNameResourceGroupPreparer, CachedResourceGroupPreparer | ||
|
||
from azure.servicebus.aio import ServiceBusClient, ServiceBusSharedKeyCredential | ||
from azure.servicebus._common.message import Message | ||
from servicebus_preparer import ( | ||
ServiceBusNamespacePreparer, | ||
ServiceBusTopicPreparer, | ||
CachedServiceBusNamespacePreparer, | ||
CachedServiceBusTopicPreparer | ||
) | ||
from utilities import get_logger, print_message | ||
|
||
_logger = get_logger(logging.DEBUG) | ||
|
||
|
||
class ServiceBusTopicsAsyncTests(AzureMgmtTestCase): | ||
@pytest.mark.liveTest | ||
@pytest.mark.live_test_only | ||
@CachedResourceGroupPreparer(name_prefix='servicebustest') | ||
@CachedServiceBusNamespacePreparer(name_prefix='servicebustest') | ||
@CachedServiceBusTopicPreparer(name_prefix='servicebustest') | ||
async def test_topic_by_servicebus_client_conn_str_send_basic(self, servicebus_namespace_connection_string, servicebus_topic, **kwargs): | ||
|
||
async with ServiceBusClient.from_connection_string( | ||
servicebus_namespace_connection_string, | ||
logging_enable=False | ||
) as sb_client: | ||
async with sb_client.get_topic_sender(servicebus_topic.name) as sender: | ||
message = Message(b"Sample topic message") | ||
await sender.send(message) | ||
|
||
@pytest.mark.liveTest | ||
@pytest.mark.live_test_only | ||
@CachedResourceGroupPreparer(name_prefix='servicebustest') | ||
@CachedServiceBusNamespacePreparer(name_prefix='servicebustest') | ||
@CachedServiceBusTopicPreparer(name_prefix='servicebustest') | ||
async def test_topic_by_sas_token_credential_conn_str_send_basic(self, servicebus_namespace, servicebus_namespace_key_name, servicebus_namespace_primary_key, servicebus_topic, **kwargs): | ||
fully_qualified_namespace = servicebus_namespace.name + '.servicebus.windows.net' | ||
async with ServiceBusClient( | ||
fully_qualified_namespace=fully_qualified_namespace, | ||
credential=ServiceBusSharedKeyCredential( | ||
policy=servicebus_namespace_key_name, | ||
key=servicebus_namespace_primary_key | ||
), | ||
logging_enable=False | ||
) as sb_client: | ||
async with sb_client.get_topic_sender(servicebus_topic.name) as sender: | ||
message = Message(b"Sample topic message") | ||
await sender.send(message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.