6363public class AMQ4485LowLimitTest extends JmsMultipleBrokersTestSupport {
6464 static final String payload = new String (new byte [10 * 1024 ]);
6565 private static final Logger LOG = LoggerFactory .getLogger (AMQ4485LowLimitTest .class );
66- final int portBase = 61600 ;
6766 int numBrokers = 4 ;
67+ final int [] brokerPorts = new int [numBrokers ];
6868 final int numProducers = 10 ;
6969 final int numMessages = 200 ;
7070 final int consumerSleepTime = 5 ;
71- StringBuilder brokersUrl = new StringBuilder ();
7271 HashMap <ActiveMQQueue , AtomicInteger > accumulators = new HashMap <ActiveMQQueue , AtomicInteger >();
7372 private ArrayList <Throwable > exceptions = new ArrayList <Throwable >();
7473
75- protected void buildUrlList () throws Exception {
74+ protected void collectBrokerPorts () throws Exception {
7675 for (int i = 0 ; i < numBrokers ; i ++) {
77- brokersUrl .append ("tcp://localhost:" + (portBase + i ));
76+ final BrokerService broker = brokers .get ("B" + i ).broker ;
77+ brokerPorts [i ] = broker .getTransportConnectors ().get (0 ).getConnectUri ().getPort ();
78+ }
79+ }
80+
81+ protected String buildBrokersUrl () {
82+ final StringBuilder sb = new StringBuilder ();
83+ for (int i = 0 ; i < numBrokers ; i ++) {
84+ sb .append ("tcp://localhost:" ).append (brokerPorts [i ]);
7885 if (i != numBrokers - 1 ) {
79- brokersUrl .append (',' );
86+ sb .append (',' );
8087 }
8188 }
89+ return sb .toString ();
8290 }
8391
8492 protected BrokerService createBroker (int brokerid ) throws Exception {
@@ -95,11 +103,8 @@ protected BrokerService createBroker(int brokerid, boolean addToNetwork) throws
95103
96104 broker .setUseJmx (true );
97105 broker .setBrokerName ("B" + brokerid );
98- broker .addConnector (new URI ("tcp://localhost:" + ( portBase + brokerid ) ));
106+ broker .addConnector (new URI ("tcp://localhost:0" ));
99107
100- if (addToNetwork ) {
101- addNetworkConnector (broker );
102- }
103108 broker .setSchedulePeriodForDestinationPurge (0 );
104109 broker .getSystemUsage ().getMemoryUsage ().setLimit (256 * 1024 * 1024l );
105110
@@ -132,12 +137,11 @@ protected BrokerService createBroker(int brokerid, boolean addToNetwork) throws
132137 return broker ;
133138 }
134139
135- private void addNetworkConnector (BrokerService broker ) throws Exception {
136- StringBuilder networkConnectorUrl = new StringBuilder ("static:(" ).append (brokersUrl .toString ());
137- networkConnectorUrl .append (')' );
140+ private void addNetworkConnector (BrokerService broker , String brokersUrl ) throws Exception {
141+ final String networkConnectorUrl = "static:(" + brokersUrl + ")" ;
138142
139143 for (int i = 0 ; i < 2 ; i ++) {
140- NetworkConnector nc = new DiscoveryNetworkConnector (new URI (networkConnectorUrl . toString () ));
144+ final NetworkConnector nc = new DiscoveryNetworkConnector (new URI (networkConnectorUrl ));
141145 nc .setName ("Bridge-" + i );
142146 nc .setNetworkTTL (1 );
143147 nc .setDecreaseNetworkConsumerPriority (true );
@@ -146,6 +150,7 @@ private void addNetworkConnector(BrokerService broker) throws Exception {
146150 nc .setDynamicallyIncludedDestinations (
147151 Arrays .asList (new ActiveMQDestination []{new ActiveMQQueue ("GW.*" )}));
148152 broker .addNetworkConnector (nc );
153+ broker .startNetworkConnector (nc , null );
149154 }
150155 }
151156
@@ -156,7 +161,8 @@ public void x_testInterleavedSend() throws Exception {
156161 BrokerService b = createBroker (0 , false );
157162 b .start ();
158163
159- ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory ("tcp://localhost:" + (portBase + 0 ));
164+ final int port = b .getTransportConnectors ().get (0 ).getConnectUri ().getPort ();
165+ ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory ("tcp://localhost:" + port );
160166 connectionFactory .setWatchTopicAdvisories (false );
161167
162168 QueueConnection c1 = connectionFactory .createQueueConnection ();
@@ -207,13 +213,19 @@ public void run() {
207213
208214 public void testBrokers () throws Exception {
209215
210- buildUrlList ();
211-
212216 for (int i = 0 ; i < numBrokers ; i ++) {
213217 createBroker (i );
214218 }
215219
216220 startAllBrokers ();
221+
222+ // Get actual ports after brokers start and add network connectors
223+ collectBrokerPorts ();
224+ final String brokersUrl = buildBrokersUrl ();
225+ for (int i = 0 ; i < numBrokers ; i ++) {
226+ addNetworkConnector (brokers .get ("B" + i ).broker , brokersUrl );
227+ }
228+
217229 waitForBridgeFormation (numBrokers - 1 );
218230
219231 verifyPeerBrokerInfos (numBrokers - 1 );
@@ -266,7 +278,7 @@ public boolean isSatisified() throws Exception {
266278
267279 private void startConsumer (String brokerName , ActiveMQDestination destination ) throws Exception {
268280 int id = Integer .parseInt (brokerName .substring (1 ));
269- ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory ("tcp://localhost:" + ( portBase + id ) );
281+ ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory ("tcp://localhost:" + brokerPorts [ id ] );
270282 connectionFactory .setWatchTopicAdvisories (false );
271283 QueueConnection queueConnection = connectionFactory .createQueueConnection ();
272284 queueConnection .start ();
@@ -303,7 +315,7 @@ private void startAllGWFanoutConsumers(int nBrokers) throws Exception {
303315 ActiveMQQueue compositeQ = new ActiveMQQueue (compositeDest .toString ());
304316
305317 for (int id = 0 ; id < nBrokers ; id ++) {
306- ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory ("failover:(tcp://localhost:" + ( portBase + id ) + ")" );
318+ ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory ("failover:(tcp://localhost:" + brokerPorts [ id ] + ")" );
307319 connectionFactory .setWatchTopicAdvisories (false );
308320
309321 QueueConnection queueConnection = connectionFactory .createQueueConnection ();
@@ -330,7 +342,7 @@ public void onMessage(Message message) {
330342 private List <ConsumerState > startAllGWConsumers (int nBrokers ) throws Exception {
331343 List <ConsumerState > consumerStates = new LinkedList <ConsumerState >();
332344 for (int id = 0 ; id < nBrokers ; id ++) {
333- ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory ("failover:(tcp://localhost:" + ( portBase + id ) + ")" );
345+ ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory ("failover:(tcp://localhost:" + brokerPorts [ id ] + ")" );
334346 connectionFactory .setWatchTopicAdvisories (false );
335347
336348 QueueConnection queueConnection = connectionFactory .createQueueConnection ();
@@ -393,7 +405,7 @@ private void produce(final int numMessages) throws Exception {
393405 @ Override
394406 public void run () {
395407 try {
396- ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory ("failover:(tcp://localhost:" + ( portBase + id ) + ")" );
408+ ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory ("failover:(tcp://localhost:" + brokerPorts [ id ] + ")" );
397409 connectionFactory .setWatchTopicAdvisories (false );
398410 QueueConnection queueConnection = connectionFactory .createQueueConnection ();
399411 queueConnection .start ();
0 commit comments