spakky-rabbitmq¶
RabbitMQ 통합 — 이벤트 전송/수신
메인¶
Plugin initialization for RabbitMQ integration.
Registers event consumers, transports, and post-processors for automatic event handler registration in RabbitMQ-enabled applications.
initialize(app)
¶
Initialize the RabbitMQ plugin.
Registers event consumers, transports, and the post-processor for automatic event handler registration. This function is called automatically by the Spakky framework during plugin loading.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
SpakkyApplication
|
The Spakky application instance. |
required |
Source code in plugins/spakky-rabbitmq/src/spakky/plugins/rabbitmq/main.py
이벤트 Transport¶
RabbitMQ event transports for integration events.
Provides synchronous and asynchronous event transports that send integration events to RabbitMQ queues with optional exchange routing.
RabbitMQEventTransport(config)
¶
Bases: IEventTransport
Synchronous RabbitMQ event transport.
Sends pre-serialized event payloads to RabbitMQ queues using blocking connections. Optionally routes through an exchange for pub/sub patterns.
Attributes:
| Name | Type | Description |
|---|---|---|
connection_string |
str
|
AMQP connection string. |
exchange_name |
str | None
|
Optional exchange name for routing. |
declare_default_queue |
bool
|
Whether the queue named after the event is declared (and bound) before publishing. |
Initialize the synchronous RabbitMQ event transport.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
RabbitMQConnectionConfig
|
RabbitMQ connection configuration. |
required |
Source code in plugins/spakky-rabbitmq/src/spakky/plugins/rabbitmq/event/transport.py
send(event_name, payload, headers, partition_key=None)
¶
Send a pre-serialized event payload to RabbitMQ.
Creates a new connection, publishes the payload with the event name as
routing key, and closes the connection. Unless declare_default_queue
is turned off, the queue of the unnamed subscriber is declared first so
that the message is kept even before a consumer has started.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_name
|
str
|
Routing key / queue name for the event. |
required |
payload
|
bytes
|
Pre-serialized JSON bytes. |
required |
headers
|
dict[str, str]
|
Metadata headers for trace propagation. |
required |
partition_key
|
str | None
|
Accepted for transport interface parity and not used for routing — RabbitMQ has no partitions and a single queue is already FIFO ordered, so there is nothing to pin the payload to. |
None
|
Source code in plugins/spakky-rabbitmq/src/spakky/plugins/rabbitmq/event/transport.py
AsyncRabbitMQEventTransport(config)
¶
Bases: IAsyncEventTransport
Asynchronous RabbitMQ event transport.
Sends pre-serialized event payloads to RabbitMQ queues using async connections. Optionally routes through an exchange for pub/sub patterns.
Attributes:
| Name | Type | Description |
|---|---|---|
connection_string |
str
|
AMQP connection string. |
exchange_name |
str | None
|
Optional exchange name for routing. |
declare_default_queue |
bool
|
Whether the queue named after the event is declared (and bound) before publishing. |
Initialize the asynchronous RabbitMQ event transport.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
RabbitMQConnectionConfig
|
RabbitMQ connection configuration. |
required |
Source code in plugins/spakky-rabbitmq/src/spakky/plugins/rabbitmq/event/transport.py
send(event_name, payload, headers, partition_key=None)
async
¶
Send a pre-serialized event payload to RabbitMQ asynchronously.
Creates a new robust connection, publishes the payload with the event
name as routing key, and closes the connection. Unless
declare_default_queue is turned off, the queue of the unnamed
subscriber is declared first so that the message is kept even before a
consumer has started.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_name
|
str
|
Routing key / queue name for the event. |
required |
payload
|
bytes
|
Pre-serialized JSON bytes. |
required |
headers
|
dict[str, str]
|
Metadata headers for trace propagation. |
required |
partition_key
|
str | None
|
Accepted for transport interface parity and not used for routing — RabbitMQ has no partitions and a single queue is already FIFO ordered, so there is nothing to pin the payload to. |
None
|
Source code in plugins/spakky-rabbitmq/src/spakky/plugins/rabbitmq/event/transport.py
이벤트 Consumer¶
RabbitMQ event consumers for integration events.
Provides synchronous and asynchronous event consumers that run as background
services. Each subscriber owns a queue of its own, and every message is handed
to the broker-neutral delivery contract of spakky.event.delivery, which decides
whether the message is acknowledged, retried or dead-lettered.
RabbitMQAuthSettlement(drop=False)
dataclass
¶
What the auth failure policy asked for while one message was delivered.
drop = False
class-attribute
instance-attribute
¶
A nack_drop policy applied; the message is rejected instead of acked.
RabbitMQEventConsumer(config)
¶
Bases: IIntegrationEventConsumer, IEventRedriver, AbstractBackgroundService
Synchronous RabbitMQ event consumer.
Runs as a background service that consumes one durable queue per subscriber
and delivers each message to that subscriber's synchronous handlers under
the delivery contract: failed handlers are retried by RetryPolicy, and a
message the subscriber gives up on moves to the subscriber's own
dead-letter queue.
Attributes:
| Name | Type | Description |
|---|---|---|
connection_string |
str
|
AMQP connection string. |
exchange_name |
str | None
|
Exchange subscriber queues are bound to, if configured. |
connection |
BlockingConnection
|
Blocking RabbitMQ connection. |
channel |
BlockingChannel
|
Blocking channel for message consumption. |
publish_channel |
BlockingChannel
|
Confirm-mode channel for retry and dead-letter copies. |
Initialize the synchronous RabbitMQ event consumer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
RabbitMQConnectionConfig
|
RabbitMQ connection configuration. |
required |
Source code in plugins/spakky-rabbitmq/src/spakky/plugins/rabbitmq/event/consumer.py
set_propagator(propagator)
¶
Set the trace propagator for extracting trace context from messages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
propagator
|
ITracePropagator
|
The trace propagator instance. |
required |
Source code in plugins/spakky-rabbitmq/src/spakky/plugins/rabbitmq/event/consumer.py
register(event, handler, *, subscriber=None, handler_name=None, retry=None)
¶
Register a handler in the subscription of subscriber for event.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
type[EventT_contra]
|
The event type to handle. |
required |
handler
|
EventHandlerCallback[EventT_contra]
|
The callback function to handle the event. |
required |
subscriber
|
SubscriberName | None
|
Independent subscriber the handler belongs to; None is the unnamed subscriber consuming the queue named after the event. |
None
|
handler_name
|
HandlerName | None
|
Name unique within the subscription. |
None
|
retry
|
RetryPolicy | None
|
Retry the handler's class declared for its subscriber. Its
attempt counts and intervals apply to that subscriber only; its
mode must match the consumer's |
None
|
Raises:
| Type | Description |
|---|---|
SubscriberExchangeRequiredError
|
A named subscriber is registered while no exchange is configured. |
ConflictingSubscriberRetryPolicyError
|
Another handler class of the subscriber declared a different retry. |
SubscriberRetryModeUnsupportedError
|
The subscriber's retry mode
differs from the consumer's |
Source code in plugins/spakky-rabbitmq/src/spakky/plugins/rabbitmq/event/consumer.py
initialize()
¶
Initialize RabbitMQ connection and declare subscriber queues.
Declares, for every subscription, the subscriber queue (bound to the exchange when one is configured), its dead-letter queue and, in delayed retry mode, its wait queue, then starts consuming.
Source code in plugins/spakky-rabbitmq/src/spakky/plugins/rabbitmq/event/consumer.py
dispose()
¶
Clean up RabbitMQ resources.
Closes the channel and connection when the service is stopped. Messages still waiting for a blocking retry return to their queue.
Source code in plugins/spakky-rabbitmq/src/spakky/plugins/rabbitmq/event/consumer.py
run()
¶
Run the event consumer loop.
Starts consuming messages from RabbitMQ queues and blocks until the stop event is set.
Source code in plugins/spakky-rabbitmq/src/spakky/plugins/rabbitmq/event/consumer.py
redrive(event, *, subscriber=None, limit=None)
¶
Run the failed handlers of this subscriber's dead letters again.
Opens a connection of its own, so it may be called from any thread while the consumer is running. A dead letter that fails again is held until every stored one was taken, then replaced by a copy whose metadata lists only the handlers that failed again, so a handler that succeeded here is not run by the next redrive. Each dead letter is delivered at most once per call.
Raises:
| Type | Description |
|---|---|
SubscriptionNotFoundError
|
Nothing is registered for the pair. |
Source code in plugins/spakky-rabbitmq/src/spakky/plugins/rabbitmq/event/consumer.py
AsyncRabbitMQEventConsumer(config)
¶
Bases: IAsyncIntegrationEventConsumer, IAsyncEventRedriver, AbstractAsyncBackgroundService
Asynchronous RabbitMQ event consumer.
Runs as an async background service that consumes one durable queue per
subscriber and delivers each message to that subscriber's asynchronous
handlers under the delivery contract: failed handlers are retried by
RetryPolicy, and a message the subscriber gives up on moves to the
subscriber's own dead-letter queue. Uses a robust connection for automatic
reconnection.
Attributes:
| Name | Type | Description |
|---|---|---|
connection_string |
str
|
AMQP connection string. |
exchange_name |
str | None
|
Exchange subscriber queues are bound to, if configured. |
connection |
AbstractRobustConnection
|
Robust RabbitMQ connection for async operations. |
channel |
AbstractChannel
|
Channel for message consumption. |
publish_channel |
AbstractChannel
|
Confirm-mode channel for retry and dead-letter copies. |
Initialize the asynchronous RabbitMQ event consumer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
RabbitMQConnectionConfig
|
RabbitMQ connection configuration. |
required |
Source code in plugins/spakky-rabbitmq/src/spakky/plugins/rabbitmq/event/consumer.py
set_propagator(propagator)
¶
Set the trace propagator for extracting trace context from messages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
propagator
|
ITracePropagator
|
The trace propagator instance. |
required |
Source code in plugins/spakky-rabbitmq/src/spakky/plugins/rabbitmq/event/consumer.py
register(event, handler, *, subscriber=None, handler_name=None, retry=None)
¶
Register an async handler in the subscription of subscriber for event.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
type[EventT_contra]
|
The event type to handle. |
required |
handler
|
AsyncEventHandlerCallback[EventT_contra]
|
The async callback function to handle the event. |
required |
subscriber
|
SubscriberName | None
|
Independent subscriber the handler belongs to; None is the unnamed subscriber consuming the queue named after the event. |
None
|
handler_name
|
HandlerName | None
|
Name unique within the subscription. |
None
|
retry
|
RetryPolicy | None
|
Retry the handler's class declared for its subscriber. Its
attempt counts and intervals apply to that subscriber only; its
mode must match the consumer's |
None
|
Raises:
| Type | Description |
|---|---|
SubscriberExchangeRequiredError
|
A named subscriber is registered while no exchange is configured. |
ConflictingSubscriberRetryPolicyError
|
Another handler class of the subscriber declared a different retry. |
SubscriberRetryModeUnsupportedError
|
The subscriber's retry mode
differs from the consumer's |
Source code in plugins/spakky-rabbitmq/src/spakky/plugins/rabbitmq/event/consumer.py
initialize_async()
async
¶
Initialize async RabbitMQ connection and declare subscriber queues.
Declares, for every subscription, the subscriber queue (bound to the exchange when one is configured), its dead-letter queue and, in delayed retry mode, its wait queue, then starts consuming.
Source code in plugins/spakky-rabbitmq/src/spakky/plugins/rabbitmq/event/consumer.py
dispose_async()
async
¶
Clean up async RabbitMQ resources.
Closes the channel and connection when the service is stopped.
Source code in plugins/spakky-rabbitmq/src/spakky/plugins/rabbitmq/event/consumer.py
run_async()
async
¶
Run the async event consumer loop.
Waits for the stop event to be set while consuming messages from RabbitMQ queues in the background.
Source code in plugins/spakky-rabbitmq/src/spakky/plugins/rabbitmq/event/consumer.py
redrive(event, *, subscriber=None, limit=None)
async
¶
Run the failed handlers of this subscriber's dead letters again.
Opens a connection of its own. A dead letter that fails again is held until every stored one was taken, then replaced by a copy whose metadata lists only the handlers that failed again, so a handler that succeeded here is not run by the next redrive. Each dead letter is delivered at most once per call.
Raises:
| Type | Description |
|---|---|
SubscriptionNotFoundError
|
Nothing is registered for the pair. |
Source code in plugins/spakky-rabbitmq/src/spakky/plugins/rabbitmq/event/consumer.py
구독자 큐 이름¶
Names of the RabbitMQ queues one subscriber owns for one event.
EventName = str
¶
Routing identity of an event; also the routing key it is published with.
QueueName = str
¶
Name of a RabbitMQ queue.
RabbitMQSubscriptionQueues
¶
Bases: BaseModel
Queues that keep one subscriber's messages apart from every other one.
queue
instance-attribute
¶
Queue the subscriber consumes; bound to the exchange by the event name.
retry_queue
instance-attribute
¶
Wait queue whose expired messages return to queue only.
dead_letter_queue
instance-attribute
¶
Queue storing the messages the subscriber gave up on.
of(event_name, subscriber)
classmethod
¶
Derive the queue names of subscriber for event_name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_name
|
EventName
|
Routing identity of the event. |
required |
subscriber
|
SubscriberName | None
|
Subscriber name; None is the unnamed subscriber, which keeps the queue named after the event alone. |
required |
Source code in plugins/spakky-rabbitmq/src/spakky/plugins/rabbitmq/event/topology.py
에러¶
Errors raised by the RabbitMQ plugin.
AbstractSpakkyRabbitMQError
¶
SubscriberExchangeRequiredError(subscriber, event_type)
¶
Bases: AbstractSpakkyRabbitMQError
Raised at startup when a named subscriber is used without an exchange.
Every named subscriber owns a queue bound to the configured exchange so that each one receives its own copy of an event. The default exchange can only deliver a message to the single queue named by its routing key.
Source code in plugins/spakky-rabbitmq/src/spakky/plugins/rabbitmq/error.py
RabbitMQAuthRequeueError
¶
Bases: AuthRequirementProviderUnavailableError
Raised when the auth failure policy keeps a message unacknowledged.
The delivery contract lets provider-unavailable errors leave the delivery
so that the message is not acknowledged. A nack_requeue auth policy asks
for exactly that, so it is signalled through the same error family.
SubscriberRetryModeUnsupportedError(subscriber)
¶
Bases: AbstractSpakkyRabbitMQError
Raised at startup when a subscriber needs a retry mode of its own.
A RabbitMQ consumer declares its queues and its prefetch limit for one
retry mode. A subscriber whose resolved retry mode differs from the
consumer's retry.mode cannot get the order guarantee it declared, so the
registration fails instead of silently using the consumer's mode.
Source code in plugins/spakky-rabbitmq/src/spakky/plugins/rabbitmq/error.py
인증 Boundary¶
Auth boundary helpers for RabbitMQ consumer callbacks.
RABBITMQ_AUTH_BOUNDARY = 'rabbitmq'
module-attribute
¶
AuthInvocation boundary value used for RabbitMQ consumer handlers.
RABBITMQ_EVENT_NAME_ATTRIBUTE = 'event_name'
module-attribute
¶
AuthInvocation attribute carrying the integration event name.
RABBITMQ_SNAPSHOT_ATTRIBUTE = 'auth_context_snapshot'
module-attribute
¶
AuthInvocation attribute carrying the signed snapshot envelope.
RabbitMQAuthBoundary(application_context, snapshot_verifier)
¶
Verify signed snapshots and seed AuthContext for RabbitMQ handlers.
Source code in plugins/spakky-rabbitmq/src/spakky/plugins/rabbitmq/auth.py
seed_auth_context(*, event_type, operation, protected)
¶
Verify the current message snapshot and seed AuthContext when needed.
Source code in plugins/spakky-rabbitmq/src/spakky/plugins/rabbitmq/auth.py
set_current_rabbitmq_message_headers(headers)
¶
Set the message headers visible to the post-processor endpoint.
Source code in plugins/spakky-rabbitmq/src/spakky/plugins/rabbitmq/auth.py
reset_current_rabbitmq_message_headers(token)
¶
Restore the previous message header context.
current_rabbitmq_message_headers()
¶
Return the active RabbitMQ message headers for this handler invocation.
Source code in plugins/spakky-rabbitmq/src/spakky/plugins/rabbitmq/auth.py
설정¶
Configuration for RabbitMQ connections.
Provides configuration dataclass for RabbitMQ connection parameters including host, port, credentials, and exchange settings.
RabbitMQAuthFailureAction
¶
Bases: StrEnum
Ack/nack action used when a protected RabbitMQ handler fails auth.
ACK = 'ack'
class-attribute
instance-attribute
¶
Acknowledge and drop the message.
NACK_REQUEUE = 'nack_requeue'
class-attribute
instance-attribute
¶
Negative-acknowledge and requeue the message.
NACK_DROP = 'nack_drop'
class-attribute
instance-attribute
¶
Negative-acknowledge without requeueing the message.
RabbitMQConnectionConfig()
¶
Bases: BaseSettings
Configuration for RabbitMQ connection.
Stores connection parameters and provides a formatted connection string for establishing RabbitMQ connections.
Attributes:
| Name | Type | Description |
|---|---|---|
host |
str
|
RabbitMQ server hostname. |
port |
int
|
RabbitMQ server port. |
user |
str
|
Username for authentication. |
password |
str
|
Password for authentication. |
exchange_name |
str | None
|
Optional exchange name for pub/sub routing. |
declare_default_queue |
bool
|
Whether transports declare the unnamed queue. |
retry |
RetryPolicy
|
Retry policy applied to every subscriber independently. |
Source code in plugins/spakky-rabbitmq/src/spakky/plugins/rabbitmq/common/config.py
use_ssl
instance-attribute
¶
Flag indicating whether to use SSL for the connection.
host
instance-attribute
¶
RabbitMQ server hostname or IP address.
port
instance-attribute
¶
RabbitMQ server port number.
user
instance-attribute
¶
Username for RabbitMQ authentication.
password
instance-attribute
¶
Password for RabbitMQ authentication.
exchange_name = None
class-attribute
instance-attribute
¶
Optional exchange name for pub/sub message routing.
None publishes through the default exchange, which reaches only the queue named after the event. Named subscribers require an exchange.
declare_default_queue = True
class-attribute
instance-attribute
¶
Whether transports declare (and bind) the queue named after the event.
That queue belongs to the unnamed subscriber. Turn it off when every consumer of the published events is a named subscriber; otherwise the queue would only accumulate messages nobody consumes.
retry = RetryPolicy()
class-attribute
instance-attribute
¶
Retry limits, spacing and mode applied to each subscriber queue.
auth_challenge_action = RabbitMQAuthFailureAction.ACK
class-attribute
instance-attribute
¶
Message action for missing, invalid, or expired snapshot decisions.
auth_deny_action = RabbitMQAuthFailureAction.ACK
class-attribute
instance-attribute
¶
Message action for explicit DENY decisions from protected handlers.
auth_error_action = RabbitMQAuthFailureAction.NACK_REQUEUE
class-attribute
instance-attribute
¶
Message action for retryable provider ERROR decisions.
protocol
property
¶
Determine protocol based on SSL usage.
Returns:
| Type | Description |
|---|---|
str
|
'amqps' if SSL is enabled, otherwise 'amqp'. |
connection_string
property
¶
Generate AMQP connection string from configuration.
Returns:
| Type | Description |
|---|---|
str
|
Formatted AMQP connection string with credentials and host information. |
후처리기¶
Post-processor for registering RabbitMQ event handlers.
Automatically discovers and registers event handlers from @EventHandler decorated classes, connecting them to RabbitMQ consumers with dependency injection.
RabbitMQPostProcessor
¶
Bases: IPostProcessor, IContainerAware, IApplicationContextAware
Post-processor that registers event handlers with RabbitMQ consumers.
Scans @EventHandler decorated classes for @event decorated methods and automatically registers them with the appropriate RabbitMQ consumer (sync or async) with proper dependency injection.
set_container(container)
¶
Set the container for dependency injection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
container
|
IContainer
|
The IoC container. |
required |
set_application_context(application_context)
¶
Set the application context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
application_context
|
IApplicationContext
|
The application context instance. |
required |
Source code in plugins/spakky-rabbitmq/src/spakky/plugins/rabbitmq/post_processor.py
post_process(pod)
¶
Register event handlers from event handler classes.
Reads the @on_event methods declared on the event handler class, so that handlers advised by an aspect (whose Pod is an AOP proxy) are registered too, and registers each with the sync or async consumer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pod
|
object
|
The Pod to process, potentially an event handler. |
required |
Returns:
| Type | Description |
|---|---|
object
|
The Pod, with event handlers registered if it's an event handler. |
Source code in plugins/spakky-rabbitmq/src/spakky/plugins/rabbitmq/post_processor.py
66 67 68 69 70 71 72 73 74 75 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 | |
추가 모듈¶
SUBSCRIBER_QUEUE_SEPARATOR = '.'
module-attribute
¶
Separator between an event name and the subscriber that owns the queue.
RETRY_QUEUE_SUFFIX = '.retry'
module-attribute
¶
Suffix of the wait queue that holds a subscriber's delayed retries.
DEAD_LETTER_QUEUE_SUFFIX = '.dlq'
module-attribute
¶
Suffix of the queue that stores a subscriber's dead letters.
DEFAULT_EXCHANGE_NAME = ''
module-attribute
¶
AMQP default exchange: routes a message to the queue named by its routing key.
DEAD_LETTER_EXCHANGE_ARGUMENT = 'x-dead-letter-exchange'
module-attribute
¶
Queue argument naming the exchange expired messages are republished to.
DEAD_LETTER_ROUTING_KEY_ARGUMENT = 'x-dead-letter-routing-key'
module-attribute
¶
Queue argument naming the routing key expired messages are republished with.
BLOCKING_PREFETCH_COUNT = 1
module-attribute
¶
Unacknowledged messages allowed per queue while retries block the queue.
PERSISTENT_DELIVERY_MODE = 2
module-attribute
¶
AMQP delivery mode that survives a broker restart in a durable queue.
MILLISECONDS_PER_SECOND = 1000
module-attribute
¶
Factor between RetryPolicy seconds and AMQP expiration milliseconds.