KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.framework.TestCase;
21
22 import javax.jms.BytesMessage JavaDoc;
23 import javax.jms.Connection JavaDoc;
24 import javax.jms.DeliveryMode JavaDoc;
25 import javax.jms.InvalidDestinationException JavaDoc;
26 import javax.jms.JMSException JavaDoc;
27 import javax.jms.Message JavaDoc;
28 import javax.jms.MessageConsumer JavaDoc;
29 import javax.jms.MessageProducer JavaDoc;
30 import javax.jms.Queue JavaDoc;
31 import javax.jms.Session JavaDoc;
32 import javax.jms.TemporaryQueue JavaDoc;
33 import javax.jms.TextMessage JavaDoc;
34 import java.util.ArrayList JavaDoc;
35
36 /**
37  * @version
38  */

39 public class JmsTempDestinationTest extends TestCase {
40
41     private Connection JavaDoc connection;
42     private ActiveMQConnectionFactory factory;
43
44     protected void setUp() throws Exception JavaDoc {
45         factory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
46         factory.setUseAsyncSend(false);
47         connection = factory.createConnection();
48     }
49
50     /**
51      * @see junit.framework.TestCase#tearDown()
52      */

53     protected void tearDown() throws Exception JavaDoc {
54         if (connection != null) {
55             connection.close();
56             connection = null;
57         }
58     }
59
60
61     /**
62      * Make sure Temp destination can only be consumed by local connection
63      *
64      * @throws JMSException
65      */

66     public void testTempDestOnlyConsumedByLocalConn() throws JMSException JavaDoc {
67         connection.start();
68
69         Session JavaDoc tempSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
70         TemporaryQueue JavaDoc queue = tempSession.createTemporaryQueue();
71         MessageProducer JavaDoc producer = tempSession.createProducer(queue);
72         producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
73         TextMessage JavaDoc message = tempSession.createTextMessage("First");
74         producer.send(message);
75
76         //temp destination should not be consume when using another connection
77
Connection JavaDoc otherConnection = factory.createConnection();
78         Session JavaDoc otherSession = otherConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
79         TemporaryQueue JavaDoc otherQueue = otherSession.createTemporaryQueue();
80         MessageConsumer JavaDoc consumer = otherSession.createConsumer(otherQueue);
81         Message msg = consumer.receive(3000);
82         assertNull(msg);
83
84         //should throw InvalidDestinationException when consuming a temp destination from another connection
85
try{
86              consumer = otherSession.createConsumer(queue);
87              fail("Send should fail since temp destination should be used from another connection");
88         }catch(InvalidDestinationException JavaDoc e){
89               assertTrue("failed to throw an exception",true);
90         }
91
92
93         //should be able to consume temp destination from the same connection
94
consumer = tempSession.createConsumer(queue);
95         msg = consumer.receive(3000);
96         assertNotNull(msg);
97
98
99     }
100
101     /**
102      * Make sure that a temp queue does not drop message if there is an active
103      * consumers.
104      *
105      * @throws JMSException
106      */

107     public void testTempQueueHoldsMessagesWithConsumers() throws JMSException JavaDoc {
108         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
109         Queue JavaDoc queue = session.createTemporaryQueue();
110         MessageConsumer JavaDoc consumer = session.createConsumer(queue);
111         connection.start();
112
113         MessageProducer JavaDoc producer = session.createProducer(queue);
114         producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
115         TextMessage JavaDoc message = session.createTextMessage("Hello");
116         producer.send(message);
117
118         Message message2 = consumer.receive(1000);
119         assertNotNull(message2);
120         assertTrue("Expected message to be a TextMessage", message2 instanceof TextMessage JavaDoc);
121         assertTrue("Expected message to be a '" + message.getText() + "'",
122                 ((TextMessage JavaDoc) message2).getText().equals(message.getText()));
123     }
124
125     /**
126      * Make sure that a temp queue does not drop message if there are no active
127      * consumers.
128      *
129      * @throws JMSException
130      */

131     public void testTempQueueHoldsMessagesWithoutConsumers() throws JMSException JavaDoc {
132
133         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
134         Queue JavaDoc queue = session.createTemporaryQueue();
135         MessageProducer JavaDoc producer = session.createProducer(queue);
136         producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
137         TextMessage JavaDoc message = session.createTextMessage("Hello");
138         producer.send(message);
139
140         connection.start();
141         MessageConsumer JavaDoc consumer = session.createConsumer(queue);
142         Message message2 = consumer.receive(3000);
143         assertNotNull(message2);
144         assertTrue("Expected message to be a TextMessage", message2 instanceof TextMessage JavaDoc);
145         assertTrue("Expected message to be a '" + message.getText() + "'",
146                 ((TextMessage JavaDoc) message2).getText().equals(message.getText()));
147
148     }
149     
150     /**
151      * Test temp queue works under load
152      * @throws JMSException
153      */

154     public void testTmpQueueWorksUnderLoad() throws JMSException JavaDoc {
155         int count = 500;
156         int dataSize = 1024;
157         
158         ArrayList JavaDoc list = new ArrayList JavaDoc(count);
159         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
160         Queue JavaDoc queue = session.createTemporaryQueue();
161         MessageProducer JavaDoc producer = session.createProducer(queue);
162         producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
163       
164         byte[] data = new byte[dataSize];
165         for (int i =0; i < count; i++){
166             BytesMessage JavaDoc message = session.createBytesMessage();
167             message.writeBytes(data);
168             producer.send(message);
169             list.add(message);
170         }
171         
172
173         connection.start();
174         MessageConsumer JavaDoc consumer = session.createConsumer(queue);
175         for (int i =0; i < count; i++){
176             Message message2 = consumer.receive(2000);
177             
178             assertTrue(message2 != null);
179             assertTrue(message2.equals(list.get(i)));
180         }
181     }
182     
183     /**
184      * Make sure you cannot publish to a temp destination that does not exist anymore.
185      *
186      * @throws JMSException
187      */

188     public void testPublishFailsForClosedConnection() throws JMSException JavaDoc {
189         
190         Connection JavaDoc tempConnection = factory.createConnection();
191         Session JavaDoc tempSession = tempConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
192         TemporaryQueue JavaDoc queue = tempSession.createTemporaryQueue();
193         
194         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
195         connection.start();
196         
197         // This message delivery should work since the temp connection is still open.
198
MessageProducer JavaDoc producer = session.createProducer(queue);
199         producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
200         TextMessage JavaDoc message = session.createTextMessage("First");
201         producer.send(message);
202
203         // Closing the connection should destroy the temp queue that was created.
204
tempConnection.close();
205         
206         // This message delivery NOT should work since the temp connection is now closed.
207
try {
208             message = session.createTextMessage("Hello");
209             producer.send(message);
210             fail("Send should fail since temp destination should not exist anymore.");
211         } catch ( JMSException JavaDoc e ) {
212             assertTrue("failed to throw an exception",true);
213         }
214     }
215     
216     /**
217      * Make sure you cannot publish to a temp destination that does not exist anymore.
218      *
219      * @throws JMSException
220      */

221     public void testPublishFailsForDestoryedTempDestination() throws JMSException JavaDoc {
222         
223         Connection JavaDoc tempConnection = factory.createConnection();
224         Session JavaDoc tempSession = tempConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
225         TemporaryQueue JavaDoc queue = tempSession.createTemporaryQueue();
226         
227         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
228         connection.start();
229         
230         // This message delivery should work since the temp connection is still open.
231
MessageProducer JavaDoc producer = session.createProducer(queue);
232         producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
233         TextMessage JavaDoc message = session.createTextMessage("First");
234         producer.send(message);
235
236         // deleting the Queue will cause sends to fail
237
queue.delete();
238         
239         // This message delivery NOT should work since the temp connection is now closed.
240
try {
241             message = session.createTextMessage("Hello");
242             producer.send(message);
243             fail("Send should fail since temp destination should not exist anymore.");
244         } catch ( JMSException JavaDoc e ) {
245             assertTrue("failed to throw an exception",true);
246         }
247     }
248     
249     /**
250      * Test you can't delete a Destination with Active Subscribers
251      * @throws JMSException
252      */

253     public void testDeleteDestinationWithSubscribersFails() throws JMSException JavaDoc {
254         Connection JavaDoc connection = factory.createConnection();
255         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
256         TemporaryQueue JavaDoc queue = session.createTemporaryQueue();
257           
258         connection.start();
259         
260         session.createConsumer(queue);
261         
262         // This message delivery should NOT work since the temp connection is now closed.
263
try {
264             queue.delete();
265             fail("Should fail as Subscribers are active");
266         } catch ( JMSException JavaDoc e ) {
267             assertTrue("failed to throw an exception",true);
268         }
269     }
270 }
271
Popular Tags