1
1
# type: ignore
2
-
3
-
4
- import time
5
- from dataclasses import dataclass
6
- from typing import Optional
7
-
8
2
from rabbitmq_amqp_python_client import (
9
3
AddressHelper ,
10
4
AMQPMessagingHandler ,
11
5
Connection ,
12
6
ConnectionClosed ,
13
- Consumer ,
14
7
Environment ,
15
8
Event ,
16
9
ExchangeSpecification ,
17
10
ExchangeToQueueBindingSpecification ,
18
- Management ,
19
11
Message ,
20
- Publisher ,
21
12
QuorumQueueSpecification ,
22
13
)
23
14
24
-
25
15
# here we keep track of the objects we need to reconnect
26
- @dataclass
27
- class ConnectionConfiguration :
28
- connection : Optional [Connection ] = None
29
- management : Optional [Management ] = None
30
- publisher : Optional [Publisher ] = None
31
- consumer : Optional [Consumer ] = None
32
-
16
+ MESSAGES_TO_PUBLISH = 50000
33
17
34
- connection_configuration = ConnectionConfiguration ()
35
- MESSAGES_TO_PUBLSH = 50000
36
18
37
-
38
- # disconnection callback
39
- # here you can cleanup or reconnect
40
- def on_disconnection ():
41
-
42
- print ("disconnected" )
43
- global environment
44
- exchange_name = "test-exchange"
45
- queue_name = "example-queue"
46
- routing_key = "routing-key"
47
-
48
- global connection_configuration
49
-
50
- addr = AddressHelper .exchange_address (exchange_name , routing_key )
51
- addr_queue = AddressHelper .queue_address (queue_name )
52
-
53
- if connection_configuration .connection is not None :
54
- connection_configuration .connection = create_connection ()
55
- if connection_configuration .management is not None :
56
- connection_configuration .management = (
57
- connection_configuration .connection .management ()
58
- )
59
- if connection_configuration .publisher is not None :
60
- connection_configuration .publisher = (
61
- connection_configuration .connection .publisher (addr )
62
- )
63
- if connection_configuration .consumer is not None :
64
- connection_configuration .consumer = (
65
- connection_configuration .connection .consumer (
66
- addr_queue , message_handler = MyMessageHandler ()
67
- )
68
- )
69
-
70
-
71
- environment = Environment (
72
- uri = "amqp://guest:guest@localhost:5672/" , on_disconnection_handler = on_disconnection
73
- )
19
+ environment = Environment (uri = "amqp://guest:guest@localhost:5672/" , reconnect = True )
74
20
75
21
76
22
class MyMessageHandler (AMQPMessagingHandler ):
@@ -102,7 +48,7 @@ def on_message(self, event: Event):
102
48
103
49
self ._count = self ._count + 1
104
50
105
- if self ._count == MESSAGES_TO_PUBLSH :
51
+ if self ._count == MESSAGES_TO_PUBLISH :
106
52
print ("closing receiver" )
107
53
# if you want you can add cleanup operations here
108
54
@@ -136,29 +82,22 @@ def main() -> None:
136
82
queue_name = "example-queue"
137
83
routing_key = "routing-key"
138
84
139
- global connection_configuration
140
-
141
85
print ("connection to amqp server" )
142
- if connection_configuration .connection is None :
143
- connection_configuration .connection = create_connection ()
144
-
145
- if connection_configuration .management is None :
146
- connection_configuration .management = (
147
- connection_configuration .connection .management ()
148
- )
86
+ connection = create_connection ()
87
+ management = connection .management ()
88
+ publisher = None
89
+ consumer = None
149
90
150
91
print ("declaring exchange and queue" )
151
- connection_configuration .management .declare_exchange (
152
- ExchangeSpecification (name = exchange_name )
153
- )
92
+ management .declare_exchange (ExchangeSpecification (name = exchange_name ))
154
93
155
- connection_configuration . management .declare_queue (
94
+ management .declare_queue (
156
95
QuorumQueueSpecification (name = queue_name )
157
96
# QuorumQueueSpecification(name=queue_name, dead_letter_exchange="dead-letter")
158
97
)
159
98
160
99
print ("binding queue to exchange" )
161
- bind_name = connection_configuration . management .bind (
100
+ bind_name = management .bind (
162
101
ExchangeToQueueBindingSpecification (
163
102
source_exchange = exchange_name ,
164
103
destination_queue = queue_name ,
@@ -171,34 +110,32 @@ def main() -> None:
171
110
addr_queue = AddressHelper .queue_address (queue_name )
172
111
173
112
print ("create a publisher and publish a test message" )
174
- if connection_configuration .publisher is None :
175
- connection_configuration .publisher = (
176
- connection_configuration .connection .publisher (addr )
177
- )
113
+ if publisher is None :
114
+ publisher = connection .publisher (addr )
178
115
179
116
print ("purging the queue" )
180
- messages_purged = connection_configuration . management .purge_queue (queue_name )
117
+ messages_purged = management .purge_queue (queue_name )
181
118
182
119
print ("messages purged: " + str (messages_purged ))
183
- # management.close()
184
120
185
121
# publishing messages
186
122
while True :
187
- for i in range (MESSAGES_TO_PUBLSH ):
123
+ for i in range (MESSAGES_TO_PUBLISH ):
188
124
189
125
if i % 1000 == 0 :
190
126
print ("published 1000 messages..." )
191
127
try :
192
- if connection_configuration . publisher is not None :
193
- connection_configuration . publisher .publish (Message (body = "test" ))
128
+ if publisher is not None :
129
+ publisher .publish (Message (body = "test" ))
194
130
except ConnectionClosed :
195
131
print ("publisher closing exception, resubmitting" )
132
+ publisher = connection .publisher (addr )
196
133
continue
197
134
198
135
print ("closing publisher" )
199
136
try :
200
- if connection_configuration . publisher is not None :
201
- connection_configuration . publisher .close ()
137
+ if publisher is not None :
138
+ publisher .close ()
202
139
except ConnectionClosed :
203
140
print ("publisher closing exception, resubmitting" )
204
141
continue
@@ -207,43 +144,39 @@ def main() -> None:
207
144
print (
208
145
"create a consumer and consume the test message - press control + c to terminate to consume"
209
146
)
210
- if connection_configuration .consumer is None :
211
- connection_configuration .consumer = (
212
- connection_configuration .connection .consumer (
213
- addr_queue , message_handler = MyMessageHandler ()
214
- )
215
- )
147
+ if consumer is None :
148
+ consumer = connection .consumer (addr_queue , message_handler = MyMessageHandler ())
216
149
217
150
while True :
218
151
try :
219
- connection_configuration . consumer .run ()
152
+ consumer .run ()
220
153
except KeyboardInterrupt :
221
154
pass
222
155
except ConnectionClosed :
223
- time .sleep (1 )
156
+ consumer = connection .consumer (
157
+ addr_queue , message_handler = MyMessageHandler ()
158
+ )
224
159
continue
225
160
except Exception as e :
226
161
print ("consumer exited for exception " + str (e ))
227
162
228
163
break
229
164
230
165
print ("cleanup" )
231
- connection_configuration .consumer .close ()
232
- # once we finish consuming if we close the connection we need to create a new one
233
- # connection = create_connection()
234
- # management = connection.management()
166
+ consumer .close ()
235
167
168
+ management = connection .management ()
236
169
print ("unbind" )
237
- connection_configuration . management .unbind (bind_name )
170
+ management .unbind (bind_name )
238
171
239
172
print ("delete queue" )
240
- connection_configuration . management .delete_queue (queue_name )
173
+ management .delete_queue (queue_name )
241
174
242
175
print ("delete exchange" )
243
- connection_configuration . management .delete_exchange (exchange_name )
176
+ management .delete_exchange (exchange_name )
244
177
245
178
print ("closing connections" )
246
- connection_configuration . management .close ()
179
+ management .close ()
247
180
print ("after management closing" )
248
181
environment .close ()
249
182
print ("after connection closing" )
0 commit comments