RabbitBroker
faststream.rabbit.broker.RabbitBroker #
RabbitBroker(
url="amqp://guest:guest@localhost:5672/",
*,
host=None,
port=None,
virtualhost=None,
ssl_options=None,
client_properties=None,
timeout=None,
fail_fast=True,
reconnect_interval=5.0,
default_channel=None,
channel_number=None,
publisher_confirms=True,
on_return_raises=False,
max_consumers=None,
app_id=SERVICE_NAME,
graceful_timeout=None,
decoder=None,
parser=None,
dependencies=(),
middlewares=(),
security=None,
asyncapi_url=None,
protocol=None,
protocol_version="0.9.1",
description=None,
tags=None,
logger=EMPTY,
log_level=INFO,
log_fmt=None,
apply_types=True,
validate=True,
_get_dependant=None,
_call_decorators=(),
)
Bases: RabbitRegistrator
, RabbitLoggingBroker
A class to represent a RabbitMQ broker.
Initialize the RabbitBroker.
PARAMETER | DESCRIPTION |
---|---|
url | RabbitMQ destination location to connect. TYPE: |
host | Destination host. This option overrides |
port | Destination port. This option overrides |
virtualhost | RabbitMQ virtual host to use in the current broker connection. |
ssl_options | Extra ssl options to establish connection. TYPE: |
client_properties | Add custom client capability. TYPE: |
timeout | Connection establishment timeout. TYPE: |
fail_fast | Broker startup raises TYPE: |
reconnect_interval | Time to sleep between reconnection attempts. TYPE: |
default_channel | Default channel settings to use. |
channel_number | Specify the channel number explicitly. Deprecated in FastStream 0.5.39. |
publisher_confirms | If TYPE: |
on_return_raises | Raise an :class: TYPE: |
max_consumers | RabbitMQ channel |
app_id | Application name to mark outgoing messages by. |
graceful_timeout | Graceful shutdown timeout. Broker waits for all running subscribers completion before shut down. |
decoder | Custom decoder object. TYPE: |
parser | Custom parser object. TYPE: |
dependencies | Dependencies to apply to all broker subscribers. TYPE: |
middlewares | Middlewares to apply to all broker publishers/subscribers. TYPE: |
security | Security options to connect broker and generate AsyncAPI server security information. TYPE: |
asyncapi_url | AsyncAPI hardcoded server addresses. Use |
protocol | AsyncAPI server protocol. |
protocol_version | AsyncAPI server protocol version. |
description | AsyncAPI server description. |
tags | AsyncAPI server tags. |
logger | User-specified logger to pass into Context and log service messages. TYPE: |
log_level | Service messages log level. |
log_fmt | Default logger log format. |
apply_types | Whether to use FastDepends or not. TYPE: |
validate | Whether to cast types using Pydantic validation. TYPE: |
_get_dependant | Custom library dependant generator callback. |
_call_decorators | Any custom decorator to apply to wrapped functions. TYPE: |
Source code in faststream/rabbit/broker/broker.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
|
setup #
Prepare all Broker entities to startup.
add_middleware #
Append BrokerMiddleware to the end of middlewares list.
Current middleware will be used as a most inner of already existed ones.
Source code in faststream/broker/core/abc.py
subscriber #
subscriber(
queue,
exchange=None,
*,
consume_args=None,
dependencies=(),
parser=None,
decoder=None,
middlewares=(),
channel=None,
reply_config=None,
filter=default_filter,
retry=False,
no_ack=False,
no_reply=False,
title=None,
description=None,
include_in_schema=True,
)
Declares RabbitMQ subscriber object and binds it to the exchange.
You can use it as a handler decorator - @broker.subscriber(...)
. Or you can create a subscriber object to call it lately - broker.subscriber(...)
.
PARAMETER | DESCRIPTION |
---|---|
queue | RabbitMQ queue to listen. FastStream declares and binds TYPE: |
exchange | RabbitMQ exchange to bind queue to. Uses default exchange TYPE: |
consume_args | Extra consumer arguments to use in TYPE: |
channel | Channel to use for consuming messages. If not specified, a default channel will be used. |
reply_config | Extra options to use at replies publishing. TYPE: |
dependencies | Dependencies list ( TYPE: |
parser | Parser to map original IncomingMessage Msg to FastStream one. TYPE: |
decoder | Function to decode FastStream msg bytes body to python objects. TYPE: |
middlewares | Subscriber middlewares to wrap incoming message processing. TYPE: |
filter | Overload subscriber to consume various messages from the same source. TYPE: |
retry | Whether to |
no_ack | Whether to disable FastStream autoacknowledgement logic or not. TYPE: |
no_reply | Whether to disable FastStream RPC and Reply To auto responses or not. TYPE: |
title | AsyncAPI subscriber object title. |
description | AsyncAPI subscriber object description. Uses decorated docstring as default. |
include_in_schema | Whether to include operation in AsyncAPI schema or not. TYPE: |
Source code in faststream/rabbit/broker/registrator.py
publisher #
publisher(
queue="",
exchange=None,
*,
routing_key="",
mandatory=True,
immediate=False,
timeout=None,
persist=False,
reply_to=None,
priority=None,
middlewares=(),
title=None,
description=None,
schema=None,
include_in_schema=True,
headers=None,
content_type=None,
content_encoding=None,
expiration=None,
message_type=None,
user_id=None,
)
Creates long-living and AsyncAPI-documented publisher object.
You can use it as a handler decorator (handler should be decorated by @broker.subscriber(...)
too) - @broker.publisher(...)
. In such case publisher will publish your handler return value.
Or you can create a publisher object to call it lately - broker.publisher(...).publish(...)
.
PARAMETER | DESCRIPTION |
---|---|
queue | Default message routing key to publish with. TYPE: |
exchange | Target exchange to publish message to. TYPE: |
routing_key | Default message routing key to publish with. TYPE: |
mandatory | Client waits for confirmation that the message is placed to some queue. RabbitMQ returns message to client if there is no suitable queue. TYPE: |
immediate | Client expects that there is a consumer ready to take the message to work. RabbitMQ returns message to client if there is no suitable consumer. TYPE: |
timeout | Send confirmation time from RabbitMQ. TYPE: |
persist | Restore the message on RabbitMQ reboot. TYPE: |
reply_to | Reply message routing key to send with (always sending to default exchange). |
priority | The message priority (0 by default). |
middlewares | Publisher middlewares to wrap outgoing messages. TYPE: |
title | AsyncAPI publisher object title. |
description | AsyncAPI publisher object description. |
schema | AsyncAPI publishing message type. Should be any python-native object annotation or |
include_in_schema | Whether to include operation in AsyncAPI schema or not. TYPE: |
headers | Message headers to store meta-information. Can be overridden by TYPE: |
content_type | Message content-type header. Used by application, not core RabbitMQ. Will be set automatically if not specified. |
content_encoding | Message body content encoding, e.g. gzip. |
expiration | Message expiration (lifetime) in seconds (or datetime or timedelta). TYPE: |
message_type | Application-specific message type, e.g. orders.created. |
user_id | Publisher connection User ID, validated if set. |
Source code in faststream/rabbit/broker/registrator.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
|
include_router #
Source code in faststream/rabbit/broker/registrator.py
include_routers #
get_fmt #
setup_subscriber #
Setup the Subscriber to prepare it to starting.
Source code in faststream/broker/core/usecase.py
setup_publisher #
Setup the Publisher to prepare it to starting.
Source code in faststream/broker/core/usecase.py
close async
#
Closes the object.
Source code in faststream/broker/core/usecase.py
connect async
#
connect(
url=EMPTY,
*,
host=None,
port=None,
virtualhost=None,
ssl_options=None,
client_properties=None,
security=None,
timeout=None,
fail_fast=EMPTY,
reconnect_interval=EMPTY,
default_channel=None,
channel_number=EMPTY,
publisher_confirms=EMPTY,
on_return_raises=EMPTY,
)
Connect broker object to RabbitMQ.
To startup subscribers too you should use broker.start()
after/instead this method.
PARAMETER | DESCRIPTION |
---|---|
url | RabbitMQ destination location to connect. |
host | Destination host. This option overrides |
port | Destination port. This option overrides |
virtualhost | RabbitMQ virtual host to use in the current broker connection. |
ssl_options | Extra ssl options to establish connection. TYPE: |
client_properties | Add custom client capability. TYPE: |
security | Security options to connect broker and generate AsyncAPI server security information. TYPE: |
timeout | Connection establishement timeout. TYPE: |
fail_fast | Broker startup raises TYPE: |
reconnect_interval | Time to sleep between reconnection attempts. TYPE: |
default_channel | Default channel settings to use. |
channel_number | Specify the channel number explicit. |
publisher_confirms | if TYPE: |
on_return_raises | raise an :class: TYPE: |
Source code in faststream/rabbit/broker/broker.py
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
|
start async
#
Connect broker to RabbitMQ and startup all subscribers.
Source code in faststream/rabbit/broker/broker.py
publish async
#
publish(
message=None,
queue="",
exchange=None,
*,
routing_key="",
mandatory=True,
immediate=False,
timeout=None,
persist=False,
reply_to=None,
rpc=False,
rpc_timeout=30.0,
raise_timeout=False,
correlation_id=None,
headers=None,
content_type=None,
content_encoding=None,
expiration=None,
message_id=None,
timestamp=None,
message_type=None,
user_id=None,
priority=None,
)
Publish message directly.
This method allows you to publish message in not AsyncAPI-documented way. You can use it in another frameworks applications or to publish messages from time to time.
Please, use @broker.publisher(...)
or broker.publisher(...).publish(...)
instead in a regular way.
PARAMETER | DESCRIPTION |
---|---|
message | Message body to send. TYPE: |
queue | Message routing key to publish with. TYPE: |
exchange | Target exchange to publish message to. TYPE: |
routing_key | Message routing key to publish with. Overrides TYPE: |
mandatory | Client waits for confirmation that the message is placed to some queue. TYPE: |
immediate | Client expects that there is consumer ready to take the message to work. TYPE: |
timeout | Send confirmation time from RabbitMQ. TYPE: |
persist | Restore the message on RabbitMQ reboot. TYPE: |
reply_to | Reply message routing key to send with (always sending to default exchange). |
rpc | Whether to wait for reply in blocking mode. TYPE: |
rpc_timeout | RPC reply waiting time. |
raise_timeout | Whether to raise TYPE: |
correlation_id | Manual message correlation_id setter. |
headers | Message headers to store metainformation. TYPE: |
content_type | Message content-type header. |
content_encoding | Message body content encoding, e.g. gzip. |
expiration | Message expiration (lifetime) in seconds (or datetime or timedelta). TYPE: |
message_id | Arbitrary message id. Generated automatically if not presented. |
timestamp | Message publish timestamp. Generated automatically if not presented. TYPE: |
message_type | Application-specific message type, e.g. orders.created. |
user_id | Publisher connection User ID, validated if set. |
priority | The message priority (0 by default). |
Source code in faststream/rabbit/broker/broker.py
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 |
|
request async
#
request(
message=None,
queue="",
exchange=None,
*,
routing_key="",
mandatory=True,
immediate=False,
timeout=None,
persist=False,
correlation_id=None,
headers=None,
content_type=None,
content_encoding=None,
expiration=None,
message_id=None,
timestamp=None,
message_type=None,
user_id=None,
priority=None,
)
Make a synchronous request to RabbitMQ.
This method uses Direct Reply-To pattern to send a message and wait for a reply. It is a blocking call and will wait for a reply until timeout.
PARAMETER | DESCRIPTION |
---|---|
message | Message body to send. TYPE: |
queue | Message routing key to publish with. TYPE: |
exchange | Target exchange to publish message to. TYPE: |
routing_key | Message routing key to publish with. Overrides TYPE: |
mandatory | Client waits for confirmation that the message is placed to some queue. TYPE: |
immediate | Client expects that there is a consumer ready to take the message to work. TYPE: |
timeout | Send confirmation time from RabbitMQ. TYPE: |
persist | Restore the message on RabbitMQ reboot. TYPE: |
correlation_id | Manual message correlation_id setter. correlation_id is a useful option to trace messages. |
headers | Message headers to store metainformation. TYPE: |
content_type | Message content-type header. Used by application, not core RabbitMQ. |
content_encoding | Message body content encoding, e.g. gzip. |
expiration | Message expiration (lifetime) in seconds (or datetime or timedelta). TYPE: |
message_id | Arbitrary message id. Generated automatically if not presented. |
timestamp | Message publish timestamp. Generated automatically if not presented. TYPE: |
message_type | Application-specific message type, e.g. orders.created. |
user_id | Publisher connection User ID, validated if set. |
priority | The message priority (0 by default). |
Source code in faststream/rabbit/broker/broker.py
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 |
|
declare_queue async
#
Declares queue object in RabbitMQ.
Source code in faststream/rabbit/broker/broker.py
declare_exchange async
#
Declares exchange object in RabbitMQ.