diff --git a/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/client/impl/ClientMessageImpl.java b/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/client/impl/ClientMessageImpl.java index 8068aa9438..d14c64ee31 100644 --- a/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/client/impl/ClientMessageImpl.java +++ b/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/client/impl/ClientMessageImpl.java @@ -189,7 +189,8 @@ public class ClientMessageImpl extends CoreMessage implements ClientMessageInter @Override public int getBodySize() { - return getBodyBuffer().writerIndex() - getBodyBuffer().readerIndex(); + checkEncode(); + return endOfBodyPosition - BUFFER_HEADER_SPACE; } @Override diff --git a/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/message/impl/CoreMessage.java b/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/message/impl/CoreMessage.java index 09188281d4..323d9f4dd0 100644 --- a/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/message/impl/CoreMessage.java +++ b/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/message/impl/CoreMessage.java @@ -308,7 +308,7 @@ public class CoreMessage extends RefCountMessage implements ICoreMessage { sendBuffer.readerIndex(0); } - private synchronized void checkEncode() { + protected synchronized void checkEncode() { if (!validBuffer) { encode(); } diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/ra/JMSContextTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/ra/JMSContextTest.java index 6956499e5b..658b3239fa 100644 --- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/ra/JMSContextTest.java +++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/ra/JMSContextTest.java @@ -16,8 +16,13 @@ */ package org.apache.activemq.artemis.tests.integration.ra; +import javax.jms.JMSConsumer; import javax.jms.JMSContext; +import javax.jms.JMSProducer; import javax.jms.JMSRuntimeException; +import javax.jms.MessageFormatRuntimeException; +import javax.jms.Queue; +import javax.jms.TextMessage; import javax.transaction.TransactionManager; import java.util.HashSet; import java.util.Set; @@ -143,4 +148,25 @@ public class JMSContextTest extends ActiveMQRATestBase { assertEquals(context.getSessionMode(), JMSContext.AUTO_ACKNOWLEDGE); } + @Test + public void testJMSContextConsumerThrowsMessageFormatExceptionOnMalformedBody() throws Exception { + Queue queue = createQueue(true, "ContextMalformedBodyTestQueue"); + + JMSContext context = qraConnectionFactory.createContext(); + JMSProducer producer = context.createProducer(); + + TextMessage message = context.createTextMessage("TestMessage"); + producer.send(queue, message); + + JMSConsumer consumer = context.createConsumer(queue); + + try { + consumer.receiveBody(Boolean.class); + fail("Should thrown MessageFormatException"); + } catch (MessageFormatRuntimeException mfre) { + // Do nothing test passed + } catch (Exception e) { + fail("Threw wrong exception, should be MessageFormatRuntimeException, instead got: " + e.getClass().getCanonicalName()); + } + } }