1 18 package org.apache.activemq.network; 19 20 import java.io.IOException ; 21 import java.net.URI ; 22 import java.net.URISyntaxException ; 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 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 connections = new ArrayList (); 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 { 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 71 protected TransportConnector createRemoteConnector() throws Exception , IOException , URISyntaxException { 72 return new TransportConnector(remoteBroker.getBroker(), 73 TransportFactory.bind(broker.getBrokerName(), 74 new URI (getRemoteURI()))); 75 } 76 77 84 protected TransportConnector createConnector() throws Exception , IOException , URISyntaxException { 85 return new TransportConnector(broker.getBroker(), 86 TransportFactory.bind(broker.getBrokerName(), new URI (getLocalURI()))); 87 } 88 89 protected String getRemoteURI() { 90 return "vm://remotehost"; 91 } 92 93 protected String getLocalURI() { 94 return "vm://localhost"; 95 } 96 97 protected PersistenceAdapter createRemotePersistenceAdapter(boolean clean) throws Exception { 98 if( remotePersistenceAdapter == null || clean ) { 99 remotePersistenceAdapter = new MemoryPersistenceAdapter(); 100 } 101 return remotePersistenceAdapter; 102 } 103 104 protected BrokerService createBroker() throws Exception { 105 BrokerService broker = BrokerFactory.createBroker(new URI ("broker:()/localhost?persistent=false&useJmx=false&")); 106 return broker; 107 } 108 109 protected BrokerService createRemoteBroker(PersistenceAdapter persistenceAdapter) throws Exception { 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 { 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 { 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 { 132 Transport transport = TransportFactory.connect(connector.getServer().getConnectURI()); 133 return transport; 134 } 135 136 protected Transport createRemoteTransport() throws Exception { 137 Transport transport = TransportFactory.connect(remoteConnector.getServer().getConnectURI()); 138 return transport; 139 } 140 141 146 protected void restartRemoteBroker() throws Exception { 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 brokerId = remoteBroker.getBrokerName(); 158 remoteConnector = new TransportConnector(broker.getBroker(),TransportFactory.bind(brokerId,new URI (getRemoteURI()))); 159 remoteConnector.start(); 160 BrokerRegistry.getInstance().bind("remotehost", remoteBroker); 161 } 162 163 protected void tearDown() throws Exception { 164 for (Iterator 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 |