-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener.py
86 lines (74 loc) · 3.05 KB
/
listener.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
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
from threading import Thread
from time import sleep
import pika
class Listener:
_connection = None
_thread = None
_host: str = 'localhost'
_port: int = 5672
_username: str = 'guest'
_password: str = 'guest'
_virtualhost: str = '/'
_exchange: str = 'output'
_queue: str = 'input'
_on_message_callback = None
def __init__(self,
host: str = 'localhost',
port: int = 5672,
username: str = 'guest',
password: str = 'guest',
virtualhost: str = '/',
exchange: str = 'output',
queue: str = 'input',
on_message_callback=None) -> None:
super().__init__()
self._host = host
self._port = port
self._username = username
self._password = password
self._virtualhost = virtualhost
self._exchange = exchange
self._queue = queue
self._on_message_callback = on_message_callback
def listen(self):
if self._thread is None:
self._thread = ListenerThread(host=self._host,
port=self._port,
username=self._username,
password=self._password,
virtualhost=self._virtualhost,
exchange=self._exchange,
queue=self._queue,
on_message_callback=self._on_message_callback)
self._thread.setDaemon(True)
self._thread.start()
class ListenerThread(Thread):
_channel = None
def __init__(self,
host: str = 'localhost',
port: int = 5672,
username: str = 'guest',
password: str = 'guest',
virtualhost: str = '/',
exchange: str = 'output',
queue: str = 'input',
on_message_callback=None):
Thread.__init__(self)
credentials = pika.PlainCredentials(username, password)
parameters = pika.ConnectionParameters(host=host,
port=port,
virtual_host=virtualhost,
credentials=credentials,
socket_timeout=10,
retry_delay=5,
connection_attempts=3)
connection = pika.BlockingConnection(parameters=parameters)
self._channel = connection.channel()
self._channel.queue_declare(queue)
self._channel.queue_bind(exchange=exchange, queue=queue)
self._channel.basic_consume(queue, on_message_callback=on_message_callback, auto_ack=True)
def run(self):
while self.is_alive():
sleep(1)
self._channel.start_consuming()
self._channel.stop_consuming()