Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending complex object via JMS in Java

Is it possible to send complex messages via JMS? I can send TextMessages, Messages etc .. but when I try to send my custom object type MyObject trough send() method of MessageProducer I get compile error.

Then I tried to cast it, I get cast exception like MyObject cannot be cast to javax.jms.Message

Here is a code I tried :

MessageProducer messageProducer = session.createProducer(destination);
messageProducer.send((Message)getMyObject()); //where getMyObject method retrieves mapped myObject type

anyone got any advice? thank you

like image 683
ant Avatar asked Jun 22 '10 16:06

ant


People also ask

Does JMS support JSON?

Yes, in JMS message(body), you could send the JSON.

Which interface controls how Java objects are converted to and from a JMS message?

The overloaded methods convertAndSend and receiveAndConvert in JmsTemplate delegate the conversion process to an instance of the MessageConverter interface. This interface defines a simple contract to convert between Java objects and JMS messages.


1 Answers

As long as your object is Serializable, you can use an ObjectMessage

MessageProducer producer = session.createProducer( destination );
ObjectMessage message = session.createObjectMessage( getMyObject() );
producer.send( message );
like image 89
jimr Avatar answered Oct 21 '22 21:10

jimr