0

I'm not an experienced Java developer. I'm trying to send an Object Message using JMS to ActiveMQ. The message is failing to process in AMQ with exception that local class serial version doesn't match with stream classdesc. I have explicitly defined the serialVersionUID in my serialize class. Don't know what is the issue. Please guide me with this.

Exception: javax.jms.JMSException: Failed to build body from bytes. Reason: java.io.InvalidClassException: com.mypkg.PaymentMessage; local class incompatible: stream classdesc serialVersionUID = 10020120822, local class serialVersionUID = 8437680918200181

PaymentMessage.java:

public class PaymentMessage implements Serializable {
    private static final long serialVersionUID=10020120822L;
    private String mymsg;
    
    public String getPaymentMessage()
    {
        return mymsg;
    }

    public void setPaymentMessage(String mymsg)
    {
        this.mymsg=mymsg;
    }
}

jmsSend.java:

public class jmsSend {
    public static void main(String args[]) {
        String strmsg = "Sample message";
        try {
            ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://XXXX");
            Connection connection = cf.createConnection();
            connection.start();
            Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
            Queue queue = session.createQueue("myqueue");
            MessageProducer producer = session.createProducer(queue);
            PaymentMessage msg = new PaymentMessage();
            msg.setPaymentMessage(strmsg);
            ObjectMessage objmsg = session.createObjectMessage(msg);
            producer.send(objmsg);
            session.close();
        } finally {
            if (connection!=null) {
                connection.close();
            }
        }
    }
}

EDIT - The above error is from the consumer which validates/processes the messages. The application has its own java client to generate test messages which won't be supported in future. So trying to create and send messages manually. The server side jars won't match to my local but I tried to name the packages as same and copied the serialVersionUID from the original client.

J Jena
  • 51
  • 3
  • Also, is there a specific reason you're using a JMS `ObjectMessage`? `ObjectMessage` objects depend on Java serialization to marshal and unmarshal their object payload. This process is **slow** and generally considered unsafe because a malicious payload can exploit the host system. [Lots of CVEs](https://cve.mitre.org/cgi-bin/cvekey.cgi?keyword=objectmessage) have been created for this. There are also a number of other issues with using JMS `ObjectMessage` not related to security that you should [read about](http://jmesnil.net/weblog/2012/07/27/on-jms-objectmessage-and-its-pitfalls/). – Justin Bertram Feb 14 '21 at 21:54
  • @JustinBertram - The error is from consumer. This is an existing application beyond my knowledge, sitting around for ages. Can't comment on why object message. – J Jena Feb 15 '21 at 12:39

1 Answers1

1

This error message is from the Java object serialization handling. The message indicates that the consuming application is using a different version of the class then what was used by the producer.

Java will throw an error if it detects a difference in the serialVersionUID value of the serialized bytes of the object, vs the serialVersionUID value of the named class in the current classpath.

Potential causes:

  • Duplicate jar in the consumer classpath, and the undesirable version of the class is resolving first.
  • Older version of the jar in the consumer classpath
  • Older version of the jar in local Maven repository (and then the classpath)
  • Older version of the jar in a remote Maven repository (and then the classpath)
Matt Pavlovich
  • 4,087
  • 1
  • 9
  • 17
  • The error is from the consumer which validates the message. To add some context, the application uses its own java client to generate the messages for testing purpose. The client won't be supported in new version; so trying to send messages manually. The consumer jar and producer jar are different in my case. I copied the serialVersionUID from the original client. Previously, consumer had error that msg body is not an instance of XXX. When I renamed my package/class, that error was gone. Is there any workaround I can make it work with my locally created msg producer? – J Jena Feb 15 '21 at 12:29
  • JMS ObjectMessages use Java's built-in object serialization, so all those rules apply. The fully qualified class name (package name + simple class name) and the serialVersionUID must match-- as well as all the serializable member fields of the class. In other words-- Java serialization has a strict compatibility matching requirement. – Matt Pavlovich Feb 15 '21 at 18:59
  • “Serialization members must match” this could be the issue because I used the final output msg body that the original client sends to AMQ. Looks like I have to reverse engineer the client to match the members. – J Jena Feb 16 '21 at 03:13