KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > usecases > CreateTemporaryQueueBeforeStartTest


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
19 package org.apache.activemq.usecases;
20 import java.util.concurrent.atomic.AtomicInteger JavaDoc;
21
22 import org.apache.activemq.ActiveMQConnectionFactory;
23 import org.apache.activemq.broker.BrokerService;
24
25 import javax.jms.Connection JavaDoc;
26 import javax.jms.Queue JavaDoc;
27 import javax.jms.QueueConnection JavaDoc;
28 import javax.jms.QueueReceiver JavaDoc;
29 import javax.jms.QueueSender JavaDoc;
30 import javax.jms.QueueSession JavaDoc;
31 import javax.jms.Session JavaDoc;
32 import javax.jms.Topic JavaDoc;
33
34 import junit.framework.TestCase;
35
36 /**
37  * @author Peter Henning
38  * @version $Revision: 1.1.1.1 $
39  */

40 public class CreateTemporaryQueueBeforeStartTest extends TestCase {
41     protected String JavaDoc bindAddress = "tcp://localhost:61621";
42     private Connection JavaDoc connection;
43     private BrokerService broker = new BrokerService();
44
45     public void testCreateTemporaryQueue() throws Exception JavaDoc {
46         connection = createConnection();
47         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
48         Queue JavaDoc queue = session.createTemporaryQueue();
49         assertTrue("No queue created!", queue != null);
50         Topic JavaDoc topic = session.createTemporaryTopic();
51         assertTrue("No topic created!", topic != null);
52     }
53
54     public void testTryToReproduceNullPointerBug() throws Exception JavaDoc {
55         String JavaDoc url = bindAddress;
56         ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(url);
57         QueueConnection JavaDoc queueConnection = factory.createQueueConnection();
58         this.connection = queueConnection;
59         QueueSession JavaDoc session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
60         QueueSender JavaDoc sender = session.createSender(null); //Unidentified
61
Queue JavaDoc receiverQueue = session.createTemporaryQueue();
62         QueueReceiver JavaDoc receiver = session.createReceiver(receiverQueue);
63         queueConnection.start();
64     }
65
66     public void testTemporaryQueueConsumer() throws Exception JavaDoc {
67         final int NUMBER = 20;
68         final AtomicInteger JavaDoc count = new AtomicInteger JavaDoc(0);
69         for (int i = 0;i < NUMBER;i++) {
70             Thread JavaDoc thread = new Thread JavaDoc(new Runnable JavaDoc() {
71                 public void run() {
72                     try {
73                         QueueConnection JavaDoc connection = createConnection();
74                         QueueSession JavaDoc session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
75                         Queue JavaDoc queue = session.createTemporaryQueue();
76                         QueueReceiver JavaDoc consumer = session.createReceiver(queue);
77                         connection.start();
78                         
79                         
80                         if (count.incrementAndGet() >= NUMBER){
81                             synchronized(count){
82                                 count.notify();
83                             }
84                         }
85                     }
86                     catch (Exception JavaDoc ex) {
87                         ex.printStackTrace();
88                     }
89                 }
90             });
91             thread.start();
92         }
93         int maxWaitTime = 20000;
94         synchronized (count) {
95             long waitTime = maxWaitTime;
96             long start = System.currentTimeMillis();
97             while (count.get() < NUMBER) {
98                 if (waitTime <= 0) {
99                     break;
100                 }
101                 else {
102                     count.wait(waitTime);
103                     waitTime = maxWaitTime - (System.currentTimeMillis() - start);
104                 }
105             }
106         }
107         assertTrue("Unexpected count: " + count, count.get() == NUMBER);
108     }
109
110     protected QueueConnection JavaDoc createConnection() throws Exception JavaDoc {
111         ActiveMQConnectionFactory factory = createConnectionFactory();
112         return factory.createQueueConnection();
113     }
114
115     protected ActiveMQConnectionFactory createConnectionFactory() throws Exception JavaDoc {
116         return new ActiveMQConnectionFactory(bindAddress);
117     }
118
119     protected void setUp() throws Exception JavaDoc {
120         broker.setPersistent(false);
121         broker.addConnector(bindAddress);
122         broker.start();
123         super.setUp();
124     }
125
126     protected void tearDown() throws Exception JavaDoc {
127         if (connection != null) {
128             connection.close();
129         }
130         broker.stop();
131         super.tearDown();
132     }
133 }
134
Popular Tags