def __init__( self, amqp_url: str = "", exchange_name: str = "amq.topic", exchange_type: ExchangeType = ExchangeType.TOPIC, routing_key: str = "", reconnect_interval: float = 1.0, prefetch_count: int = 1, on_message: MessageHandlerType = None, loop: AbstractEventLoop = None, ) -> None: """ :param amqp_url: A URL defining the AMQP connection parameters. If no URL is specified then a default setting is used which is suitable for connecting to RabbitMQ on localhost on port 5672, with a default username and password of guest and guest and a default virtual host of /. :param exchange_name: The name of the exchange to publish messages to. :param exchange_type: The type of exchange to declare. Default is topic. :param routing-key: The routing key to use when binding the message queue to the exchange. :param reconnect_interval: The number of seconds between reconnection attempts. Defaults to 1.0. :param prefetch_count: This parameter sets the limit of how many unacknowledged messages can be outstanding at any time on the channel. :param on_message: a user function that will be called whenever a new message is received. The callback is expected to take two arguments which are a message payload (automatically decompressed and deserialized) and a IncomingMessage object which provides the handler function with access to message headers. :param loop: The event loop to run in. """ self.loop = loop or asyncio.get_event_loop() self.amqp_url = amqp_url if amqp_url else utils.build_amqp_url() self.exchange_name = exchange_name self.exchange_type = exchange_type self.routing_key = routing_key self.reconnect_interval = reconnect_interval self.prefetch_count = prefetch_count self.connection = None self.channel = None self.exchange = None self.queue = None self._consumer_tag = None # type: Optional[str] self._on_message_handler = on_message
def test_create_url_with_ssl_options(self): ssl_options = dict( ca_certs="/etc/ssl/cacert.pem", certfile="/etc/ssl/mycert.pem", cert_reqs=ssl.CERT_REQUIRED, keyfile="/etc/ssl/mykey.pem", ) url = utils.build_amqp_url(ssl_options=ssl_options) self.assertEqual( url, "amqps://*****:*****@127.0.0.1:5671//?ca_certs=/etc/ssl/cacert.pem&certfile=/etc/ssl/mycert.pem&cert_reqs=2&keyfile=/etc/ssl/mykey.pem", )
def __init__( self, amqp_url: str = "", exchange_name: str = "amq.topic", exchange_type: ExchangeType = ExchangeType.TOPIC, routing_key: str = "", reconnect_interval: float = 1.0, serialization: str = None, compression: str = None, loop: AbstractEventLoop = None, ) -> None: """ :param amqp_url: A URL defining the AMQP connection parameters. If no URL is specified then a default setting is used which is suitable for connecting to RabbitMQ on localhost on port 5672, with a default username and password of guest and guest and a default virtual host of /. :param exchange_name: The name of the exchange to publish messages to. :param exchange_type: The type of exchange to declare. Default is topic. :param routing-key: The default routing key to use when publishing a message. :param reconnect_interval: The number of seconds between reconnection attempts. Defaults to 1.0. :param serialization: The name of the default serialization strategy to use when publishing messages. This strategy will be applied if no serialization is explicitly specified when publishing a message. :param compression: An optional string specifying the compression strategy to use. It can be provided using the convenience name or the mime-type. This strategy will be applied if no compression is explicitly specified when publishing a message. :param loop: The event loop to run in. Defaults to the currently running event loop. """ self.loop = loop or asyncio.get_event_loop() self.amqp_url = amqp_url if amqp_url else utils.build_amqp_url() self.exchange_name = exchange_name self.exchange_type = exchange_type self.routing_key = routing_key self.serialization = serialization self.compression = compression self.reconnect_interval = reconnect_interval self.connection = None # type: Optional[Connection] self.channel = None # type: Optional[Channel] self.exchange = None # type: Optional[Exchange]
def test_create_url_with_env_settings(self): username = "******" password = "******" hostname = "rando.host" port = "15672" values = { "RABBITMQ_USER": username, "RABBITMQ_PASS": password, "RABBITMQ_HOST": hostname, "RABBITMQ_PORT": port, } with unittest.mock.patch.dict("os.environ", values=values, clear=True): url = utils.build_amqp_url() self.assertEqual( url, f"amqp://{username}:{password}@{hostname}:{port}//")
def __init__( self, amqp_url: str = "", exchange_name: str = "", exchange_type: ExchangeType = ExchangeType.DIRECT, service_name: str = "", reconnect_interval: float = 1.0, prefetch_count: int = 1, serialization: str = None, compression: str = None, dlx_name: str = "rpc.dlx", on_request: Optional[MessageHandlerType] = None, loop: AbstractEventLoop = None, ) -> None: """ :param amqp_url: A URL defining the AMQP connection parameters. If no URL is specified then a default setting is used which is suitable for connecting to RabbitMQ on localhost on port 5672, with a default username and password of guest and guest and a default virtual host of /. :param exchange_name: The name of the exchange to bind the service queue to. This defaults to an empty string which results in the default exchange being used. :param exchange_type: The type of exchange to declare. Default is direct. :param service_name: The name to give to this service's message queue. :param prefetch_count: This parameter sets the limit of how many unacknowledged messages can be outstanding at any time on the channel. :param serialization: The name of the default serialization strategy to use when sending messages. This strategy will be applied if no serialization is explicitly specified when sending a message. :param compression: An optional string specifying the compression strategy to use. It can be provided using the convenience name or the mime-type. This strategy will be applied if no compression is explicitly specified when sending a message. :param dlx_name: The name of a Dead Letter Exchange used by a service provider as the destination for sending unhandled messages. The same exchange name must be used by client and server as the client binds a queue to obtain unhandled messages. :param on_request: a user function that will be called whenever a new request message is received. The callback is expected to take two arguments which are a message payload (automatically decompressed and deserialized) and a IncomingMessage object which provides the handler function with access to message headers. The function is expected to return a response object that will be returned to the sender. :param loop: The event loop to run in. """ self.loop = loop or asyncio.get_event_loop() self.amqp_url = amqp_url if amqp_url else utils.build_amqp_url() self.exchange_name = exchange_name self.exchange_type = exchange_type self.service_name = service_name self.serialization = serialization self.compression = compression if on_request is None: raise Exception("A response handler must be provided") self._request_handler = on_request # type: MessageHandlerType self.reconnect_interval = reconnect_interval self.prefetch_count = prefetch_count self.connection = None # type: Optional[Connection] self.channel = None # type: Optional[Channel] self.exchange = None # type: Optional[Exchange] self.dlx_name = dlx_name self.queue = None # type: Optional[Queue] self._consumer_tag = None # type: Optional[str]
def __init__( self, amqp_url: str = "", exchange_name: str = "", exchange_type: ExchangeType = ExchangeType.DIRECT, service_name: str = "", reconnect_interval: float = 1.0, prefetch_count: int = 1, serialization: str = None, compression: str = None, dlx_name: str = "rpc.dlx", loop: AbstractEventLoop = None, ) -> None: """ :param amqp_url: A URL defining the AMQP connection parameters. If no URL is specified then a default setting is used which is suitable for connecting to RabbitMQ on localhost on port 5672, with a default username and password of guest and guest and a default virtual host of /. :param exchange_name: The name of the exchange to bind the reply queue to. This defaults to an empty string which results in the default exchange being used. :param exchange_type: The type of exchange to declare. Default is direct. :param service_name: The routing-key to use when sending a request. This must match the service name queue created by the service provider. :param prefetch_count: This parameter sets the limit of how many unacknowledged messages can be outstanding at any time on the channel. :param serialization: The name of the default serialization strategy to use when sending messages. This strategy will be applied if no serialization is explicitly specified when sending a message. :param compression: An optional string specifying the compression strategy to use. It can be provided using the convenience name or the mime-type. This strategy will be applied if no compression is explicitly specified when sending a message. :param dlx_name: The name of a Dead Letter Exchange used by a service provider as the destination for sending unhandled messages. The same exchange name must be used by client and server as the client binds a queue to obtain unhandled messages. :param loop: The event loop to run in. """ self.loop = loop or asyncio.get_event_loop() self.amqp_url = amqp_url if amqp_url else utils.build_amqp_url() self.exchange_name = exchange_name self.exchange_type = exchange_type self.service_name = service_name self.serialization = serialization self.compression = compression self.reconnect_interval = reconnect_interval self.prefetch_count = prefetch_count self.connection = None # type: Optional[Connection] self.channel = None # type: Optional[Channel] self.exchange = None # type: Optional[Exchange] self.response_queue = None # type: Optional[Queue] self.dlx_name = dlx_name self.dlx_exchange = None # type: Optional[Exchange] self._consumer_tag = None # type: Optional[str] self._response_message_task = None self.futures = dict() # type: Dict[str, asyncio.Future]
def test_create_url_without_virtual_host(self): url = utils.build_amqp_url(user="******", password="******", host="my.host", port=5673) self.assertEqual(url, "amqp://*****:*****@my.host:5673//")
def test_create_url_with_virtual_host(self): url = utils.build_amqp_url(virtual_host="vhost") self.assertEqual(url, "amqp://*****:*****@127.0.0.1:5672/vhost")
def test_create_url_with_connection_attempts_and_heartbeat_interval(self): url = utils.build_amqp_url(connection_attempts=5, heartbeat_interval=2) self.assertEqual( url, "amqp://*****:*****@127.0.0.1:5672//?connection_attempts=5&heartbeat_interval=2", )
def test_create_url_with_heartbeat_interval(self): url = utils.build_amqp_url(heartbeat_interval=2) self.assertEqual( url, "amqp://*****:*****@127.0.0.1:5672//?heartbeat_interval=2")
def test_create_url_with_connection_attempts(self): url = utils.build_amqp_url(connection_attempts=5) self.assertEqual( url, "amqp://*****:*****@127.0.0.1:5672//?connection_attempts=5")
def test_create_url_without_args(self): url = utils.build_amqp_url() self.assertEqual(url, "amqp://*****:*****@127.0.0.1:5672//")