Forum Stats

  • 3,757,818 Users
  • 2,251,272 Discussions


Serializing & Deserializing objects through ObjectMessage

886634 Member Posts: 14
edited Sep 14, 2011 3:49PM in Java Message Service (JMS)

I'm having trouble sending objects/instances of a class (Data) through the message queue. I also made Data to be serializable.
public class Data implements Serializable {
The following are snippets of code from the Message producer side:
objectMessage = mySess.createObjectMessage();
MessageProducer myMsgProducer = mySess.createProducer(myQueue);
Data e = new Data();
And the following are snippets of code from the message consumer:
public void onMessage(Message message) {
             if(message instanceof ObjectMessage ){
	    	ObjectMessage objMsg = (ObjectMessage) message;
	    	Data myData = (Data)objMsg.getObject();
	    catch(JMSException e){
	    	System.out.println("Exception occured: " + e.toString());
The result of attempting to cast the objMsg to a Data is an exception thrown.

i did noticed however, that when I was debugging the message consumer, that the byteArrayInputStream values for both message and objMsg are null meaning that the data is not being sent or received. Any help would be greatly appreciated, thanks!

Edited by: 883631 on Sep 13, 2011 4:09 PM

Edited by: EJP on 14/09/2011 09:22: added {noformat}
{noformat} tags. Please use them.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        


  • EJP
    EJP Member Posts: 32,920 Gold Crown
    The result of attempting to cast the objMsg to a Data is an exception thrown.
    Such as?
  • 886634
    886634 Member Posts: 14
    Sorry, the exception thrown is:

    Exception occured: com.sun.messaging.jms.MessageFormatException: [C4015]: Deserialize message failed. - cause: java.lang.ClassNotFoundException: MsgSender.Data
    Sep 14, 2011 8:53:50 AM com.sun.messaging.jmq.jmsclient.ExceptionHandler logCaughtException
    WARNING: [I500]: Caught JVM Exception: java.lang.ClassNotFoundException: MsgSender.Data

    where MsgSender is the name of the project that produces the messages to the queue.
    Data is the class that is both on the sender and receiver side, and the exception is thrown as a result of casting the object message back into a type Data.

    Data myData = (Data)objMsg.getObject();

    I hope this isn't too confusing. What I'm wondering is if there are extra steps to serializing your object or if the ObjectMessage's setObject method automatically does that for you so as long as you declare your class to implement Serializable. And if that's the case, how do you deserialize it back to the original object?

    Thank you
  • DrClap
    DrClap Member Posts: 25,479
    All you have to do is make sure that the MsgSender.Data class is in the classpath when you try to deserialize it.
  • 886634
    886634 Member Posts: 14
    Got it, thank you for your help!
This discussion has been closed.