Added test showing how a Text Message that was compressed can be received in a state where the text is not compressed but the Message compressed flag indicates it is resulting in an exception from getText() indicating an unknown compression type.

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1074725 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Timothy A. Bish 2011-02-25 22:33:19 +00:00
parent 57f5e7efa5
commit 56f8e368d5
2 changed files with 201 additions and 9 deletions

View File

@ -39,7 +39,7 @@ import org.apache.activemq.wireformat.WireFormat;
/** /**
* @openwire:marshaller code="28" * @openwire:marshaller code="28"
* *
*/ */
public class ActiveMQTextMessage extends ActiveMQMessage implements TextMessage { public class ActiveMQTextMessage extends ActiveMQMessage implements TextMessage {
@ -86,6 +86,7 @@ public class ActiveMQTextMessage extends ActiveMQMessage implements TextMessage
text = MarshallingSupport.readUTF8(dataIn); text = MarshallingSupport.readUTF8(dataIn);
dataIn.close(); dataIn.close();
setContent(null); setContent(null);
setCompressed(false);
} }
} catch (IOException ioe) { } catch (IOException ioe) {
throw JMSExceptionSupport.create(ioe); throw JMSExceptionSupport.create(ioe);
@ -135,7 +136,7 @@ public class ActiveMQTextMessage extends ActiveMQMessage implements TextMessage
* If this message body was read-only, calling this method leaves the * If this message body was read-only, calling this method leaves the
* message body in the same state as an empty body in a newly created * message body in the same state as an empty body in a newly created
* message. * message.
* *
* @throws JMSException if the JMS provider fails to clear the message body * @throws JMSException if the JMS provider fails to clear the message body
* due to some internal error. * due to some internal error.
*/ */
@ -154,16 +155,16 @@ public class ActiveMQTextMessage extends ActiveMQMessage implements TextMessage
} }
return super.getSize(); return super.getSize();
} }
public String toString() { public String toString() {
try { try {
String text = getText(); String text = getText();
if (text != null && text.length() > 63) { if (text != null && text.length() > 63) {
text = text.substring(0, 45) + "..." + text.substring(text.length() - 12); text = text.substring(0, 45) + "..." + text.substring(text.length() - 12);
HashMap<String, Object> overrideFields = new HashMap<String, Object>(); HashMap<String, Object> overrideFields = new HashMap<String, Object>();
overrideFields.put("text", text); overrideFields.put("text", text);
return super.toString(overrideFields); return super.toString(overrideFields);
} }
} catch (JMSException e) { } catch (JMSException e) {
} }
return super.toString(); return super.toString();

View File

@ -0,0 +1,191 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.usecases;
import static org.junit.Assert.*;
import java.net.URI;
import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.openmbean.CompositeData;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.broker.TransportConnector;
import org.apache.activemq.broker.jmx.QueueViewMBean;
import org.apache.activemq.command.ActiveMQMessage;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ConsumeUncompressedCompressedMessageTest {
private static final Logger LOG = LoggerFactory.getLogger(ConsumeUncompressedCompressedMessageTest.class);
private BrokerService broker;
private URI tcpUri;
ActiveMQConnectionFactory factory;
ActiveMQConnection connection;
Session session;
Queue queue;
@Before
public void setUp() throws Exception {
broker = createBroker();
broker.start();
broker.waitUntilStarted();
factory = new ActiveMQConnectionFactory(tcpUri);
factory.setUseCompression(true);
connection = (ActiveMQConnection) factory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
queue = session.createQueue("CompressionTestQueue");
}
@After
public void tearDown() throws Exception {
if(connection != null) {
connection.close();
}
broker.stop();
broker.waitUntilStopped();
}
protected BrokerService createBroker() throws Exception {
return createBroker(true);
}
protected BrokerService createBroker(boolean delete) throws Exception {
BrokerService answer = new BrokerService();
answer.setPersistent(false);
answer.setDeleteAllMessagesOnStartup(true);
answer.setSchedulerSupport(false);
answer.setUseJmx(true);
TransportConnector connector = answer.addConnector("tcp://localhost:0");
tcpUri = connector.getConnectUri();
return answer;
}
@Test
public void testBrowseAndReceiveCompressedMessages() throws Exception {
assertTrue(((ActiveMQConnection) connection).isUseCompression());
createProducerAndSendMessages(1);
QueueViewMBean queueView = getProxyToQueueViewMBean();
assertNotNull(queueView);
CompositeData[] compdatalist = queueView.browse();
if (compdatalist.length == 0) {
fail("There is no message in the queue:");
}
CompositeData cdata = compdatalist[0];
assertComplexData(0, cdata, "Text", "Test Text Message: " + 0);
assertMessageAreCorrect(1);
}
@Test
public void testReceiveAndResendWithCompressionOff() throws Exception {
assertTrue(connection.isUseCompression());
createProducerAndSendMessages(1);
MessageConsumer consumer = session.createConsumer(queue);
TextMessage message = (TextMessage) consumer.receive(5000);
assertTrue(((ActiveMQMessage) message).isCompressed());
LOG.debug("Received Message with Text = " + message.getText());
connection.setUseCompression(false);
MessageProducer producer = session.createProducer(queue);
producer.send(message);
producer.close();
message = (TextMessage) consumer.receive(5000);
LOG.debug("Received Message with Text = " + message.getText());
}
protected void assertComplexData(int messageIndex, CompositeData cdata, String name, Object expected) {
Object value = cdata.get(name);
assertEquals("Message " + messageIndex + " CData field: " + name, expected, value);
}
private void createProducerAndSendMessages(int numToSend) throws Exception {
session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(queue);
for (int i = 0; i < numToSend; i++) {
TextMessage message = session.createTextMessage("Test Text Message: " + i);
if (i != 0 && i % 10000 == 0) {
LOG.info("sent: " + i);
}
producer.send(message);
}
producer.close();
}
private QueueViewMBean getProxyToQueueViewMBean()
throws MalformedObjectNameException, JMSException {
ObjectName queueViewMBeanName = new ObjectName("org.apache.activemq"
+ ":Type=Queue,Destination=" + queue.getQueueName()
+ ",BrokerName=localhost");
QueueViewMBean proxy = (QueueViewMBean) broker.getManagementContext()
.newProxyInstance(queueViewMBeanName, QueueViewMBean.class,
true);
return proxy;
}
private void assertMessageAreCorrect(int numToReceive) throws Exception {
MessageConsumer consumer = session.createConsumer(queue);
try{
for (int i = 0; i < numToReceive; ++i) {
TextMessage message = (TextMessage) consumer.receive(5000);
assertNotNull(message);
assertEquals("Test Text Message: " + i, message.getText());
}
} finally {
consumer.close();
}
}
}