I'm facing an issue in my Spring Boot application where I'm unable to display the ObjectMessage
body. The error message I receive is:
jakarta.jms.JMSException: Failed to build body from content. Serializable class not available to broker. Reason: java.lang.ClassNotFoundException:
The code involves sending and receiving JMS messages. Could you assist me in identifying the root cause and providing a solution to resolve this serialization issue?
Here is the code snippet:
public void send(Test test) {
try {
log.info("Sending Message with JMSCorrelationID: {}", test);
ObjectMessage message = jmsTemplate.getConnectionFactory().createContext().createObjectMessage();
message.setObject(test);
jmsTemplate.convertAndSend(destinationName, message);
} catch (Exception e) {
throw new RuntimeException("Cannot send message to the Queue");
}
}
@JmsListener(destination = "yourQueueName")
public void receiveMessage(Message message) {
try {
ObjectMessage objectMessage = (ObjectMessage) message;
log.info("Received Message: " + objectMessage.getObject());
} catch (Exception e) {
log.error("Received Exception: " + e);
}
}
I appreciate your help in resolving this issue.
When using ObjectMessage
to serialize and de-serialize an object that Class needs to be on the Classpath in both the sending and receiving application. I general your project needs to include a common Jar on the receiver side that contains the class being transmitted for this to work.
ObjectMessage
has a rather checkered past in terms of security vulnerabilities and should be avoided when possible as many brokers place limits on what types you can send there without specialized configuration, you may want to consider sending payload like JSON in a TextMessage
.