-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconsumer.py
More file actions
224 lines (197 loc) · 9.21 KB
/
consumer.py
File metadata and controls
224 lines (197 loc) · 9.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
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
"""AMQP consumer implementation"""
import asyncio
from typing import Any, AsyncGenerator
try:
from aio_pika import ExchangeType # type: ignore[import-not-found]
from aio_pika.abc import ( # type: ignore[import-not-found]
AbstractChannel,
AbstractConnection,
AbstractExchange,
AbstractQueue,
)
except ImportError as e:
raise ImportError(
"aio-pika is required for AMQP support. "
"Install with: pip install asyncapi-python[amqp]"
) from e
from asyncapi_python.kernel.wire.typing import Consumer
from .config import AmqpBindingType
from .message import AmqpIncomingMessage
class AmqpConsumer(Consumer[AmqpIncomingMessage]):
"""AMQP consumer implementation with comprehensive binding support"""
def __init__(
self,
connection: AbstractConnection,
queue_name: str,
exchange_name: str = "",
exchange_type: str = "direct",
routing_key: str = "",
binding_type: AmqpBindingType = AmqpBindingType.QUEUE,
queue_properties: dict[str, Any] | None = None,
binding_arguments: dict[str, Any] | None = None,
arguments: dict[str, Any] | None = None,
):
self._connection = connection
self._queue_name = queue_name
self._exchange_name = exchange_name
self._exchange_type = exchange_type
self._routing_key = routing_key
self._binding_type = binding_type
self._queue_properties = queue_properties or {}
self._binding_arguments = binding_arguments or {}
self._arguments = arguments or {}
self._channel: AbstractChannel | None = None
self._queue: AbstractQueue | None = None
self._exchange: AbstractExchange | None = None
self._started = False
self._stop_event = asyncio.Event()
async def start(self) -> None:
"""Start the consumer with pattern matching for binding types"""
if self._started:
return
self._channel = await self._connection.channel()
# Pattern matching for queue setup based on binding type
match self._binding_type:
# Reply channel pattern
case AmqpBindingType.REPLY:
self._queue = await self._channel.declare_queue(
name=self._queue_name,
durable=self._queue_properties.get("durable", True),
exclusive=self._queue_properties.get("exclusive", False),
auto_delete=self._queue_properties.get("auto_delete", False),
arguments=self._arguments,
)
# Simple queue binding pattern (default exchange)
case AmqpBindingType.QUEUE:
self._queue = await self._channel.declare_queue(
name=self._queue_name,
durable=self._queue_properties.get("durable", True),
exclusive=self._queue_properties.get("exclusive", False),
auto_delete=self._queue_properties.get("auto_delete", False),
arguments=self._arguments,
)
# Routing key binding pattern (pub/sub with named exchange)
case AmqpBindingType.ROUTING_KEY:
# Declare the exchange
match self._exchange_type:
case "direct":
self._exchange = await self._channel.declare_exchange(
name=self._exchange_name,
type=ExchangeType.DIRECT,
durable=True,
arguments=self._arguments,
)
case "topic":
self._exchange = await self._channel.declare_exchange(
name=self._exchange_name,
type=ExchangeType.TOPIC,
durable=True,
arguments=self._arguments,
)
case "fanout":
self._exchange = await self._channel.declare_exchange(
name=self._exchange_name,
type=ExchangeType.FANOUT,
durable=True,
arguments=self._arguments,
)
case "headers":
self._exchange = await self._channel.declare_exchange(
name=self._exchange_name,
type=ExchangeType.HEADERS,
durable=True,
arguments=self._arguments,
)
case unknown_type:
raise ValueError(f"Unsupported exchange type: {unknown_type}")
# Create exclusive queue for this consumer
self._queue = await self._channel.declare_queue(
name="", # Auto-generated name
durable=self._queue_properties.get("durable", False),
exclusive=self._queue_properties.get("exclusive", True),
auto_delete=self._queue_properties.get("auto_delete", True),
arguments=self._arguments,
)
# Bind queue to exchange with routing key
await self._queue.bind(self._exchange, routing_key=self._routing_key)
# Exchange binding pattern (advanced pub/sub with binding arguments)
case AmqpBindingType.EXCHANGE:
# Declare the exchange
match self._exchange_type:
case "fanout":
self._exchange = await self._channel.declare_exchange(
name=self._exchange_name,
type=ExchangeType.FANOUT,
durable=True,
arguments=self._arguments,
)
case "headers":
self._exchange = await self._channel.declare_exchange(
name=self._exchange_name,
type=ExchangeType.HEADERS,
durable=True,
arguments=self._arguments,
)
case "topic":
self._exchange = await self._channel.declare_exchange(
name=self._exchange_name,
type=ExchangeType.TOPIC,
durable=True,
arguments=self._arguments,
)
case "direct":
self._exchange = await self._channel.declare_exchange(
name=self._exchange_name,
type=ExchangeType.DIRECT,
durable=True,
arguments=self._arguments,
)
case unknown_type:
raise ValueError(f"Unsupported exchange type: {unknown_type}")
# Create exclusive queue for this consumer
self._queue = await self._channel.declare_queue(
name="", # Auto-generated name
durable=self._queue_properties.get("durable", False),
exclusive=self._queue_properties.get("exclusive", True),
auto_delete=self._queue_properties.get("auto_delete", True),
arguments=self._arguments,
)
# Bind queue to exchange with binding arguments (for headers exchange)
if self._binding_arguments:
await self._queue.bind(
self._exchange, arguments=self._binding_arguments
)
else:
await self._queue.bind(self._exchange)
self._started = True
async def stop(self) -> None:
"""Stop the consumer"""
if not self._started:
return
self._stop_event.set()
if self._channel:
await self._channel.close()
self._channel = None
self._queue = None
self._exchange = None
self._started = False
def recv(self) -> AsyncGenerator[AmqpIncomingMessage, None]:
"""Async generator that yields incoming messages"""
return self._message_generator()
async def _message_generator(self) -> AsyncGenerator[AmqpIncomingMessage, None]:
"""Internal async generator for messages"""
if not self._started or not self._queue:
raise RuntimeError("Consumer not started")
async with self._queue.iterator() as queue_iter:
async for amqp_message in queue_iter:
if self._stop_event.is_set():
break
# Convert to our message format
incoming_msg = AmqpIncomingMessage(
_payload=amqp_message.body,
_headers=dict(amqp_message.headers) if amqp_message.headers else {},
_correlation_id=amqp_message.correlation_id,
_reply_to=amqp_message.reply_to,
_amqp_message=amqp_message,
)
yield incoming_msg