Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Message from RabbitMQ into string/json

I am currently struggling hard with a fair simple problem. I want to receive a message from RabbitMQ and have that transformed into a string (or later a json object). But all I get is bytes.

The Message object displays itself as a string that way

(Body:'{"cityId":644}'; ID:null; Content:application/json; Headers:{}; Exchange:; RoutingKey:pages.type.index; Reply:null; DeliveryMode:NON_PERSISTENT; DeliveryTag:1)

The configuration class (using spring)

@Configuration
public class RabbitConfiguration {

    @Bean
    public CachingConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory("www.example.com");
        connectionFactory.setUsername("xxxx");
        connectionFactory.setPassword("xxxx");
        return connectionFactory;
    }

    @Bean
    public MessageConverter jsonMessageConverter(){
        JsonMessageConverter jsonMessageConverter = new JsonMessageConverter();
        return jsonMessageConverter;
    }

    @Bean
    public SimpleMessageListenerContainer messageListenerContainer(){
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory());
        container.setAutoStartup(false);
        container.setQueues(indexQueue());
        container.setConcurrentConsumers(1);
        container.setAcknowledgeMode(AcknowledgeMode.AUTO);
        container.setMessageListener(new MessageListenerAdapter(pageListener(), jsonMessageConverter()));
        return container;
    }

    @Bean
    public Queue indexQueue(){
        return new Queue("pages.type.index");
    }

    @Bean
    public MessageListener pageListener(){
        return new PageQueueListener();
    }

}

and the message listener

public class PageQueueListener implements MessageListener {

    public void onMessage(Message message) {
        System.out.println(message);
        System.out.println(message.getBody());
    }
 }

my problem is, that the getBody() method displayes [B@4dbb73b0 so nothing is ever converted. Neither to a string nor to a json object :(

I feel stupid, but I cannot find a solution here

like image 643
MatthiasLaug Avatar asked Oct 05 '12 14:10

MatthiasLaug


People also ask

Does RabbitMQ support JSON?

RabbitMQ does not validate or use these fields, it exists for applications and plugins to use and interpret. For example, messages with JSON payload should use application/json. If the payload is compressed with the LZ77 (GZip) algorithm, its content encoding should be gzip.

How do I get RabbitMQ data?

In the Managed Service for ClickHouse cluster, create a table on the RabbitMQ engine. Send the test data to the RabbitMQ queue. Check that the test data is present in the Managed Service for ClickHouse cluster table. Delete the resources you created.

What is payload in RabbitMQ?

The payload is the data that you want to transmit. The label (routing key) describes the payload and the RabbitMQ messaging system uses this to determine who will receive a copy of your message. Unlike TCP — where you need to specify the sender and receiver — AMQP only describes the message with a label.


2 Answers

message.getBody() returns a byte[]

Try:

byte[] body = message.getBody();
System.out.println(new String(body));
like image 181
slim Avatar answered Oct 18 '22 11:10

slim


If you want to parse to a JSONObject, the best way is add the RabbitMQ message to a StringBuilder in String format. Then parse the StringBuilder into a JSONObject, by using any of the conversion utils.
For e.g.:

StringBuilder sb = new StringBuilder();
sb.append(publisher.toString());
payload = (JSONObject)jsonParser.parse(sb.toString());
like image 40
Eranjene S Avatar answered Oct 18 '22 11:10

Eranjene S