Skip to content

[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

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from

Conversation

jenkoian
Copy link
Contributor

@jenkoian jenkoian commented Oct 24, 2022

Q A
Branch? 6.2
Bug fix? no
New feature? no
Deprecations? no
Tickets Fix #43039
License MIT
Doc PR N/A

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

@carsonbot carsonbot added this to the 6.2 milestone Oct 24, 2022
@carsonbot carsonbot changed the title [Messenger][AMQP] Add routing key to data passed to serializer decode. [Messenger] [AMQP] Add routing key to data passed to serializer decode. Oct 24, 2022
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
@nicolas-grekas nicolas-grekas modified the milestones: 6.2, 6.3 Nov 5, 2022
@glengemann
Copy link

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?

@jenkoian
Copy link
Contributor Author

jenkoian commented Dec 6, 2022

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.

@nicolas-grekas nicolas-grekas modified the milestones: 6.3, 6.4 May 23, 2023
@nicolas-grekas nicolas-grekas modified the milestones: 6.4, 7.1 Nov 15, 2023
@xabbuh xabbuh added the Feature label May 15, 2024
@xabbuh xabbuh modified the milestones: 7.1, 7.2 May 15, 2024
@fabpot fabpot modified the milestones: 7.2, 7.3 Nov 20, 2024
@fabpot fabpot modified the milestones: 7.3, 7.4 May 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add routing key to header for amqp custom serializer
6 participants