1 18 package org.apache.activemq.usecases; 19 20 import javax.jms.Connection ; 21 import javax.jms.JMSException ; 22 import javax.jms.Message ; 23 import javax.jms.TextMessage ; 24 25 import org.apache.activemq.ActiveMQConnection; 26 27 30 public class ConsumeTopicPrefetchTest extends ProducerConsumerTestSupport { 31 32 protected int prefetchSize = 100; 33 protected String [] messageTexts; 34 protected long consumerTimeout = 10000L; 35 36 public void testSendPrefetchSize() throws JMSException { 37 testWithMessageCount(prefetchSize); 38 } 39 40 public void testSendDoublePrefetchSize() throws JMSException { 41 testWithMessageCount(prefetchSize * 2); 42 } 43 44 public void testSendPrefetchSizePlusOne() throws JMSException { 45 testWithMessageCount(prefetchSize + 1); 46 } 47 48 protected void testWithMessageCount(int messageCount) throws JMSException { 49 makeMessages(messageCount); 50 51 log.info("About to send and receive: " + messageCount + " on destination: " + destination 52 + " of type: " + destination.getClass().getName()); 53 54 for (int i = 0; i < messageCount; i++) { 55 Message message = session.createTextMessage(messageTexts[i]); 56 producer.send(message); 57 } 58 59 for (int i = 0; i < messageCount; i++) { 61 consumeMessge(i); 62 } 63 } 64 65 protected Connection createConnection() throws Exception { 66 ActiveMQConnection connection = (ActiveMQConnection) super.createConnection(); 67 connection.getPrefetchPolicy().setQueuePrefetch(prefetchSize); 68 connection.getPrefetchPolicy().setTopicPrefetch(prefetchSize); 69 return connection; 70 } 71 72 protected void consumeMessge(int i) throws JMSException { 73 Message message = consumer.receive(consumerTimeout); 74 assertTrue("Should have received a message by now for message: " + i, message != null); 75 assertTrue("Should be a TextMessage: " + message, message instanceof TextMessage ); 76 TextMessage textMessage = (TextMessage ) message; 77 assertEquals("Message content", messageTexts[i], textMessage.getText()); 78 } 79 80 81 protected void makeMessages(int messageCount) { 82 messageTexts = new String [messageCount]; 83 for (int i = 0; i < messageCount; i++) { 84 messageTexts[i] = "Message for test: + " + getName() + " = " + i; 85 } 86 } 87 88 } 89 | Popular Tags |