14

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

ant
  • 22,634
  • 36
  • 132
  • 182

2 Answers2

21

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 );
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
jimr
  • 11,080
  • 1
  • 33
  • 32
0

You have one of two problems:

  1. MyObject does not implement javax.jms.Message
  2. getMyObject does not return a MyObject (assuming that it does implement Message)
G__
  • 7,003
  • 5
  • 36
  • 54