|
| 1 | +# type: ignore |
| 2 | + |
| 3 | + |
| 4 | +import time |
| 5 | + |
| 6 | +from rabbitmq_amqp_python_client import ( |
| 7 | + AddressHelper, |
| 8 | + AMQPMessagingHandler, |
| 9 | + BindingSpecification, |
| 10 | + Connection, |
| 11 | + ConnectionClosed, |
| 12 | + Event, |
| 13 | + ExchangeSpecification, |
| 14 | + Message, |
| 15 | + QuorumQueueSpecification, |
| 16 | +) |
| 17 | + |
| 18 | +connection = None |
| 19 | +management = None |
| 20 | +publisher = None |
| 21 | +consumer = None |
| 22 | + |
| 23 | + |
| 24 | +def on_disconnected(): |
| 25 | + |
| 26 | + print("disconnected") |
| 27 | + exchange_name = "test-exchange" |
| 28 | + queue_name = "example-queue" |
| 29 | + routing_key = "routing-key" |
| 30 | + |
| 31 | + global connection |
| 32 | + global management |
| 33 | + global publisher |
| 34 | + global consumer |
| 35 | + |
| 36 | + addr = AddressHelper.exchange_address(exchange_name, routing_key) |
| 37 | + addr_queue = AddressHelper.queue_address(queue_name) |
| 38 | + |
| 39 | + connection = create_connection() |
| 40 | + if management is not None: |
| 41 | + management = connection.management() |
| 42 | + if publisher is not None: |
| 43 | + publisher = connection.publisher(addr) |
| 44 | + if consumer is not None: |
| 45 | + consumer = connection.consumer(addr_queue, handler=MyMessageHandler()) |
| 46 | + |
| 47 | + |
| 48 | +class MyMessageHandler(AMQPMessagingHandler): |
| 49 | + |
| 50 | + def __init__(self): |
| 51 | + super().__init__() |
| 52 | + self._count = 0 |
| 53 | + |
| 54 | + def on_message(self, event: Event): |
| 55 | + print("received message: " + str(event.message.annotations)) |
| 56 | + |
| 57 | + # accepting |
| 58 | + self.delivery_context.accept(event) |
| 59 | + |
| 60 | + # in case of rejection (+eventually deadlettering) |
| 61 | + # self.delivery_context.discard(event) |
| 62 | + |
| 63 | + # in case of requeuing |
| 64 | + # self.delivery_context.requeue(event) |
| 65 | + |
| 66 | + # annotations = {} |
| 67 | + # annotations[symbol('x-opt-string')] = 'x-test1' |
| 68 | + # in case of requeuing with annotations added |
| 69 | + # self.delivery_context.requeue_with_annotations(event, annotations) |
| 70 | + |
| 71 | + # in case of rejection with annotations added |
| 72 | + # self.delivery_context.discard_with_annotations(event) |
| 73 | + |
| 74 | + print("count " + str(self._count)) |
| 75 | + |
| 76 | + self._count = self._count + 1 |
| 77 | + |
| 78 | + if self._count == 100: |
| 79 | + print("closing receiver") |
| 80 | + # if you want you can add cleanup operations here |
| 81 | + # event.receiver.close() |
| 82 | + # event.connection.close() |
| 83 | + |
| 84 | + def on_connection_closed(self, event: Event): |
| 85 | + # if you want you can add cleanup operations here |
| 86 | + print("connection closed") |
| 87 | + |
| 88 | + def on_link_closed(self, event: Event) -> None: |
| 89 | + # if you want you can add cleanup operations here |
| 90 | + print("link closed") |
| 91 | + |
| 92 | + |
| 93 | +def create_connection() -> Connection: |
| 94 | + connection = Connection( |
| 95 | + "amqp://guest:guest@localhost:5672/", on_disconnection_handler=on_disconnected |
| 96 | + ) |
| 97 | + connection.dial() |
| 98 | + |
| 99 | + return connection |
| 100 | + |
| 101 | + |
| 102 | +def main() -> None: |
| 103 | + |
| 104 | + exchange_name = "test-exchange" |
| 105 | + queue_name = "example-queue" |
| 106 | + routing_key = "routing-key" |
| 107 | + messages_to_publish = 50000 |
| 108 | + |
| 109 | + global connection |
| 110 | + global management |
| 111 | + global publisher |
| 112 | + global consumer |
| 113 | + |
| 114 | + print("connection to amqp server") |
| 115 | + if connection is None: |
| 116 | + connection = create_connection() |
| 117 | + |
| 118 | + if management is None: |
| 119 | + management = connection.management() |
| 120 | + |
| 121 | + print("declaring exchange and queue") |
| 122 | + management.declare_exchange(ExchangeSpecification(name=exchange_name, arguments={})) |
| 123 | + |
| 124 | + management.declare_queue( |
| 125 | + QuorumQueueSpecification(name=queue_name) |
| 126 | + # QuorumQueueSpecification(name=queue_name, dead_letter_exchange="dead-letter") |
| 127 | + ) |
| 128 | + |
| 129 | + print("binding queue to exchange") |
| 130 | + bind_name = management.bind( |
| 131 | + BindingSpecification( |
| 132 | + source_exchange=exchange_name, |
| 133 | + destination_queue=queue_name, |
| 134 | + binding_key=routing_key, |
| 135 | + ) |
| 136 | + ) |
| 137 | + |
| 138 | + addr = AddressHelper.exchange_address(exchange_name, routing_key) |
| 139 | + |
| 140 | + addr_queue = AddressHelper.queue_address(queue_name) |
| 141 | + |
| 142 | + print("create a publisher and publish a test message") |
| 143 | + if publisher is None: |
| 144 | + publisher = connection.publisher(addr) |
| 145 | + |
| 146 | + print("purging the queue") |
| 147 | + messages_purged = management.purge_queue(queue_name) |
| 148 | + |
| 149 | + print("messages purged: " + str(messages_purged)) |
| 150 | + # management.close() |
| 151 | + |
| 152 | + # publish 10 messages |
| 153 | + while True: |
| 154 | + for i in range(messages_to_publish): |
| 155 | + |
| 156 | + if i % 1000 == 0: |
| 157 | + print("publishing") |
| 158 | + try: |
| 159 | + publisher.publish(Message(body="test")) |
| 160 | + except ConnectionClosed: |
| 161 | + print("publisher closing exception, resubmitting") |
| 162 | + continue |
| 163 | + |
| 164 | + print("closing") |
| 165 | + try: |
| 166 | + publisher.close() |
| 167 | + except ConnectionClosed: |
| 168 | + print("publisher closing exception, resubmitting") |
| 169 | + continue |
| 170 | + break |
| 171 | + |
| 172 | + print( |
| 173 | + "create a consumer and consume the test message - press control + c to terminate to consume" |
| 174 | + ) |
| 175 | + if consumer is None: |
| 176 | + consumer = connection.consumer(addr_queue, handler=MyMessageHandler()) |
| 177 | + |
| 178 | + while True: |
| 179 | + try: |
| 180 | + consumer.run() |
| 181 | + except KeyboardInterrupt: |
| 182 | + pass |
| 183 | + except ConnectionClosed: |
| 184 | + time.sleep(1) |
| 185 | + continue |
| 186 | + except Exception as e: |
| 187 | + print("consumer exited for exception " + str(e)) |
| 188 | + |
| 189 | + break |
| 190 | + |
| 191 | + print("cleanup") |
| 192 | + consumer.close() |
| 193 | + # once we finish consuming if we close the connection we need to create a new one |
| 194 | + # connection = create_connection() |
| 195 | + # management = connection.management() |
| 196 | + |
| 197 | + print("unbind") |
| 198 | + management.unbind(bind_name) |
| 199 | + |
| 200 | + print("delete queue") |
| 201 | + management.delete_queue(queue_name) |
| 202 | + |
| 203 | + print("delete exchange") |
| 204 | + management.delete_exchange(exchange_name) |
| 205 | + |
| 206 | + print("closing connections") |
| 207 | + management.close() |
| 208 | + print("after management closing") |
| 209 | + connection.close() |
| 210 | + print("after connection closing") |
| 211 | + |
| 212 | + |
| 213 | +if __name__ == "__main__": |
| 214 | + main() |
0 commit comments