1 18 package org.apache.activemq; 19 20 import java.util.Enumeration ; 21 22 import javax.jms.Message ; 23 import javax.jms.MessageConsumer ; 24 import javax.jms.MessageProducer ; 25 import javax.jms.Queue ; 26 import javax.jms.QueueBrowser ; 27 import javax.jms.Session ; 28 import javax.jms.TextMessage ; 29 30 import org.apache.activemq.command.ActiveMQQueue; 31 32 35 public class JmsQueueBrowserTest extends JmsTestSupport { 36 private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory 37 .getLog(JmsQueueBrowserTest.class); 38 39 40 46 public void testReceiveBrowseReceive() throws Exception { 47 48 Session session = connection.createSession(false, 0); 49 ActiveMQQueue destination = new ActiveMQQueue("TEST"); 50 MessageProducer producer = session.createProducer(destination); 51 MessageConsumer consumer = session.createConsumer(destination); 52 connection.start(); 53 54 Message[] outbound = new Message[]{session.createTextMessage("First Message"), 55 session.createTextMessage("Second Message"), 56 session.createTextMessage("Third Message")}; 57 58 while (consumer.receive(1000) != null) { 60 } 61 62 producer.send(outbound[0]); 63 producer.send(outbound[1]); 64 producer.send(outbound[2]); 65 66 assertEquals(outbound[0], consumer.receive(1000)); 68 consumer.close(); 69 71 QueueBrowser browser = session.createBrowser((Queue ) destination); 72 Enumeration enumeration = browser.getEnumeration(); 73 74 assertTrue("should have received the second message", enumeration.hasMoreElements()); 76 assertEquals(outbound[1], (Message) enumeration.nextElement()); 77 78 assertTrue("Should have received the third message", enumeration.hasMoreElements()); 80 assertEquals(outbound[2], (Message) enumeration.nextElement()); 81 82 boolean tooMany = false; 84 while (enumeration.hasMoreElements()) { 85 log.info("Got extra message: " + ((TextMessage ) enumeration.nextElement()).getText()); 86 tooMany = true; 87 } 88 assertFalse(tooMany); 89 browser.close(); 90 91 consumer = session.createConsumer(destination); 93 assertEquals(outbound[1], consumer.receive(1000)); 95 assertEquals(outbound[2], consumer.receive(1000)); 97 consumer.close(); 98 99 } 100 } 101 | Popular Tags |