KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > network > NetworkTestSupport


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.network;
19
20 import java.io.IOException JavaDoc;
21 import java.net.URI JavaDoc;
22 import java.net.URISyntaxException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 import org.apache.activemq.broker.BrokerFactory;
27 import org.apache.activemq.broker.BrokerRegistry;
28 import org.apache.activemq.broker.BrokerService;
29 import org.apache.activemq.broker.BrokerTestSupport;
30 import org.apache.activemq.broker.StubConnection;
31 import org.apache.activemq.broker.TransportConnector;
32 import org.apache.activemq.memory.UsageManager;
33 import org.apache.activemq.store.PersistenceAdapter;
34 import org.apache.activemq.store.memory.MemoryPersistenceAdapter;
35 import org.apache.activemq.transport.Transport;
36 import org.apache.activemq.transport.TransportFactory;
37
38 public class NetworkTestSupport extends BrokerTestSupport {
39     
40     protected ArrayList JavaDoc connections = new ArrayList JavaDoc();
41     
42     protected TransportConnector connector;
43
44     protected PersistenceAdapter remotePersistenceAdapter;
45     protected BrokerService remoteBroker;
46     protected UsageManager remoteMemoryManager;
47     protected TransportConnector remoteConnector;
48
49
50     protected void setUp() throws Exception JavaDoc {
51         
52         super.setUp();
53         connector = createConnector();
54         connector.start();
55         
56         remotePersistenceAdapter = createRemotePersistenceAdapter(true);
57         remotePersistenceAdapter.start();
58         remoteBroker = createRemoteBroker(remotePersistenceAdapter);
59         remoteBroker.start();
60         BrokerRegistry.getInstance().bind("remotehost", remoteBroker);
61         remoteConnector = createRemoteConnector();
62         remoteConnector.start();
63     }
64
65     /**
66      * @return
67      * @throws Exception
68      * @throws IOException
69      * @throws URISyntaxException
70      */

71     protected TransportConnector createRemoteConnector() throws Exception JavaDoc, IOException JavaDoc, URISyntaxException JavaDoc {
72         return new TransportConnector(remoteBroker.getBroker(),
73                 TransportFactory.bind(broker.getBrokerName(),
74                         new URI JavaDoc(getRemoteURI())));
75     }
76
77     /**
78      * @param value
79      * @return
80      * @throws Exception
81      * @throws IOException
82      * @throws URISyntaxException
83      */

84     protected TransportConnector createConnector() throws Exception JavaDoc, IOException JavaDoc, URISyntaxException JavaDoc {
85         return new TransportConnector(broker.getBroker(),
86                 TransportFactory.bind(broker.getBrokerName(), new URI JavaDoc(getLocalURI())));
87     }
88
89     protected String JavaDoc getRemoteURI() {
90         return "vm://remotehost";
91     }
92
93     protected String JavaDoc getLocalURI() {
94         return "vm://localhost";
95     }
96
97     protected PersistenceAdapter createRemotePersistenceAdapter(boolean clean) throws Exception JavaDoc {
98         if( remotePersistenceAdapter == null || clean ) {
99             remotePersistenceAdapter = new MemoryPersistenceAdapter();
100         }
101         return remotePersistenceAdapter;
102     }
103     
104     protected BrokerService createBroker() throws Exception JavaDoc {
105         BrokerService broker = BrokerFactory.createBroker(new URI JavaDoc("broker:()/localhost?persistent=false&useJmx=false&"));
106         return broker;
107     }
108     
109     protected BrokerService createRemoteBroker(PersistenceAdapter persistenceAdapter) throws Exception JavaDoc {
110         BrokerService answer = new BrokerService();
111         answer.setBrokerName("remote");
112         answer.setUseJmx(false);
113         answer.setPersistenceAdapter(persistenceAdapter);
114         return answer;
115     }
116     
117     protected StubConnection createConnection() throws Exception JavaDoc {
118         Transport transport = TransportFactory.connect(connector.getServer().getConnectURI());
119         StubConnection connection = new StubConnection(transport);
120         connections.add(connection);
121         return connection;
122     }
123
124     protected StubConnection createRemoteConnection() throws Exception JavaDoc {
125         Transport transport = TransportFactory.connect(remoteConnector.getServer().getConnectURI());
126         StubConnection connection = new StubConnection(transport);
127         connections.add(connection);
128         return connection;
129     }
130     
131     protected Transport createTransport() throws Exception JavaDoc {
132         Transport transport = TransportFactory.connect(connector.getServer().getConnectURI());
133         return transport;
134     }
135
136     protected Transport createRemoteTransport() throws Exception JavaDoc {
137         Transport transport = TransportFactory.connect(remoteConnector.getServer().getConnectURI());
138         return transport;
139     }
140
141     /**
142      * Simulates a broker restart. The memory based persistence adapter is
143      * reused so that it does not "loose" it's "persistent" messages.
144      * @throws Exception
145      */

146     protected void restartRemoteBroker() throws Exception JavaDoc {
147
148         BrokerRegistry.getInstance().unbind("remotehost");
149         remoteConnector.stop();
150
151         remoteBroker.stop();
152         remotePersistenceAdapter.stop();
153         remotePersistenceAdapter = createRemotePersistenceAdapter(false);
154         remotePersistenceAdapter.start();
155         remoteBroker = createRemoteBroker(remotePersistenceAdapter);
156         remoteBroker.start();
157         String JavaDoc brokerId = remoteBroker.getBrokerName();
158         remoteConnector = new TransportConnector(broker.getBroker(),TransportFactory.bind(brokerId,new URI JavaDoc(getRemoteURI())));
159         remoteConnector.start();
160         BrokerRegistry.getInstance().bind("remotehost", remoteBroker);
161     }
162     
163     protected void tearDown() throws Exception JavaDoc {
164         for (Iterator JavaDoc iter = connections.iterator(); iter.hasNext();) {
165             StubConnection connection = (StubConnection) iter.next();
166             connection.stop();
167             iter.remove();
168         }
169         
170         BrokerRegistry.getInstance().unbind("remotehost");
171         remoteConnector.stop();
172         connector.stop();
173
174         remoteBroker.stop();
175         remotePersistenceAdapter.stop();
176         super.tearDown();
177     }
178
179 }
180
Popular Tags