-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Messenger] [AMQP] Add routing key to data passed to serializer decode. #47975
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 7.4
Are you sure you want to change the base?
Conversation
It's often useful to have the routing key available when decoding messages from AMQP. For example, sometimes the routing key contains meta information required for the processing of the message. This introduces an `extra` element which currently only contains the routing key, but there is scope to add other meta information from \AMQPEnvelope such as the exchange name for example. Fixes symfony#43039
Hi! It is possible that you can solve the problem or at least have a workaround using a Middleware in conjunction with the HandlerArgumentsStamp. For example, I have a message with some AMQP properties attached: $message = new DetectFacesMessage($file->getContent());
$this->bus->dispatch($message, [
new AmqpStamp('detect-faces', attributes: [
'content_type' => 'text/plain',
'correlation_id' => $uniqid,
'reply_to' => 'response-queue',
]),
]); And I need the content_type, correlation_id and reply_to AMQP properties in the consumer in order to perform some logic. Then, my consumer look this way: public function __invoke(
DetectFacesMessage $message,
string $replyTo,
string $correlationId,
string $contentType,
string $timestamp,
) {
} I have the AMPQ properties as a method parameters because I am using a Middleware. It reads/gets the AMPQ properties from the AMPQ Envelope. The Middleware look like this: public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
$amqpStamp = $envelope->last(AmqpReceivedStamp::class);
if ($amqpStamp) {
$replyTo = $amqpStamp->getAmqpEnvelope()->getReplyTo();
$correlationId = $amqpStamp->getAmqpEnvelope()->getCorrelationId();
$contentType = $amqpStamp->getAmqpEnvelope()->getContentType();
$timestamp = $amqpStamp->getAmqpEnvelope()->getTimestamp();
$routingKey = $amqpStamp->getAmqpEnvelope()->getRoutingKey(); // Here!
$arguments = [
$replyTo,
$correlationId,
$contentType,
$timestamp,
];
$envelope = $envelope->with(new HandlerArgumentsStamp($arguments));
}
return $stack->next()->handle($envelope, $stack);
} I think you can get the routing key in this way. Sounds good to you? |
Thanks @glengemann, I'm looking to have the routing key available in the serializer as part of decoding (I need it to work out which message to create based from the routing key). If I've understood correctly, serializer decoding (deserializing) is done before it enter into the bus and thus the middlewares. |
It's often useful to have the routing key available when decoding messages from AMQP. For example, sometimes the routing key contains meta information required for the processing of the message.
This introduces an
extra
element which currently only contains the routing key, but there is scope to add other meta information from \AMQPEnvelope such as the exchange name for example.Fixes #43039