From b41ca04801138b5d9cdbc1c18626736bf3195ac3 Mon Sep 17 00:00:00 2001 From: Stanislav Hejny Date: Wed, 23 Jul 2025 18:33:34 +0100 Subject: [PATCH] feat/#61 - add one way communication option --- amqp/service/amq_message_handler.py | 39 ++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/amqp/service/amq_message_handler.py b/amqp/service/amq_message_handler.py index 8b958ad..34f593c 100644 --- a/amqp/service/amq_message_handler.py +++ b/amqp/service/amq_message_handler.py @@ -297,7 +297,7 @@ class DataMessageHandler: future = asyncio.Future() # Store the future in the outstanding dictionary using the message ID as the key - message_id = message.id() + message_id = message.id().as_string() self.outstanding[message_id] = future # Create a Pika message with the correlation ID set to the message ID @@ -328,3 +328,40 @@ class DataMessageHandler: ) return future + + async def send_command(self, route, message: DataMessage) -> bool: + """ + Sends an RPC message to the specified route and returns a Future that will be completed + when the response is received. + + :param route: AMQRoute object containing exchange and routing information + :param message: DataMessage to be sent + :return: Future that will be completed with the response payload + """ + # Create a Pika message with the correlation ID set to the message ID + pika_message = Message( + body=DataMessageFactory.serialize(message), + correlation_id=message.id().as_string(), + content_type=( + message.content_type()[0] + if isinstance(message.content_type(), list) + else message.content_type() + ), + ) + + # Get the exchange from the route + rpc_exchange_name = route.exchange + exchange: AbstractRobustExchange = await self.rabbit_mq_client.channel.declare_exchange( + name=rpc_exchange_name, type=ExchangeType.topic + ) + # Publish the message to the exchange with the component name as the routing key + await exchange.publish(message=pika_message, routing_key=route.component_name) + + logging_debug( + "###### RPC Request Published to exchg:(%s) routing_key=%s, msgId=%s", + rpc_exchange_name, + route.component_name, + message.id().as_string(), + ) + + return True