How to read headers in RabbitMQ in Java?
Asked Answered
S

4

6

I have some properties that I would like to read from previously set message headers. I did this:

 Delivery delivery = consumer.nextDelivery();
 Map<String, Object> headers = delivery.getProperties().getHeaders();

Problem is, headers have weird types - like LongString for example. Is there any helper class that would allow me to easily convert headers to anything more useful?

Sueannsuede answered 24/11, 2015 at 14:35 Comment(0)
A
4

You must put Headers in your Message:

MessageProperties props = MessagePropertiesBuilder.newInstance().setContentType(MessageProperties.CONTENT_TYPE_JSON).build();
props.setHeader("headerKey1", "headerValue1");

Message msg = new Message("{'body':'value1','body2':value2}".getBytes(), props);        

rabbitTemplate.send("exchange.direct.one", new String(), msg);

For read the headers of Message inbound from Rabbit Queue:

import org.springframework.amqp.core.Message;
    import org.springframework.amqp.core.MessageListener;

    public class MessagesHandler implements MessageListener {

        public void onMessage(Message message) {
            Map<String, Object> headers = message.getMessageProperties().getHeaders();
            for (Map.Entry<String, Object> header : headers.entrySet())
            {
                System.out.println(header.getKey() + " : " + header.getValue());
            }
        }
    }
Apivorous answered 24/11, 2015 at 15:3 Comment(4)
I'm not asking how to put headers - I did that. I'm asking how to read them - since they don't have usual type(s).Sueannsuede
Show the second paragraph: "For read the headers of Message inbound from Rabbit Queue"Apivorous
Yeah, but this assumes that Object.toString() on header value is all I will ever need - are you sure this is the best / the correct way to read those values?Sueannsuede
I have similar issue.. I am putting a String into the headers before sending, but when I receive it, it has become a LongStringHelper$ByteArrayLongStringWhit
W
3

That's how I've been able to do it, I am casting to LongString and then convert to String :

protected String extractCorrelationIdFromHeaders(AMQP.BasicProperties properties) throws UnsupportedEncodingException {

    String decodedCorrelationId=null;

    if(properties.getHeaders() != null) {

        try {
            Object rawCorrelationId = properties.getHeaders().get(CORRELATION_ID_KEY);

            if(rawCorrelationId==null){
                log.info("no correlationId provided in headers");
                return null;
            }

            byte[] correlationIdAsByteArray = ((LongString) rawCorrelationId).getBytes();

            decodedCorrelationId = new String(correlationIdAsByteArray, "UTF-8");
        }
        catch(UnsupportedEncodingException e){
            log.warn("extracted correlationId, but unable to decode it",e);
        }
    }

    return decodedCorrelationId;
}

Strangely enough, I feel it's not very well documented out there. Hope this helps !

Whit answered 5/12, 2016 at 5:36 Comment(0)
H
1

I just cast to LongString and the convert toString()

String header = "foo";

String value = ((LongString) message.getProps().getHeaders().get(header)).toString();
Houk answered 27/1, 2022 at 3:54 Comment(0)
G
0

I apologize for the necroposting, but I was looking for an answer to a similar question and this works for me personally:

Object o = event.getProperties().getHeaders().get("stringHeader");

String stringHeader;
if(Objects.nonNull(o)){
   stringHeader  = o.toString();
}else {
   stringHeader  = "";
}

...some stringHeader usege 
Glut answered 17/9, 2024 at 10:48 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.