Is there a way I can build the JMS message as JSON type. I need to create a JSON message for testing. There are a lot of attributes so i don't want to set them individually and create object and then convert it to JSON. Any other way (say) I read the json file and create a Message and then send to a JMS Queue.
Asked
Active
Viewed 8,257 times
1 Answers
1
Yes, in JMS message(body), you could send the JSON.
MessageProducer producer = session.createProducer(destination);
// We will send a small text message saying 'Hello World!'
TextMessage message = session.createTextMessage(" { "\name"\:"\John"\, "\age"\:31, "\city"\:"\New York"\ }");
// Here we are sending the message!
producer.send(message);
System.out.println("Sent message '" + message.getText() + "'");

Red Boy
- 5,429
- 3
- 28
- 41
-
I tried this way already. The problem is that on listening this message when I try to convert it to Object using jackson library it throws Class Cast exception as textmessage can't be converted to the object. The jackson is defined as: @Bean // Serialize message content to json using TextMessage public MessageConverter jacksonJmsMessageConverter() { MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter(); converter.setTargetType(MessageType.TEXT); converter.setTypeIdPropertyName("DomainClassName"); return converter; – Megan May 11 '18 at 00:20
-
1Its different problem now, you have sent JSON Message, all is well on message produce side. On the consumer end you have to do following, `Message message = consumer.receive(1000); if (message instanceof TextMessage) { TextMessage textMessage = (TextMessage) message; String text = textMessage.getText(); System.out.println("Received: " + text); } else { System.out.println("Received: " + message); }` . Now, you have JSON Test in text, do Jackson magic now. – Red Boy May 11 '18 at 10:09