KickJava   Java API By Example, From Geeks To Geeks.

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


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.usecases;
19
20 import java.net.URI JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.concurrent.CountDownLatch JavaDoc;
24 import java.util.concurrent.TimeUnit JavaDoc;
25
26 import javax.jms.Destination JavaDoc;
27 import javax.jms.MessageConsumer JavaDoc;
28
29 import org.apache.activemq.JmsMultipleBrokersTestSupport;
30 import org.apache.activemq.util.MessageIdList;
31
32 /**
33  * @version $Revision: 1.1.1.1 $
34  */

35 public class MultiBrokersMultiClientsTest extends JmsMultipleBrokersTestSupport {
36     public static final int BROKER_COUNT = 2; // number of brokers to network
37
public static final int CONSUMER_COUNT = 3; // consumers per broker
38
public static final int PRODUCER_COUNT = 3; // producers per broker
39
public static final int MESSAGE_COUNT = 10; // messages per producer
40

41     protected Map JavaDoc consumerMap;
42
43     public void testTopicAllConnected() throws Exception JavaDoc {
44         bridgeAllBrokers();
45         startAllBrokers();
46
47
48         // Setup topic destination
49
Destination JavaDoc dest = createDestination("TEST.FOO", true);
50
51         CountDownLatch JavaDoc latch = new CountDownLatch JavaDoc(
52                 BROKER_COUNT * PRODUCER_COUNT *
53                 BROKER_COUNT * CONSUMER_COUNT *
54                 MESSAGE_COUNT);
55         
56         // Setup consumers
57
for (int i=1; i<=BROKER_COUNT; i++) {
58             for (int j=0; j<CONSUMER_COUNT; j++) {
59                 consumerMap.put("Consumer:" + i + ":" + j, createConsumer("Broker" + i, dest, latch));
60             }
61         }
62
63         //wait for consumers to get propagated
64
Thread.sleep(5000);
65
66         // Send messages
67
for (int i=1; i<=BROKER_COUNT; i++) {
68             for (int j=0; j<PRODUCER_COUNT; j++) {
69                 sendMessages("Broker" + i, dest, MESSAGE_COUNT);
70             }
71         }
72         
73         assertTrue("Missing "+latch.getCount()+ " messages", latch.await(30, TimeUnit.SECONDS));
74
75         // Get message count
76
for (int i=1; i<=BROKER_COUNT; i++) {
77             for (int j=0; j<CONSUMER_COUNT; j++) {
78                 MessageIdList msgs = getConsumerMessages("Broker" + i, (MessageConsumer JavaDoc)consumerMap.get("Consumer:" + i + ":" + j));
79                 assertEquals(BROKER_COUNT * PRODUCER_COUNT * MESSAGE_COUNT, msgs.getMessageCount());
80             }
81         }
82         
83     }
84
85     public void testQueueAllConnected() throws Exception JavaDoc {
86         bridgeAllBrokers();
87         startAllBrokers();
88
89         // Setup topic destination
90
Destination JavaDoc dest = createDestination("TEST.FOO", false);
91
92         CountDownLatch JavaDoc latch = new CountDownLatch JavaDoc(BROKER_COUNT * PRODUCER_COUNT * MESSAGE_COUNT);
93         
94         // Setup consumers
95
for (int i=1; i<=BROKER_COUNT; i++) {
96             for (int j=0; j<CONSUMER_COUNT; j++) {
97                 consumerMap.put("Consumer:" + i + ":" + j, createConsumer("Broker" + i, dest, latch));
98             }
99         }
100         
101         //wait for consumers to get propagated
102
Thread.sleep(5000);
103
104         // Send messages
105
for (int i=1; i<=BROKER_COUNT; i++) {
106             for (int j=0; j<PRODUCER_COUNT; j++) {
107                 sendMessages("Broker" + i, dest, MESSAGE_COUNT);
108             }
109         }
110
111         // Wait for messages to be delivered
112
assertTrue("Missing "+latch.getCount()+ " messages", latch.await(30, TimeUnit.SECONDS));
113         
114         // Get message count
115
int totalMsg = 0;
116         for (int i=1; i<=BROKER_COUNT; i++) {
117             for (int j=0; j<CONSUMER_COUNT; j++) {
118                 MessageIdList msgs = getConsumerMessages("Broker" + i, (MessageConsumer JavaDoc)consumerMap.get("Consumer:" + i + ":" + j));
119                 totalMsg += msgs.getMessageCount();
120             }
121         }
122         assertEquals(BROKER_COUNT * PRODUCER_COUNT * MESSAGE_COUNT, totalMsg);
123     }
124
125     public void setUp() throws Exception JavaDoc {
126         super.setAutoFail(true);
127         super.setUp();
128
129         // Setup n brokers
130
for (int i=1; i<=BROKER_COUNT; i++) {
131             createBroker(new URI JavaDoc("broker:()/Broker" + i + "?persistent=false&useJmx=false"));
132         }
133
134         consumerMap = new HashMap JavaDoc();
135     }
136 }
137
Popular Tags