KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
21 import java.util.Enumeration JavaDoc;
22
23 import javax.jms.Message JavaDoc;
24 import javax.jms.MessageConsumer JavaDoc;
25 import javax.jms.MessageProducer JavaDoc;
26 import javax.jms.Queue JavaDoc;
27 import javax.jms.QueueBrowser JavaDoc;
28 import javax.jms.Session JavaDoc;
29 import javax.jms.TextMessage JavaDoc;
30
31 import org.apache.activemq.test.JmsResourceProvider;
32
33
34 /**
35  * @version $Revision: 1.2 $
36  */

37 public class JmsQueueTransactionTest extends JmsTransactionTestSupport {
38     private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
39             .getLog(JmsQueueTransactionTest.class);
40     
41     /**
42      *
43      * @see org.apache.activemq.JmsTransactionTestSupport#getJmsResourceProvider()
44      */

45     protected JmsResourceProvider getJmsResourceProvider() {
46         JmsResourceProvider p = new JmsResourceProvider();
47         p.setTopic(false);
48         return p;
49     }
50
51     /**
52      * Tests if the the connection gets reset, the messages will still be received.
53      *
54      * @throws Exception
55      */

56     public void testReceiveTwoThenCloseConnection() throws Exception JavaDoc {
57         Message[] outbound = new Message[]{
58             session.createTextMessage("First Message"),
59             session.createTextMessage("Second Message")
60         };
61
62         // lets consume any outstanding messages from previous test runs
63
while (consumer.receive(1000) != null) {
64         }
65         session.commit();
66
67         producer.send(outbound[0]);
68         producer.send(outbound[1]);
69         session.commit();
70
71         log.info("Sent 0: " + outbound[0]);
72         log.info("Sent 1: " + outbound[1]);
73
74         ArrayList JavaDoc messages = new ArrayList JavaDoc();
75         Message message = consumer.receive(1000);
76         assertEquals(outbound[0], message);
77
78         message = consumer.receive(1000);
79         assertNotNull(message);
80         assertEquals(outbound[1], message);
81         
82         // Close and reopen connection.
83
reconnect();
84         
85         // Consume again.. the previous message should
86
// get redelivered.
87
message = consumer.receive(5000);
88         assertNotNull("Should have re-received the first message again!", message);
89         messages.add(message);
90         assertEquals(outbound[0], message);
91
92         message = consumer.receive(5000);
93         assertNotNull("Should have re-received the second message again!", message);
94         messages.add(message);
95         assertEquals(outbound[1], message);
96         session.commit();
97
98         Message inbound[] = new Message[messages.size()];
99         messages.toArray(inbound);
100
101         assertTextMessagesEqual("Rollback did not work", outbound, inbound);
102     }
103     
104     /**
105      * Tests sending and receiving messages with two sessions(one for producing and another for consuming).
106      *
107      * @throws Exception
108      */

109     public void testSendReceiveInSeperateSessionTest() throws Exception JavaDoc {
110         session.close();
111         int batchCount = 10;
112
113         for (int i=0; i < batchCount; i++) {
114             //Session that sends messages
115
{
116                 Session JavaDoc session = resourceProvider.createSession(connection);
117                 MessageProducer JavaDoc producer = resourceProvider.createProducer(session, destination);
118                 //consumer = resourceProvider.createConsumer(session, destination);
119
producer.send(session.createTextMessage("Test Message: "+i));
120                 session.commit();
121                 session.close();
122             }
123             
124             //Session that consumes messages
125
{
126                 Session JavaDoc session = resourceProvider.createSession(connection);
127                 MessageConsumer JavaDoc consumer = resourceProvider.createConsumer(session, destination);
128
129                 TextMessage JavaDoc message = (TextMessage JavaDoc) consumer.receive(1000*5);
130                 assertNotNull("Received only "+i+" messages in batch ", message);
131                 assertEquals("Test Message: "+i, message.getText());
132
133                 session.commit();
134                 session.close();
135             }
136         }
137     }
138     
139     /**
140      * Tests the queue browser. Browses the messages then the consumer tries to receive them.
141      * The messages should still be in the queue even when it was browsed.
142      *
143      * @throws Exception
144      */

145     public void testReceiveBrowseReceive() throws Exception JavaDoc {
146         Message[] outbound = new Message[] { session.createTextMessage("First Message"),
147                                              session.createTextMessage("Second Message"),
148                                              session.createTextMessage("Third Message") };
149
150         // lets consume any outstanding messages from previous test runs
151
while (consumer.receive(1000) != null) {
152         }
153         session.commit();
154
155         producer.send(outbound[0]);
156         producer.send(outbound[1]);
157         producer.send(outbound[2]);
158         session.commit();
159
160         // Get the first.
161
assertEquals(outbound[0], consumer.receive(1000));
162         consumer.close();
163
164         QueueBrowser JavaDoc browser = session.createBrowser((Queue JavaDoc) destination);
165         Enumeration JavaDoc enumeration = browser.getEnumeration();
166
167         // browse the second
168
assertTrue("should have received the second message", enumeration.hasMoreElements());
169         assertEquals(outbound[1], (Message) enumeration.nextElement());
170
171         // browse the third.
172
assertTrue("Should have received the third message", enumeration.hasMoreElements());
173         assertEquals(outbound[2], (Message) enumeration.nextElement());
174
175         // There should be no more.
176
boolean tooMany = false;
177         while (enumeration.hasMoreElements()) {
178             log.info("Got extra message: " + ((TextMessage JavaDoc) enumeration.nextElement()).getText());
179             tooMany = true;
180         }
181         assertFalse(tooMany);
182         browser.close();
183
184         // Re-open the consumer.
185
consumer = resourceProvider.createConsumer(session, destination);
186         // Receive the second.
187
assertEquals(outbound[1], consumer.receive(1000));
188         // Receive the third.
189
assertEquals(outbound[2], consumer.receive(1000));
190         consumer.close();
191
192         session.commit();
193     }
194     
195 }
196
Popular Tags