|
| 1 | +# type: ignore |
| 2 | + |
| 3 | + |
| 4 | +from rabbitmq_amqp_python_client import ( # SSlConfigurationContext,; SslConfigurationContext,; ClientCert, |
| 5 | + AddressHelper, |
| 6 | + AMQPMessagingHandler, |
| 7 | + BindingSpecification, |
| 8 | + ClientCert, |
| 9 | + Connection, |
| 10 | + Event, |
| 11 | + ExchangeSpecification, |
| 12 | + Message, |
| 13 | + QuorumQueueSpecification, |
| 14 | + SslConfigurationContext, |
| 15 | +) |
| 16 | + |
| 17 | +messages_to_publish = 100 |
| 18 | + |
| 19 | + |
| 20 | +class MyMessageHandler(AMQPMessagingHandler): |
| 21 | + |
| 22 | + def __init__(self): |
| 23 | + super().__init__() |
| 24 | + self._count = 0 |
| 25 | + |
| 26 | + def on_message(self, event: Event): |
| 27 | + print("received message: " + str(event.message.body)) |
| 28 | + |
| 29 | + # accepting |
| 30 | + self.delivery_context.accept(event) |
| 31 | + |
| 32 | + # in case of rejection (+eventually deadlettering) |
| 33 | + # self.delivery_context.discard(event) |
| 34 | + |
| 35 | + # in case of requeuing |
| 36 | + # self.delivery_context.requeue(event) |
| 37 | + |
| 38 | + # annotations = {} |
| 39 | + # annotations[symbol('x-opt-string')] = 'x-test1' |
| 40 | + # in case of requeuing with annotations added |
| 41 | + # self.delivery_context.requeue_with_annotations(event, annotations) |
| 42 | + |
| 43 | + # in case of rejection with annotations added |
| 44 | + # self.delivery_context.discard_with_annotations(event) |
| 45 | + |
| 46 | + print("count " + str(self._count)) |
| 47 | + |
| 48 | + self._count = self._count + 1 |
| 49 | + |
| 50 | + if self._count == messages_to_publish: |
| 51 | + print("closing receiver") |
| 52 | + # if you want you can add cleanup operations here |
| 53 | + # event.receiver.close() |
| 54 | + # event.connection.close() |
| 55 | + |
| 56 | + def on_connection_closed(self, event: Event): |
| 57 | + # if you want you can add cleanup operations here |
| 58 | + print("connection closed") |
| 59 | + |
| 60 | + def on_link_closed(self, event: Event) -> None: |
| 61 | + # if you want you can add cleanup operations here |
| 62 | + print("link closed") |
| 63 | + |
| 64 | + |
| 65 | +def create_connection() -> Connection: |
| 66 | + # in case of SSL enablement |
| 67 | + ca_cert_file = ".ci/certs/ca_certificate.pem" |
| 68 | + client_cert = ".ci/certs/client_certificate.pem" |
| 69 | + client_key = ".ci/certs/client_key.pem" |
| 70 | + connection = Connection( |
| 71 | + "amqps://guest:guest@localhost:5671/", |
| 72 | + ssl_context=SslConfigurationContext( |
| 73 | + ca_cert=ca_cert_file, |
| 74 | + client_cert=ClientCert(client_cert=client_cert, client_key=client_key), |
| 75 | + ), |
| 76 | + ) |
| 77 | + connection.dial() |
| 78 | + |
| 79 | + return connection |
| 80 | + |
| 81 | + |
| 82 | +def main() -> None: |
| 83 | + |
| 84 | + exchange_name = "test-exchange" |
| 85 | + queue_name = "example-queue" |
| 86 | + routing_key = "routing-key" |
| 87 | + |
| 88 | + print("connection to amqp server") |
| 89 | + connection = create_connection() |
| 90 | + |
| 91 | + management = connection.management() |
| 92 | + |
| 93 | + print("declaring exchange and queue") |
| 94 | + management.declare_exchange(ExchangeSpecification(name=exchange_name, arguments={})) |
| 95 | + |
| 96 | + management.declare_queue( |
| 97 | + QuorumQueueSpecification(name=queue_name) |
| 98 | + # QuorumQueueSpecification(name=queue_name, dead_letter_exchange="dead-letter") |
| 99 | + ) |
| 100 | + |
| 101 | + print("binding queue to exchange") |
| 102 | + bind_name = management.bind( |
| 103 | + BindingSpecification( |
| 104 | + source_exchange=exchange_name, |
| 105 | + destination_queue=queue_name, |
| 106 | + binding_key=routing_key, |
| 107 | + ) |
| 108 | + ) |
| 109 | + |
| 110 | + addr = AddressHelper.exchange_address(exchange_name, routing_key) |
| 111 | + |
| 112 | + addr_queue = AddressHelper.queue_address(queue_name) |
| 113 | + |
| 114 | + print("create a publisher and publish a test message") |
| 115 | + publisher = connection.publisher(addr) |
| 116 | + |
| 117 | + print("purging the queue") |
| 118 | + messages_purged = management.purge_queue(queue_name) |
| 119 | + |
| 120 | + print("messages purged: " + str(messages_purged)) |
| 121 | + # management.close() |
| 122 | + |
| 123 | + # publish 10 messages |
| 124 | + for i in range(messages_to_publish): |
| 125 | + status = publisher.publish(Message(body="test")) |
| 126 | + if status.ACCEPTED: |
| 127 | + print("message accepted") |
| 128 | + elif status.RELEASED: |
| 129 | + print("message not routed") |
| 130 | + elif status.REJECTED: |
| 131 | + print("message not rejected") |
| 132 | + |
| 133 | + publisher.close() |
| 134 | + |
| 135 | + print( |
| 136 | + "create a consumer and consume the test message - press control + c to terminate to consume" |
| 137 | + ) |
| 138 | + consumer = connection.consumer(addr_queue, handler=MyMessageHandler()) |
| 139 | + |
| 140 | + try: |
| 141 | + consumer.run() |
| 142 | + except KeyboardInterrupt: |
| 143 | + pass |
| 144 | + |
| 145 | + print("cleanup") |
| 146 | + consumer.close() |
| 147 | + # once we finish consuming if we close the connection we need to create a new one |
| 148 | + # connection = create_connection() |
| 149 | + # management = connection.management() |
| 150 | + |
| 151 | + print("unbind") |
| 152 | + management.unbind(bind_name) |
| 153 | + |
| 154 | + print("delete queue") |
| 155 | + management.delete_queue(queue_name) |
| 156 | + |
| 157 | + print("delete exchange") |
| 158 | + management.delete_exchange(exchange_name) |
| 159 | + |
| 160 | + print("closing connections") |
| 161 | + management.close() |
| 162 | + print("after management closing") |
| 163 | + connection.close() |
| 164 | + print("after connection closing") |
| 165 | + |
| 166 | + |
| 167 | +if __name__ == "__main__": |
| 168 | + main() |
0 commit comments