1 17 package org.apache.activemq.command; 18 19 import java.net.URI ; 20 21 import javax.jms.JMSException ; 22 import javax.jms.MessageConsumer ; 23 import javax.jms.MessageProducer ; 24 import javax.jms.Session ; 25 26 import junit.framework.TestCase; 27 28 import org.apache.activemq.ActiveMQConnection; 29 import org.apache.activemq.ActiveMQConnectionFactory; 30 import org.apache.activemq.broker.BrokerService; 31 import org.apache.activemq.broker.TransportConnector; 32 33 public class MessageCompressionTest extends TestCase { 34 35 protected BrokerService broker; 36 private ActiveMQQueue queue; 37 private static final String BROKER_URL = "tcp://localhost:61216"; 38 39 private static final String TEXT = 41 "The quick red fox jumped over the lazy brown dog. " + 42 "The quick red fox jumped over the lazy brown dog. " + 43 "The quick red fox jumped over the lazy brown dog. " + 44 "The quick red fox jumped over the lazy brown dog. " + 45 "The quick red fox jumped over the lazy brown dog. " + 46 "The quick red fox jumped over the lazy brown dog. " + 47 "The quick red fox jumped over the lazy brown dog. " + 48 "The quick red fox jumped over the lazy brown dog. " + 49 "The quick red fox jumped over the lazy brown dog. " + 50 "The quick red fox jumped over the lazy brown dog. " + 51 "The quick red fox jumped over the lazy brown dog. " + 52 "The quick red fox jumped over the lazy brown dog. " + 53 "The quick red fox jumped over the lazy brown dog. " + 54 "The quick red fox jumped over the lazy brown dog. " + 55 "The quick red fox jumped over the lazy brown dog. " + 56 "The quick red fox jumped over the lazy brown dog. " + 57 "The quick red fox jumped over the lazy brown dog. "; 58 59 protected void setUp() throws Exception { 60 broker = new BrokerService(); 61 62 TransportConnector tc = new TransportConnector(); 63 tc.setUri(new URI (BROKER_URL)); 64 tc.setName("tcp"); 65 66 queue = new ActiveMQQueue("TEST."+System.currentTimeMillis()); 67 68 broker.addConnector(tc); 69 broker.start(); 70 71 } 72 73 protected void tearDown() throws Exception { 74 if (broker != null) { 75 broker.stop(); 76 } 77 } 78 79 public void testTextMessageCompression() throws Exception { 80 81 ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(BROKER_URL); 82 factory.setUseCompression(true); 83 sendTestMessage(factory, TEXT); 84 ActiveMQTextMessage message = receiveTestMessage(factory); 85 int compressedSize = message.getContent().getLength(); 86 87 factory = new ActiveMQConnectionFactory(BROKER_URL); 88 factory.setUseCompression(false); 89 sendTestMessage(factory, TEXT); 90 message = receiveTestMessage(factory); 91 int unCompressedSize = message.getContent().getLength(); 92 93 assertTrue("expected: compressed Size '"+compressedSize+"' < unCompressedSize '"+unCompressedSize+"'", compressedSize < unCompressedSize); 94 } 95 96 private void sendTestMessage(ActiveMQConnectionFactory factory, String message) throws JMSException { 97 ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection(); 98 Session session = connection.createSession(false, 0); 99 MessageProducer producer = session.createProducer(queue); 100 producer.send(session.createTextMessage(message)); 101 connection.close(); 102 } 103 104 private ActiveMQTextMessage receiveTestMessage(ActiveMQConnectionFactory factory) throws JMSException { 105 ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection(); 106 connection.start(); 107 Session session = connection.createSession(false, 0); 108 MessageConsumer consumer = session.createConsumer(queue); 109 ActiveMQTextMessage rc = (ActiveMQTextMessage) consumer.receive(); 110 connection.close(); 111 return rc; 112 } 113 114 115 } 124 | Popular Tags |