11# type: ignore
2+
3+
24from rabbitmq_amqp_python_client import (
5+ AddressHelper ,
36 BindingSpecification ,
47 Connection ,
8+ DeliveryConsumerHandler ,
59 Event ,
610 ExchangeSpecification ,
711 Message ,
8- MessageAck ,
9- MessagingHandler ,
1012 QuorumQueueSpecification ,
11- exchange_address ,
12- queue_address ,
1313)
1414
1515
16- class MyMessageHandler (MessagingHandler ):
16+ class MyMessageHandler (DeliveryConsumerHandler ):
1717
1818 def __init__ (self ):
19- super ().__init__ (auto_accept = False , auto_settle = False )
19+ super ().__init__ ()
2020 self ._count = 0
2121
2222 def on_message (self , event : Event ):
2323 print ("received message: " + str (event .message .annotations ))
2424
2525 # accepting
26- MessageAck .accept (event )
26+ self . delivery_context .accept (event )
2727
2828 # in case of rejection (+eventually deadlettering)
29- # MessageAck .discard(event)
29+ # self.delivery_context .discard(event)
3030
3131 # in case of requeuing
32- # MessageAck .requeue(event)
32+ # self.delivery_context .requeue(event)
3333
3434 # annotations = {}
3535 # annotations[symbol('x-opt-string')] = 'x-test1'
3636 # in case of requeuing with annotations added
37- # MessageAck .requeue_with_annotations(event, annotations)
37+ # self.delivery_context .requeue_with_annotations(event, annotations)
3838
3939 # in case of rejection with annotations added
40- # MessageAck .discard_with_annotations(event)
40+ # self.delivery_context .discard_with_annotations(event)
4141
4242 print ("count " + str (self ._count ))
4343
4444 self ._count = self ._count + 1
4545
4646 if self ._count == 100 :
4747 print ("closing receiver" )
48- event .receiver .close ()
49- event .connection .close ()
48+ # if you want you can add cleanup operations here
49+ # event.receiver.close()
50+ # event.connection.close()
5051
5152 def on_connection_closed (self , event : Event ):
53+ # if you want you can add cleanup operations here
5254 print ("connection closed" )
5355
5456 def on_link_closed (self , event : Event ) -> None :
57+ # if you want you can add cleanup operations here
5558 print ("link closed" )
5659
5760
@@ -91,9 +94,9 @@ def main() -> None:
9194 )
9295 )
9396
94- addr = exchange_address (exchange_name , routing_key )
97+ addr = AddressHelper . exchange_address (exchange_name , routing_key )
9598
96- addr_queue = queue_address (queue_name )
99+ addr_queue = AddressHelper . queue_address (queue_name )
97100
98101 print ("create a publisher and publish a test message" )
99102 publisher = connection .publisher (addr )
@@ -102,7 +105,7 @@ def main() -> None:
102105 messages_purged = management .purge_queue (queue_name )
103106
104107 print ("messages purged: " + str (messages_purged ))
105- management .close ()
108+ # management.close()
106109
107110 # publish 10 messages
108111 for i in range (messages_to_publish ):
@@ -121,22 +124,22 @@ def main() -> None:
121124 pass
122125
123126 print ("cleanup" )
124- # once we finish consuming we close the connection so we need to create a new one
125- connection = create_connection ()
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()
126131
127- management = connection .management ()
128132 print ("unbind" )
129133 management .unbind (bind_name )
130134
131135 print ("delete queue" )
132- # management.delete_queue(queue_name)
136+ management .delete_queue (queue_name )
133137
134138 print ("delete exchange" )
135139 management .delete_exchange (exchange_name )
136140
137141 print ("closing connections" )
138142 management .close ()
139- # consumer.close()
140143 print ("after management closing" )
141144 connection .close ()
142145 print ("after connection closing" )
0 commit comments