Search code examples
javaspringrabbitmqspring-amqpspring-rabbit

What's the earliest point of entry to read a rabbit message in spring-amqp?


I store thread-local rabbit message data in an MDC. I would like to clear the old and add new context data for an incoming rabbit message, like reading certain values from the headers or reading the rabbit message payload as a byte[]. Unfortunately I often see exceptions happening prior to the message hitting my @RabbitHandler annotated methods. Is there an earlier entry-point that I can hook into to establish this context? I don't know what happens before deserialization occurs, but ideally I'd like access to the message before attempting to deserialize it. Perhaps there's an onMessageReceived(byte[] message, Map headers) method hook somewhere. The earlier in the call stack the better.


Solution

  • The @RabbitHandler is populated by the AbstractRabbitListenerContainerFactory which can be supplied with the custom MessageConverter: https://docs.spring.io/spring-amqp/docs/2.0.1.RELEASE/reference/html/_reference.html#message-converters. Its fromMessage() is called from the MessagingMessageListenerAdapter.toMessagingMessage(). And that is done in the MessagingMessageListenerAdapter.onMessage(). That's indeed very early place you can hook. And you really there still have a raw org.springframework.amqp.core.Message object without any conversion and with all available headers and properties.

    Well, you also can inject:

    /**
     * @param afterReceivePostProcessors the post processors.
     * @see AbstractMessageListenerContainer#setAfterReceivePostProcessors(MessagePostProcessor...)
     */
    public void setAfterReceivePostProcessors(MessagePostProcessor... afterReceivePostProcessors) {
    

    With similar reason you are requesting.