KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > JmsQueueBrowserTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq;
19
20 import java.util.Enumeration JavaDoc;
21
22 import javax.jms.Message JavaDoc;
23 import javax.jms.MessageConsumer JavaDoc;
24 import javax.jms.MessageProducer JavaDoc;
25 import javax.jms.Queue JavaDoc;
26 import javax.jms.QueueBrowser JavaDoc;
27 import javax.jms.Session JavaDoc;
28 import javax.jms.TextMessage JavaDoc;
29
30 import org.apache.activemq.command.ActiveMQQueue;
31
32 /**
33  * @version $Revision: 1.4 $
34  */

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     /**
41      * Tests the queue browser. Browses the messages then the consumer tries to receive them. The messages should still
42      * be in the queue even when it was browsed.
43      *
44      * @throws Exception
45      */

46     public void testReceiveBrowseReceive() throws Exception JavaDoc {
47
48         Session JavaDoc session = connection.createSession(false, 0);
49         ActiveMQQueue destination = new ActiveMQQueue("TEST");
50         MessageProducer JavaDoc producer = session.createProducer(destination);
51         MessageConsumer JavaDoc 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         // lets consume any outstanding messages from previous test runs
59
while (consumer.receive(1000) != null) {
60         }
61
62         producer.send(outbound[0]);
63         producer.send(outbound[1]);
64         producer.send(outbound[2]);
65
66         // Get the first.
67
assertEquals(outbound[0], consumer.receive(1000));
68         consumer.close();
69         //Thread.sleep(200);
70

71         QueueBrowser JavaDoc browser = session.createBrowser((Queue JavaDoc) destination);
72         Enumeration JavaDoc enumeration = browser.getEnumeration();
73
74         // browse the second
75
assertTrue("should have received the second message", enumeration.hasMoreElements());
76         assertEquals(outbound[1], (Message) enumeration.nextElement());
77
78         // browse the third.
79
assertTrue("Should have received the third message", enumeration.hasMoreElements());
80         assertEquals(outbound[2], (Message) enumeration.nextElement());
81
82         // There should be no more.
83
boolean tooMany = false;
84         while (enumeration.hasMoreElements()) {
85             log.info("Got extra message: " + ((TextMessage JavaDoc) enumeration.nextElement()).getText());
86             tooMany = true;
87         }
88         assertFalse(tooMany);
89         browser.close();
90
91         // Re-open the consumer.
92
consumer = session.createConsumer(destination);
93         // Receive the second.
94
assertEquals(outbound[1], consumer.receive(1000));
95         // Receive the third.
96
assertEquals(outbound[2], consumer.receive(1000));
97         consumer.close();
98
99     }
100 }
101
Popular Tags