Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pass JSON object to the RabbitMQ queue using java application?

I want to pass JSON object to the RabbitMQ queue.

In the below code, I am using obj.toJSONString().getBytes() for converting Json object in to string, Is it possible to pass JSON object in the queue instead passing as string.

JSONObject obj = new JSONObject();
obj.put("Transaction","Test value");								
channel.basicPublish("", queueName, null, obj.toJSONString().getBytes()); 
System.out.println(" [x] Sent '" + obj.toJSONString() + "'");	
like image 969
Karthikeyan Velmurugan Avatar asked Nov 01 '25 08:11

Karthikeyan Velmurugan


1 Answers

We can only send the data as bytes to a rabbitmq queue. So we have to convert the json object into string. In your code snippet, you have done by using the code - obj.toJSONString().getBytes(). This is the correct approach.

like image 74
Faariz Mohammed Avatar answered Nov 03 '25 21:11

Faariz Mohammed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!