def open(self): """ Open the Sender using the supplied conneciton. If the handler has previously been redirected, the redirect context will be used to create a new handler before opening it. :param connection: The underlying client shared connection. :type: connection: ~uamqp.connection.Connection Example: .. literalinclude:: ../examples/test_examples_eventhub.py :start-after: [START eventhub_client_sender_open] :end-before: [END eventhub_client_sender_open] :language: python :dedent: 4 :caption: Open the Sender using the supplied conneciton. """ self.running = True if self.redirected: self.target = self.redirected.address self._handler = SendClient( self.target, auth=self.client.get_auth(), debug=self.client.debug, msg_timeout=self.timeout, error_policy=self.retry_policy, keep_alive_interval=self.keep_alive, client_name=self.name, properties=self.client.create_properties()) self._handler.open() while not self._handler.client_ready(): time.sleep(0.05)
def __init__(self, client, target, partition=None, keep_alive=None, auto_reconnect=True): """ Instantiate an EventHub event Sender handler. :param client: The parent EventHubClient. :type client: ~azure.eventhub.client.EventHubClient. :param target: The URI of the EventHub to send to. :type target: str """ self.client = client self.target = target self.partition = partition self.redirected = None self.error = None self.keep_alive = keep_alive self.auto_reconnect = auto_reconnect self.retry_policy = errors.ErrorPolicy(max_retries=3, on_error=_error_handler) self.name = "EHSender-{}".format(uuid.uuid4()) if partition: self.target += "/Partitions/" + partition self.name += "-partition{}".format(partition) self._handler = SendClient( self.target, auth=self.client.get_auth(), debug=self.client.debug, msg_timeout=Sender.TIMEOUT, error_policy=self.retry_policy, keep_alive_interval=self.keep_alive, client_name=self.name, properties=self.client.create_properties()) self._outcome = None self._condition = None
def _create_handler(self, auth): self._handler = SendClient(self._entity_uri, auth=auth, debug=self._config.logging_enable, properties=self._properties, error_policy=self._error_policy, client_name=self._name, encoding=self._config.encoding)
def __init__(self, client, target, partition=None, send_timeout=60, keep_alive=None, auto_reconnect=True): """ Instantiate an EventHubProducer. EventHubProducer should be instantiated by calling the `create_producer` method in EventHubClient. :param client: The parent EventHubClient. :type client: ~azure.eventhub.client.EventHubClient. :param target: The URI of the EventHub to send to. :type target: str :param partition: The specific partition ID to send to. Default is None, in which case the service will assign to all partitions using round-robin. :type partition: str :param send_timeout: The timeout in seconds for an individual event to be sent from the time that it is queued. Default value is 60 seconds. If set to 0, there will be no timeout. :type send_timeout: float :param keep_alive: The time interval in seconds between pinging the connection to keep it alive during periods of inactivity. The default value is None, i.e. no keep alive pings. :type keep_alive: float :param auto_reconnect: Whether to automatically reconnect the producer if a retryable error occurs. Default value is `True`. :type auto_reconnect: bool """ self.running = False self.client = client self.target = target self.partition = partition self.timeout = send_timeout self.redirected = None self.error = None self.keep_alive = keep_alive self.auto_reconnect = auto_reconnect self.retry_policy = errors.ErrorPolicy( max_retries=self.client.config.max_retries, on_error=_error_handler) self.reconnect_backoff = 1 self.name = "EHProducer-{}".format(uuid.uuid4()) self.unsent_events = None if partition: self.target += "/Partitions/" + partition self.name += "-partition{}".format(partition) self._handler = SendClient(self.target, auth=self.client.get_auth(), debug=self.client.config.network_tracing, msg_timeout=self.timeout, error_policy=self.retry_policy, keep_alive_interval=self.keep_alive, client_name=self.name, properties=self.client._create_properties( self.client.config.user_agent)) # pylint: disable=protected-access self._outcome = None self._condition = None
def _create_handler(self, auth): # type: (AMQPAuth) -> None self._handler = SendClient(self._entity_uri, auth=auth, debug=self._config.logging_enable, properties=self._properties, error_policy=self._error_policy, client_name=self._name, keep_alive_interval=self._config.keep_alive, encoding=self._config.encoding)
def _build_handler(self): auth = None if self.connection else authentication.SASTokenAuth.from_shared_access_key(**self.auth_config) self._handler = SendClient( self.endpoint, auth=auth, debug=self.debug, properties=self.properties, client_name=self.name, error_policy=self.error_policy, encoding=self.encoding, **self.handler_kwargs)
def _create_handler(self): self._handler = SendClient(self.target, auth=self.client.get_auth(), debug=self.client.config.network_tracing, msg_timeout=self.timeout, error_policy=self.retry_policy, keep_alive_interval=self.keep_alive, client_name=self.name, link_properties=self._link_properties, properties=self.client._create_properties( self.client.config.user_agent)) # pylint: disable=protected-access
def _create_handler(self, auth): self._handler = SendClient( self._target, auth=auth, debug=self._client._config.network_tracing, # pylint:disable=protected-access msg_timeout=self._timeout * 1000, idle_timeout=self._idle_timeout, error_policy=self._retry_policy, keep_alive_interval=self._keep_alive, client_name=self._name, link_properties=self._link_properties, properties=create_properties(self._client._config.user_agent)) # pylint: disable=protected-access
def _reconnect(self): # pylint: disable=protected-access self._handler.close() unsent_events = self._handler.pending_messages self._handler = SendClient(self.target, auth=self.client.get_auth(), debug=self.client.debug, msg_timeout=self.timeout, error_policy=self.retry_policy, keep_alive_interval=self.keep_alive, client_name=self.name, properties=self.client.create_properties()) try: self._handler.open() self._handler.queue_message(*unsent_events) self._handler.wait() return True except errors.TokenExpired as shutdown: log.info("Sender disconnected due to token expiry. Shutting down.") error = EventHubError(str(shutdown), shutdown) self.close(exception=error) raise error except (errors.LinkDetach, errors.ConnectionClose) as shutdown: if shutdown.action.retry and self.auto_reconnect: log.info("Sender detached. Attempting reconnect.") return False log.info("Sender reconnect failed. Shutting down.") error = EventHubError(str(shutdown), shutdown) self.close(exception=error) raise error except errors.MessageHandlerError as shutdown: if self.auto_reconnect: log.info("Sender detached. Attempting reconnect.") return False log.info("Sender reconnect failed. Shutting down.") error = EventHubError(str(shutdown), shutdown) self.close(exception=error) raise error except errors.AMQPConnectionError as shutdown: if str(shutdown).startswith("Unable to open authentication session" ) and self.auto_reconnect: log.info("Sender couldn't authenticate. Attempting reconnect.") return False log.info("Sender connection error (%r). Shutting down.", shutdown) error = EventHubError(str(shutdown)) self.close(exception=error) raise error except Exception as e: log.info("Unexpected error occurred (%r). Shutting down.", e) error = EventHubError("Sender Reconnect failed: {}".format(e)) self.close(exception=error) raise error
def open(self, connection): """ Open the Sender using the supplied conneciton. If the handler has previously been redirected, the redirect context will be used to create a new handler before opening it. :param connection: The underlying client shared connection. :type: connection: ~uamqp.connection.Connection """ if self.redirected: self._handler = SendClient(self.redirected.address, auth=None, debug=self.debug, msg_timeout=Sender.TIMEOUT) self._handler.open(connection)
def authenticate_client_by_jwt(): # Create the JWTTokenAuth object auth_uri = "<amqp endpoint uri for authentication>" # The AMQP endpoint URI for authentication. token_audience = "<token audience>" # The token audience field. auth = authentication.JWTTokenAuth(audience=token_audience, uri=auth_uri, get_token=get_token) # Instantiate the SendClient with the JWTTokenAuth object target = "<target amqp service endpoint>" # The target AMQP service endpoint. send_client = SendClient(target=target, auth=auth) # Send a message message = Message(b'data') send_client.send_message(message) send_client.close()
def __init__(self, client, target, partition=None): """ Instantiate an EventHub event Sender client. :param client: The parent EventHubClient. :type client: ~azure.eventhub.EventHubClient. :param target: The URI of the EventHub to send to. :type target: str """ self.partition = partition if partition: target += "/Partitions/" + partition self._handler = SendClient(target, auth=client.auth, debug=client.debug, msg_timeout=Sender.TIMEOUT) self._outcome = None self._condition = None
def reconnect(self): """If the Sender was disconnected from the service with a retryable error - attempt to reconnect.""" # pylint: disable=protected-access pending_states = (constants.MessageState.WaitingForSendAck, constants.MessageState.WaitingToBeSent) unsent_events = [e for e in self._handler._pending_messages if e.state in pending_states] self._handler.close() self._handler = SendClient( self.target, auth=self.client.get_auth(), debug=self.client.debug, msg_timeout=Sender.TIMEOUT, error_policy=self.retry_policy, keep_alive_interval=self.keep_alive, client_name=self.name, properties=self.client.create_properties()) self._handler.open() self._handler._pending_messages = unsent_events self._handler.wait()
def open(self): """ Open the Sender using the supplied conneciton. If the handler has previously been redirected, the redirect context will be used to create a new handler before opening it. :param connection: The underlying client shared connection. :type: connection: ~uamqp.connection.Connection """ if self.redirected: self.target = self.redirected.address self._handler = SendClient( self.target, auth=self.client.get_auth(), debug=self.client.debug, msg_timeout=self.timeout, error_policy=self.retry_policy, keep_alive_interval=self.keep_alive, client_name=self.name, properties=self.client.create_properties()) self._handler.open() while not self.has_started(): self._handler._connection.work() # pylint: disable=protected-access
def _open(self): """ Open the EventHubProducer using the supplied connection. If the handler has previously been redirected, the redirect context will be used to create a new handler before opening it. """ # pylint: disable=protected-access self._check_closed() if self.redirected: self.target = self.redirected.address self._handler = SendClient( self.target, auth=self.client.get_auth(), debug=self.client.config.network_tracing, msg_timeout=self.timeout, error_policy=self.retry_policy, keep_alive_interval=self.keep_alive, client_name=self.name, properties=self.client._create_properties( self.client.config.user_agent)) if not self.running: self._connect() self.running = True
def reconnect(self): """If the Sender was disconnected from the service with a retryable error - attempt to reconnect.""" # pylint: disable=protected-access self._handler.close() unsent_events = self._handler.pending_messages self._handler = SendClient(self.target, auth=self.client.get_auth(), debug=self.client.debug, msg_timeout=self.timeout, error_policy=self.retry_policy, keep_alive_interval=self.keep_alive, client_name=self.name, properties=self.client.create_properties()) try: self._handler.open() self._handler.queue_message(*unsent_events) self._handler.wait() except (errors.LinkDetach, errors.ConnectionClose) as shutdown: if shutdown.action.retry and self.auto_reconnect: self.reconnect() else: error = EventHubError(str(shutdown), shutdown) self.close(exception=error) raise error except errors.MessageHandlerError as shutdown: if self.auto_reconnect: self.reconnect() else: error = EventHubError(str(shutdown), shutdown) self.close(exception=error) raise error except Exception as e: error = EventHubError("Sender Reconnect failed: {}".format(e)) self.close(exception=error) raise error
def _build_connection(self, is_reconnect=False): """ :param is_reconnect: True - trying to reconnect after fail to connect or a connection is lost. False - the 1st time to connect :return: True - connected. False - not connected """ # pylint: disable=protected-access if is_reconnect: self._handler.close() self._handler = SendClient( self.target, auth=self.client.get_auth(), debug=self.client.config.network_tracing, msg_timeout=self.timeout, error_policy=self.retry_policy, keep_alive_interval=self.keep_alive, client_name=self.name, properties=self.client._create_properties( self.client.config.user_agent)) try: self._handler.open() while not self._handler.client_ready(): time.sleep(0.05) return True except errors.AuthenticationException as shutdown: if is_reconnect: log.info( "EventHubProducer couldn't authenticate. Shutting down. (%r)", shutdown) error = AuthenticationError(str(shutdown), shutdown) self.close(exception=error) raise error else: log.info( "EventHubProducer couldn't authenticate. Attempting reconnect." ) return False except (errors.LinkDetach, errors.ConnectionClose) as shutdown: if shutdown.action.retry: log.info("EventHubProducer detached. Attempting reconnect.") return False else: log.info("EventHubProducer detached. Shutting down.") error = ConnectError(str(shutdown), shutdown) self.close(exception=error) raise error except errors.MessageHandlerError as shutdown: if is_reconnect: log.info("EventHubProducer detached. Shutting down.") error = ConnectError(str(shutdown), shutdown) self.close(exception=error) raise error else: log.info("EventHubProducer detached. Attempting reconnect.") return False except errors.AMQPConnectionError as shutdown: if is_reconnect: log.info( "EventHubProducer connection error (%r). Shutting down.", shutdown) error = AuthenticationError(str(shutdown), shutdown) self.close(exception=error) raise error else: log.info( "EventHubProducer couldn't authenticate. Attempting reconnect." ) return False except compat.TimeoutException as shutdown: if is_reconnect: log.info( "EventHubProducer authentication timed out. Shutting down." ) error = AuthenticationError(str(shutdown), shutdown) self.close(exception=error) raise error else: log.info( "EventHubProducer authentication timed out. Attempting reconnect." ) return False except Exception as e: log.info( "Unexpected error occurred when building connection (%r). Shutting down.", e) error = EventHubError( "Unexpected error occurred when building connection", e) self.close(exception=error) raise error