-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumer.py
36 lines (31 loc) · 1.06 KB
/
consumer.py
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
from typing import Callable
from dataclasses import dataclass, field
from session import MQSession
from enums import MQTopic
@dataclass
class MQConsumer:
"""
Class that implements RabbitMQ consumer method.
"""
topic: MQTopic
on_msg_cb: Callable[[dict], None]
_session: MQSession = field(default_factory=MQSession.default_session)
class Config:
arbitrary_types_allowed = True
def start(self):
"""
Start RabbitMQ consumer for messages.
"""
with self._session as session:
channel = session.channel()
channel.exchange_declare(
exchange="rabbitmq", exchange_type="topic")
result = channel.queue_declare("", exclusive=True)
queue = result.method.queue
channel.queue_bind(
exchange="rabbitmq", queue=queue, routing_key=self.topic.value)
channel.basic_consume(
queue=queue,
on_message_callback=self._on_message,
auto_ack=True)
channel.start_consuming()