Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -856,8 +856,7 @@ public boolean isBodyAssignableTo(Class c) throws JMSException {
@SuppressWarnings("unchecked")
protected <T> T doGetBody(Class<T> asType) throws JMSException {
storeContent();
final ByteSequence content = getContent();
final Map<String, Object> map = content != null ? deserialize(content) : null;
final Map<String, Object> map = getContentMap();

//This implementation treats an empty map as not having a body so if empty
//we should return null as well
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,6 @@ public boolean isBodyAssignableTo(Class c) throws JMSException {

@SuppressWarnings("unchecked")
protected <T> T doGetBody(Class<T> asType) throws JMSException {
storeContent();
final ByteSequence content = getContent();
return content != null ? (T) deserialize(content) : null;
return (T) getObject();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<String, Object> body = msg.getBody(Map.class);
Map<String, Object> contentMap = msg.getContentMap();

assertSame("doGetBody() should reuse the cached map instead of deserializing again", contentMap, body);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +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;
Expand Down Expand Up @@ -174,4 +177,46 @@ 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);
}

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();
}
}

}