1+ # type: ignore
2+
3+
14from rabbitmq_amqp_python_client import (
5+ AddressHelper ,
6+ AMQPMessagingHandler ,
27 BindingSpecification ,
38 Connection ,
9+ Event ,
410 ExchangeSpecification ,
511 Message ,
612 QuorumQueueSpecification ,
7- exchange_address ,
813)
914
1015
16+ class MyMessageHandler (AMQPMessagingHandler ):
17+
18+ def __init__ (self ):
19+ super ().__init__ ()
20+ self ._count = 0
21+
22+ def on_message (self , event : Event ):
23+ print ("received message: " + str (event .message .annotations ))
24+
25+ # accepting
26+ self .delivery_context .accept (event )
27+
28+ # in case of rejection (+eventually deadlettering)
29+ # self.delivery_context.discard(event)
30+
31+ # in case of requeuing
32+ # self.delivery_context.requeue(event)
33+
34+ # annotations = {}
35+ # annotations[symbol('x-opt-string')] = 'x-test1'
36+ # in case of requeuing with annotations added
37+ # self.delivery_context.requeue_with_annotations(event, annotations)
38+
39+ # in case of rejection with annotations added
40+ # self.delivery_context.discard_with_annotations(event)
41+
42+ print ("count " + str (self ._count ))
43+
44+ self ._count = self ._count + 1
45+
46+ if self ._count == 100 :
47+ print ("closing receiver" )
48+ # if you want you can add cleanup operations here
49+ # event.receiver.close()
50+ # event.connection.close()
51+
52+ def on_connection_closed (self , event : Event ):
53+ # if you want you can add cleanup operations here
54+ print ("connection closed" )
55+
56+ def on_link_closed (self , event : Event ) -> None :
57+ # if you want you can add cleanup operations here
58+ print ("link closed" )
59+
60+
61+ def create_connection () -> Connection :
62+ connection = Connection ("amqp://guest:guest@localhost:5672/" )
63+ connection .dial ()
64+
65+ return connection
66+
67+
1168def main () -> None :
69+
1270 exchange_name = "test-exchange"
1371 queue_name = "example-queue"
1472 routing_key = "routing-key"
15- connection = Connection ( "amqp://guest:guest@localhost:5672/" )
73+ messages_to_publish = 100
1674
1775 print ("connection to amqp server" )
18- connection . dial ()
76+ connection = create_connection ()
1977
2078 management = connection .management ()
2179
2280 print ("declaring exchange and queue" )
2381 management .declare_exchange (ExchangeSpecification (name = exchange_name , arguments = {}))
2482
25- management .declare_queue (QuorumQueueSpecification (name = queue_name ))
83+ management .declare_queue (
84+ QuorumQueueSpecification (name = queue_name )
85+ # QuorumQueueSpecification(name=queue_name, dead_letter_exchange="dead-letter")
86+ )
2687
2788 print ("binding queue to exchange" )
2889 bind_name = management .bind (
@@ -33,21 +94,44 @@ def main() -> None:
3394 )
3495 )
3596
36- addr = exchange_address (exchange_name , routing_key )
97+ addr = AddressHelper .exchange_address (exchange_name , routing_key )
98+
99+ addr_queue = AddressHelper .queue_address (queue_name )
37100
38101 print ("create a publisher and publish a test message" )
39102 publisher = connection .publisher (addr )
40103
41- publisher .publish (Message (body = "test" ))
104+ print ("purging the queue" )
105+ messages_purged = management .purge_queue (queue_name )
106+
107+ print ("messages purged: " + str (messages_purged ))
108+ # management.close()
109+
110+ # publish 10 messages
111+ for i in range (messages_to_publish ):
112+ publisher .publish (Message (body = "test" ))
42113
43114 publisher .close ()
44115
116+ print (
117+ "create a consumer and consume the test message - press control + c to terminate to consume"
118+ )
119+ consumer = connection .consumer (addr_queue , handler = MyMessageHandler ())
120+
121+ try :
122+ consumer .run ()
123+ except KeyboardInterrupt :
124+ pass
125+
126+ print ("cleanup" )
127+ consumer .close ()
128+ # once we finish consuming if we close the connection we need to create a new one
129+ # connection = create_connection()
130+ # management = connection.management()
131+
45132 print ("unbind" )
46133 management .unbind (bind_name )
47134
48- print ("purging the queue" )
49- management .purge_queue (queue_name )
50-
51135 print ("delete queue" )
52136 management .delete_queue (queue_name )
53137
@@ -56,7 +140,9 @@ def main() -> None:
56140
57141 print ("closing connections" )
58142 management .close ()
143+ print ("after management closing" )
59144 connection .close ()
145+ print ("after connection closing" )
60146
61147
62148if __name__ == "__main__" :
0 commit comments