Search code examples
rabbitmqspring-cloud-streamspring-amqp

Read AMQP message headers spring cloud stream


I made spring batch application that produces amqp (rabbitmq) messages which consists of list of json objects. Messages have headers with some metadata. Spring cloud stream app is consuming messages, and I used functional approach. How can I access the headers?
Is it bad approach to use message headers for anything but routing?


Solution

  • If your function signature in Spring Cloud Stream application accepts Message (for example):

    @Bean
    public Consumer<Message<?>> consume() {
        return message -> {
            message.getHeaders()....
        };
    }
    
    

    . . then you can simply access message headers. We're also working on simplifying it a bit.

    No, it s not a bad approach to use message headers for anything but routing. Think of Message Headers as meta information, only relevant to the current message . . . anything that is important for the current message but not after.