|
| 1 | +# type: ignore |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import signal |
| 5 | + |
| 6 | +from rabbitmq_amqp_python_client import ( |
| 7 | + AddressHelper, |
| 8 | + AMQPMessagingHandler, |
| 9 | + AsyncEnvironment, |
| 10 | + Converter, |
| 11 | + Event, |
| 12 | + ExchangeSpecification, |
| 13 | + ExchangeToQueueBindingSpecification, |
| 14 | + Message, |
| 15 | + OutcomeState, |
| 16 | + QuorumQueueSpecification, |
| 17 | +) |
| 18 | + |
| 19 | +MESSAGES_TO_PUBLISH = 100 |
| 20 | + |
| 21 | + |
| 22 | +class StopConsumerException(Exception): |
| 23 | + """Exception to signal consumer should stop""" |
| 24 | + |
| 25 | + pass |
| 26 | + |
| 27 | + |
| 28 | +class MyMessageHandler(AMQPMessagingHandler): |
| 29 | + def __init__(self): |
| 30 | + super().__init__() |
| 31 | + self._count = 0 |
| 32 | + |
| 33 | + def on_amqp_message(self, event: Event): |
| 34 | + print( |
| 35 | + "received message: {} ".format( |
| 36 | + Converter.bytes_to_string(event.message.body) |
| 37 | + ) |
| 38 | + ) |
| 39 | + |
| 40 | + self.delivery_context.accept(event) |
| 41 | + self._count = self._count + 1 |
| 42 | + print("count " + str(self._count)) |
| 43 | + |
| 44 | + def on_connection_closed(self, event: Event): |
| 45 | + print("connection closed") |
| 46 | + |
| 47 | + def on_link_closed(self, event: Event) -> None: |
| 48 | + print("link closed") |
| 49 | + |
| 50 | + |
| 51 | +async def main(): |
| 52 | + exchange_name = "test-exchange" |
| 53 | + queue_name = "example-queue" |
| 54 | + routing_key = "routing-key" |
| 55 | + |
| 56 | + print("connection to amqp server") |
| 57 | + async with AsyncEnvironment( |
| 58 | + uri="amqp://guest:guest@localhost:5672/" |
| 59 | + ) as environment: |
| 60 | + async with await environment.connection() as connection: |
| 61 | + async with await connection.management() as management: |
| 62 | + print("declaring exchange and queue") |
| 63 | + await management.declare_exchange( |
| 64 | + ExchangeSpecification(name=exchange_name) |
| 65 | + ) |
| 66 | + await management.declare_queue( |
| 67 | + QuorumQueueSpecification(name=queue_name) |
| 68 | + ) |
| 69 | + |
| 70 | + print("binding queue to exchange") |
| 71 | + bind_name = await management.bind( |
| 72 | + ExchangeToQueueBindingSpecification( |
| 73 | + source_exchange=exchange_name, |
| 74 | + destination_queue=queue_name, |
| 75 | + binding_key=routing_key, |
| 76 | + ) |
| 77 | + ) |
| 78 | + |
| 79 | + addr = AddressHelper.exchange_address(exchange_name, routing_key) |
| 80 | + addr_queue = AddressHelper.queue_address(queue_name) |
| 81 | + |
| 82 | + print("create a publisher and publish a test message") |
| 83 | + async with await connection.publisher(addr) as publisher: |
| 84 | + print("purging the queue") |
| 85 | + messages_purged = await management.purge_queue(queue_name) |
| 86 | + print("messages purged: " + str(messages_purged)) |
| 87 | + |
| 88 | + # publish messages |
| 89 | + for i in range(MESSAGES_TO_PUBLISH): |
| 90 | + status = await publisher.publish( |
| 91 | + Message( |
| 92 | + body=Converter.string_to_bytes( |
| 93 | + "test message {} ".format(i) |
| 94 | + ) |
| 95 | + ) |
| 96 | + ) |
| 97 | + if status.remote_state == OutcomeState.ACCEPTED: |
| 98 | + print("message accepted") |
| 99 | + |
| 100 | + print( |
| 101 | + "create a consumer and consume the test message - press control + c to terminate" |
| 102 | + ) |
| 103 | + handler = MyMessageHandler() |
| 104 | + async with await connection.consumer( |
| 105 | + addr_queue, message_handler=handler |
| 106 | + ) as consumer: |
| 107 | + # Create stop event and signal handler |
| 108 | + stop_event = asyncio.Event() |
| 109 | + |
| 110 | + def handle_sigint(): |
| 111 | + print("\nCtrl+C detected, stopping consumer gracefully...") |
| 112 | + stop_event.set() |
| 113 | + |
| 114 | + # Register signal handler |
| 115 | + loop = asyncio.get_running_loop() |
| 116 | + loop.add_signal_handler(signal.SIGINT, handle_sigint) |
| 117 | + |
| 118 | + try: |
| 119 | + # Run consumer in background |
| 120 | + consumer_task = asyncio.create_task(consumer.run()) |
| 121 | + |
| 122 | + # Wait for stop signal |
| 123 | + await stop_event.wait() |
| 124 | + |
| 125 | + # Stop consumer gracefully |
| 126 | + print("Stopping consumer...") |
| 127 | + await consumer.stop_processing() |
| 128 | + |
| 129 | + # Wait for task to complete |
| 130 | + try: |
| 131 | + await asyncio.wait_for(consumer_task, timeout=3.0) |
| 132 | + except asyncio.TimeoutError: |
| 133 | + print("Consumer task timed out") |
| 134 | + |
| 135 | + finally: |
| 136 | + loop.remove_signal_handler(signal.SIGINT) |
| 137 | + |
| 138 | + print("unbind") |
| 139 | + await management.unbind(bind_name) |
| 140 | + |
| 141 | + print("delete queue") |
| 142 | + await management.delete_queue(queue_name) |
| 143 | + |
| 144 | + print("delete exchange") |
| 145 | + await management.delete_exchange(exchange_name) |
| 146 | + |
| 147 | + |
| 148 | +if __name__ == "__main__": |
| 149 | + asyncio.run(main()) |
0 commit comments