async def test_client_secret_credential_async(aad_credential, live_eventhub): try: from azure.identity.aio import ClientSecretCredential except ImportError: pytest.skip("No azure identity library") client_id, secret, tenant_id = aad_credential credential = ClientSecretCredential(client_id=client_id, client_secret=secret, tenant_id=tenant_id) client = EventHubClient(host=live_eventhub['hostname'], event_hub_path=live_eventhub['event_hub'], credential=credential, user_agent='customized information') sender = client.create_producer(partition_id='0') receiver = client.create_consumer(consumer_group="$default", partition_id='0', event_position=EventPosition("@latest")) async with receiver: received = await receiver.receive(timeout=3) assert len(received) == 0 async with sender: event = EventData(body='A single message') await sender.send(event) await asyncio.sleep(1) received = await receiver.receive(timeout=3) assert len(received) == 1 assert list(received[0].body)[0] == 'A single message'.encode('utf-8')
async def main(): client = EventHubClient(host=HOSTNAME, event_hub_path=EVENT_HUB, credential=EventHubSharedKeyCredential(USER, KEY), network_tracing=False) consumer = client.create_consumer(consumer_group="$default", partition_id="0", event_position=EVENT_POSITION) async with consumer: async for item in consumer: print(item)
async def test_long_running_receive_async(connection_str): parser = argparse.ArgumentParser() parser.add_argument("--duration", help="Duration in seconds of the test", type=int, default=30) parser.add_argument("--consumer", help="Consumer group name", default="$default") parser.add_argument("--partitions", help="Comma seperated partition IDs") parser.add_argument("--offset", help="Starting offset", default="-1") parser.add_argument("--conn-str", help="EventHub connection string", default=connection_str) parser.add_argument("--eventhub", help="Name of EventHub") parser.add_argument("--address", help="Address URI to the EventHub entity") parser.add_argument( "--sas-policy", help="Name of the shared access policy to authenticate with") parser.add_argument("--sas-key", help="Shared access key") loop = asyncio.get_event_loop() args, _ = parser.parse_known_args() if args.conn_str: client = EventHubClient.from_connection_string( args.conn_str, event_hub_path=args.eventhub, auth_timeout=240, network_tracing=False) elif args.address: client = EventHubClient(host=args.address, event_hub_path=args.eventhub, credential=EventHubSharedKeyCredential( args.sas_policy, args.sas_key), auth_timeout=240, network_tracing=False) else: try: import pytest pytest.skip("Must specify either '--conn-str' or '--address'") except ImportError: raise ValueError("Must specify either '--conn-str' or '--address'") if not args.partitions: partitions = await client.get_partition_ids() else: partitions = args.partitions.split(",") pumps = [] for pid in partitions: receiver = client.create_consumer(consumer_group="$default", partition_id=pid, event_position=EventPosition( args.offset), prefetch=300, loop=loop) pumps.append(pump(pid, receiver, args, args.duration)) await asyncio.gather(*pumps)
async def main(): if not HOSTNAME: raise ValueError("No EventHubs URL supplied.") client = EventHubClient(host=HOSTNAME, event_hub_path=EVENT_HUB, credential=EventHubSharedKeyCredential(USER, KEY), network_tracing=False) consumer = client.create_consumer(consumer_group="$default", partition_id="0", event_position=EVENT_POSITION) await iter_consumer(consumer)
class EventHubPartitionPump(PartitionPump): """ Pulls and messages from lease partition from eventhub and sends them to processor. """ def __init__(self, host, lease): PartitionPump.__init__(self, host, lease) self.eh_client = None self.partition_receiver = None self.partition_receive_handler = None self.running = None async def on_open_async(self): """ Eventhub Override for on_open_async. """ _opened_ok = False _retry_count = 0 while (not _opened_ok) and (_retry_count < 5): try: await self.open_clients_async() _opened_ok = True except Exception as err: # pylint: disable=broad-except _logger.warning( "%r,%r PartitionPumpWarning: Failure creating client or receiver, retrying: %r", self.host.guid, self.partition_context.partition_id, err) last_exception = err _retry_count += 1 if not _opened_ok: await self.processor.process_error_async(self.partition_context, last_exception) self.set_pump_status("OpenFailed") if self.pump_status == "Opening": loop = asyncio.get_event_loop() self.set_pump_status("Running") self.running = loop.create_task(self.partition_receiver.run()) if self.pump_status in ["OpenFailed", "Errored"]: self.set_pump_status("Closing") await self.clean_up_clients_async() self.set_pump_status("Closed") async def open_clients_async(self): """ Responsible for establishing connection to event hub client throws EventHubsException, IOException, InterruptedException, ExecutionException. """ await self.partition_context.get_initial_offset_async() # Create event hub client and receive handler and set options hostname = "{}.{}".format(self.host.eh_config.sb_name, self.host.eh_config.namespace_suffix) event_hub_path = self.host.eh_config.eh_name shared_key_cred = EventHubSharedKeyCredential( self.host.eh_config.policy, self.host.eh_config.sas_key) self.eh_client = EventHubClient( hostname, event_hub_path, shared_key_cred, network_tracing=self.host.eph_options.debug_trace, http_proxy=self.host.eph_options.http_proxy) self.partition_receive_handler = self.eh_client.create_consumer( partition_id=self.partition_context.partition_id, consumer_group=self.partition_context.consumer_group_name, event_position=EventPosition(self.partition_context.offset), prefetch=self.host.eph_options.prefetch_count, loop=self.loop) self.partition_receiver = PartitionReceiver(self) async def clean_up_clients_async(self): """ Resets the pump swallows all exceptions. """ if self.partition_receiver: if self.eh_client: await self.partition_receive_handler.close() self.partition_receiver = None self.partition_receive_handler = None self.eh_client = None async def on_closing_async(self, reason): """ Overides partition pump on closing. :param reason: The reason for the shutdown. :type reason: str """ self.partition_receiver.eh_partition_pump.set_pump_status("Errored") try: await self.running except TypeError: _logger.debug("No partition pump running.") except Exception as err: # pylint: disable=broad-except _logger.info("Error on closing partition pump: %r", err) await self.clean_up_clients_async()