From f1004c8b949cff2b74e819c257bff3521061ede2 Mon Sep 17 00:00:00 2001 From: Justin Bertram Date: Mon, 25 Oct 2021 12:01:20 -0500 Subject: [PATCH] ARTEMIS-2922 artemis-cli consumer on large message results in a ClassCastException --- .../tools/xml/XMLMessageExporter.java | 13 +++++++--- .../cli/test/MessageSerializerTest.java | 26 +++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/tools/xml/XMLMessageExporter.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/tools/xml/XMLMessageExporter.java index 5e12473fc2..b2d7b98738 100644 --- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/tools/xml/XMLMessageExporter.java +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/tools/xml/XMLMessageExporter.java @@ -25,6 +25,7 @@ import org.apache.activemq.artemis.api.core.ActiveMQException; import org.apache.activemq.artemis.api.core.ICoreMessage; import org.apache.activemq.artemis.api.core.Message; import org.apache.activemq.artemis.api.core.SimpleString; +import org.apache.activemq.artemis.core.client.impl.ClientLargeMessageImpl; import org.apache.activemq.artemis.core.message.LargeBodyReader; import org.apache.activemq.artemis.core.server.LargeServerMessage; import org.apache.activemq.artemis.reader.TextMessageUtil; @@ -58,7 +59,7 @@ public class XMLMessageExporter { xmlWriter.writeStartElement(XmlDataConstants.MESSAGE_BODY); if (message.isLargeMessage()) { - printLargeMessageBody((LargeServerMessage) message); + printLargeMessageBody(message); } else { if (encodeTextMessageUTF8 && message.toCore().getType() == Message.TEXT_TYPE) { xmlWriter.writeCData(TextMessageUtil.readBodyText(message.toCore().getReadOnlyBodyBuffer()).toString()); @@ -78,12 +79,18 @@ public class XMLMessageExporter { return chunkBytes; } - public void printLargeMessageBody(LargeServerMessage message) throws XMLStreamException { + public void printLargeMessageBody(Message message) throws XMLStreamException { xmlWriter.writeAttribute(XmlDataConstants.MESSAGE_IS_LARGE, Boolean.TRUE.toString()); LargeBodyReader encoder = null; try { - encoder = message.toMessage().toCore().getLargeBodyReader(); + if (message instanceof LargeServerMessage) { + encoder = ((LargeServerMessage)message).toMessage().toCore().getLargeBodyReader(); + } else if (message instanceof ClientLargeMessageImpl) { + encoder = ((ClientLargeMessageImpl)message).getLargeBodyReader(); + } else { + throw new RuntimeException("Unrecognized message implementation: " + message.getClass().getName()); + } encoder.open(); long totalBytesWritten = 0; int bufferSize; diff --git a/artemis-cli/src/test/java/org/apache/activemq/cli/test/MessageSerializerTest.java b/artemis-cli/src/test/java/org/apache/activemq/cli/test/MessageSerializerTest.java index 92e1567de9..2e612b0641 100644 --- a/artemis-cli/src/test/java/org/apache/activemq/cli/test/MessageSerializerTest.java +++ b/artemis-cli/src/test/java/org/apache/activemq/cli/test/MessageSerializerTest.java @@ -34,6 +34,7 @@ import java.util.List; import java.util.UUID; import org.apache.activemq.artemis.api.core.RoutingType; +import org.apache.activemq.artemis.api.core.client.ActiveMQClient; import org.apache.activemq.artemis.cli.commands.address.CreateAddress; import org.apache.activemq.artemis.cli.commands.messages.Consumer; import org.apache.activemq.artemis.cli.commands.messages.Producer; @@ -93,6 +94,17 @@ public class MessageSerializerTest extends CliTestBase { return messages; } + private List generateLargeTextMessages(Session session, Destination destination) throws Exception { + List messages = new ArrayList<>(TEST_MESSAGE_COUNT); + for (int i = 0; i < TEST_MESSAGE_COUNT; i++) { + messages.add(session.createTextMessage(new String(RandomUtil.randomBytes(ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE * 2)))); + } + + sendMessages(session, destination, messages); + + return messages; + } + private void checkSentMessages(Session session, List messages, String address) throws Exception { checkSentMessages(session, messages, address, null); } @@ -188,6 +200,20 @@ public class MessageSerializerTest extends CliTestBase { checkSentMessages(session, sent, address); } + @Test + public void testLargeMessageExport() throws Exception { + String address = "test"; + File file = createMessageFile(); + + Session session = createSession(connection); + + generateLargeTextMessages(session, getDestination(address)); + + exportMessages(address, file); + + Wait.assertTrue(() -> verifyMessageCount(address, 0), 2000, 100); + } + @Test public void testObjectMessageImportExport() throws Exception { String address = "test";