From fa7853019f96a89c652e498b098ac17e64c8fcbf Mon Sep 17 00:00:00 2001 From: Gautam Tandon Date: Sat, 12 Sep 2026 22:55:10 -0700 Subject: [PATCH 1/2] fix: ActiveMQObjectMessage/ActiveMQMapMessage doGetBody() deserializes message body twice doGetBody() in both classes re-deserializes the content directly instead of reusing the cache getObject()/getContentMap() already populate, so getBody() ends up deserializing twice. Fixed both to delegate to the cached getter, same as ActiveMQTextMessage already does. Added a test per class to catch the regression. See: https://github.com/apache/activemq/issues/2516 --- .../activemq/command/ActiveMQMapMessage.java | 3 +-- .../activemq/command/ActiveMQObjectMessage.java | 3 +-- .../command/ActiveMQMapMessageTest.java | 17 +++++++++++++++++ .../command/ActiveMQObjectMessageTest.java | 15 +++++++++++++++ 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/activemq-client/src/main/java/org/apache/activemq/command/ActiveMQMapMessage.java b/activemq-client/src/main/java/org/apache/activemq/command/ActiveMQMapMessage.java index 6f5473f659b..0a00a98a637 100644 --- a/activemq-client/src/main/java/org/apache/activemq/command/ActiveMQMapMessage.java +++ b/activemq-client/src/main/java/org/apache/activemq/command/ActiveMQMapMessage.java @@ -856,8 +856,7 @@ public boolean isBodyAssignableTo(Class c) throws JMSException { @SuppressWarnings("unchecked") protected T doGetBody(Class asType) throws JMSException { storeContent(); - final ByteSequence content = getContent(); - final Map map = content != null ? deserialize(content) : null; + final Map map = getContentMap(); //This implementation treats an empty map as not having a body so if empty //we should return null as well diff --git a/activemq-client/src/main/java/org/apache/activemq/command/ActiveMQObjectMessage.java b/activemq-client/src/main/java/org/apache/activemq/command/ActiveMQObjectMessage.java index 395dcca554c..8247ae05087 100644 --- a/activemq-client/src/main/java/org/apache/activemq/command/ActiveMQObjectMessage.java +++ b/activemq-client/src/main/java/org/apache/activemq/command/ActiveMQObjectMessage.java @@ -301,7 +301,6 @@ public boolean isBodyAssignableTo(Class c) throws JMSException { @SuppressWarnings("unchecked") protected T doGetBody(Class asType) throws JMSException { storeContent(); - final ByteSequence content = getContent(); - return content != null ? (T) deserialize(content) : null; + return (T) getObject(); } } diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/command/ActiveMQMapMessageTest.java b/activemq-unit-tests/src/test/java/org/apache/activemq/command/ActiveMQMapMessageTest.java index 6a183f5882f..2672226fd36 100644 --- a/activemq-unit-tests/src/test/java/org/apache/activemq/command/ActiveMQMapMessageTest.java +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/command/ActiveMQMapMessageTest.java @@ -19,6 +19,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; @@ -29,6 +30,7 @@ import java.util.Collections; import java.util.Enumeration; import java.util.List; +import java.util.Map; import jakarta.jms.JMSException; import jakarta.jms.MessageFormatException; @@ -515,4 +517,19 @@ private void testUnmarshalException(boolean compressed) throws Exception { ExceptionUtils.getRootCause(e) instanceof ActiveMQUnmarshalEOFException); } } + + // JMS 2.0 getBody() calls isBodyAssignableTo() (which populates the cached + // map via getContentMap()) and then doGetBody(). doGetBody() must reuse that + // cache instead of deserializing the content a second time. + @Test(timeout = 10000) + public void testGetBodyDoesNotDeserializeTwice() throws Exception { + ActiveMQMapMessage msg = new ActiveMQMapMessage(); + msg.setString("key", "value"); + msg.storeContentAndClear(); + + Map body = msg.getBody(Map.class); + Map contentMap = msg.getContentMap(); + + assertSame("doGetBody() should reuse the cached map instead of deserializing again", contentMap, body); + } } diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/command/ActiveMQObjectMessageTest.java b/activemq-unit-tests/src/test/java/org/apache/activemq/command/ActiveMQObjectMessageTest.java index 3c3cfc7106b..6af7dd44499 100644 --- a/activemq-unit-tests/src/test/java/org/apache/activemq/command/ActiveMQObjectMessageTest.java +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/command/ActiveMQObjectMessageTest.java @@ -21,6 +21,7 @@ import static org.mockito.Mockito.when; import java.io.IOException; +import java.io.Serializable; import jakarta.jms.JMSException; import jakarta.jms.MessageNotReadableException; @@ -174,4 +175,18 @@ private void testUnmarshalException(boolean compressed) throws Exception { } } + // JMS 2.0 getBody() calls isBodyAssignableTo() (which populates the cached + // object via getObject()) and then doGetBody(). doGetBody() must reuse that + // cache instead of deserializing the content a second time. + public void testGetBodyDoesNotDeserializeTwice() throws Exception { + ActiveMQObjectMessage msg = new ActiveMQObjectMessage(); + msg.setObject("value"); + msg.storeContentAndClear(); + + Serializable body = msg.getBody(Serializable.class); + Serializable object = msg.getObject(); + + assertSame("doGetBody() should reuse the cached object instead of deserializing again", object, body); + } + } From 7c5c9b887918d4b75d79bdf0a99dcb05a6fe9b49 Mon Sep 17 00:00:00 2001 From: Gautam Tandon Date: Mon, 16 Nov 2026 21:57:19 -0800 Subject: [PATCH 2/2] Drop redundant storeContent() in ActiveMQObjectMessage.doGetBody() getObject() already handles it. Also add a deserialization-count test suggested in review. --- .../command/ActiveMQObjectMessage.java | 1 - .../command/ActiveMQObjectMessageTest.java | 30 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/activemq-client/src/main/java/org/apache/activemq/command/ActiveMQObjectMessage.java b/activemq-client/src/main/java/org/apache/activemq/command/ActiveMQObjectMessage.java index 8247ae05087..dc2b9d61192 100644 --- a/activemq-client/src/main/java/org/apache/activemq/command/ActiveMQObjectMessage.java +++ b/activemq-client/src/main/java/org/apache/activemq/command/ActiveMQObjectMessage.java @@ -300,7 +300,6 @@ public boolean isBodyAssignableTo(Class c) throws JMSException { @SuppressWarnings("unchecked") protected T doGetBody(Class asType) throws JMSException { - storeContent(); return (T) getObject(); } } diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/command/ActiveMQObjectMessageTest.java b/activemq-unit-tests/src/test/java/org/apache/activemq/command/ActiveMQObjectMessageTest.java index 6af7dd44499..cb4d14f4dcc 100644 --- a/activemq-unit-tests/src/test/java/org/apache/activemq/command/ActiveMQObjectMessageTest.java +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/command/ActiveMQObjectMessageTest.java @@ -21,7 +21,9 @@ import static org.mockito.Mockito.when; import java.io.IOException; +import java.io.ObjectInputStream; import java.io.Serializable; +import java.util.concurrent.atomic.AtomicInteger; import jakarta.jms.JMSException; import jakarta.jms.MessageNotReadableException; @@ -189,4 +191,32 @@ public void testGetBodyDoesNotDeserializeTwice() throws Exception { assertSame("doGetBody() should reuse the cached object instead of deserializing again", object, body); } + public void testGetBodyDeserializesOnce() throws Exception { + ActiveMQObjectMessage msg = new ActiveMQObjectMessage(); + msg.setObject(new CountingSerializable("test")); + // as on the consumer side: only the serialized content is available + msg.storeContentAndClear(); + + CountingSerializable.READ_COUNT.set(0); + CountingSerializable body = msg.getBody(CountingSerializable.class); + assertEquals("test", body.value); + assertEquals(1, CountingSerializable.READ_COUNT.get()); + } + + static class CountingSerializable implements Serializable { + private static final long serialVersionUID = 1L; + static final AtomicInteger READ_COUNT = new AtomicInteger(); + + final String value; + + CountingSerializable(String value) { + this.value = value; + } + + private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { + in.defaultReadObject(); + READ_COUNT.incrementAndGet(); + } + } + }