KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.activemq.broker.BrokerService;
21 import org.apache.activemq.broker.TransportConnector;
22 import org.apache.activemq.network.DemandForwardingBridge;
23 import org.apache.activemq.network.NetworkBridgeConfiguration;
24 import org.apache.activemq.transport.TransportFactory;
25
26 import java.util.List JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.net.URI JavaDoc;
30
31 /**
32  * @version $Revision: 1.1.1.1 $
33  */

34 public class MultiBrokersMultiClientsUsingTcpTest extends MultiBrokersMultiClientsTest {
35     protected List JavaDoc bridges;
36
37     protected void bridgeAllBrokers(String JavaDoc groupName) throws Exception JavaDoc {
38         for (int i=1; i<=BROKER_COUNT; i++) {
39             for (int j=1; j<=BROKER_COUNT; j++) {
40                 if (i != j) {
41                     bridgeBrokers("Broker" + i, "Broker" + j);
42                 }
43             }
44         }
45
46         MAX_SETUP_TIME = 5000;
47     }
48
49     protected void bridgeBrokers(BrokerService localBroker, BrokerService remoteBroker) throws Exception JavaDoc {
50         List JavaDoc remoteTransports = remoteBroker.getTransportConnectors();
51         List JavaDoc localTransports = localBroker.getTransportConnectors();
52
53         URI JavaDoc remoteURI, localURI;
54         if (!remoteTransports.isEmpty() && !localTransports.isEmpty()) {
55             remoteURI = ((TransportConnector)remoteTransports.get(0)).getConnectUri();
56             localURI = ((TransportConnector)localTransports.get(0)).getConnectUri();
57
58             // Ensure that we are connecting using tcp
59
if (remoteURI.toString().startsWith("tcp:") && localURI.toString().startsWith("tcp:")) {
60                 NetworkBridgeConfiguration config = new NetworkBridgeConfiguration();
61                 config.setBrokerName(localBroker.getBrokerName());
62                 DemandForwardingBridge bridge = new DemandForwardingBridge(config,TransportFactory.connect(localURI),
63                                                                            TransportFactory.connect(remoteURI));
64                 bridges.add(bridge);
65
66                 bridge.start();
67             } else {
68                 throw new Exception JavaDoc("Remote broker or local broker is not using tcp connectors");
69             }
70         } else {
71             throw new Exception JavaDoc("Remote broker or local broker has no registered connectors.");
72         }
73     }
74
75     public void setUp() throws Exception JavaDoc {
76         super.setUp();
77
78         // Assign a tcp connector to each broker
79
int j=0;
80         for (Iterator JavaDoc i=brokers.values().iterator(); i.hasNext();) {
81             ((BrokerItem)i.next()).broker.addConnector("tcp://localhost:" + (61616 + j++));
82         }
83
84         bridges = new ArrayList JavaDoc();
85     }
86 }
87
Popular Tags