| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package org.apache.bugs; |
| 18 |
|
| 19 |
import static org.junit.Assert.assertTrue; |
| 20 |
import static org.junit.Assert.fail; |
| 21 |
|
| 22 |
import java.util.concurrent.Callable; |
| 23 |
import java.util.concurrent.CountDownLatch; |
| 24 |
import java.util.concurrent.ExecutorService; |
| 25 |
import java.util.concurrent.Executors; |
| 26 |
import java.util.concurrent.TimeUnit; |
| 27 |
import java.util.concurrent.atomic.AtomicInteger; |
| 28 |
|
| 29 |
import javax.jms.JMSException; |
| 30 |
import javax.jms.Message; |
| 31 |
import javax.jms.MessageListener; |
| 32 |
import javax.jms.Session; |
| 33 |
import javax.jms.TextMessage; |
| 34 |
|
| 35 |
import org.apache.activemq.ActiveMQConnectionFactory; |
| 36 |
import org.apache.activemq.broker.BrokerRegistry; |
| 37 |
import org.apache.activemq.broker.BrokerService; |
| 38 |
import org.apache.activemq.command.ActiveMQQueue; |
| 39 |
import org.apache.activemq.network.NetworkConnector; |
| 40 |
import org.apache.activemq.store.memory.MemoryPersistenceAdapter; |
| 41 |
import org.junit.Ignore; |
| 42 |
import org.junit.Test; |
| 43 |
import org.slf4j.Logger; |
| 44 |
import org.slf4j.LoggerFactory; |
| 45 |
import org.springframework.jms.connection.CachingConnectionFactory; |
| 46 |
import org.springframework.jms.connection.SingleConnectionFactory; |
| 47 |
import org.springframework.jms.core.JmsTemplate; |
| 48 |
import org.springframework.jms.core.MessageCreator; |
| 49 |
import org.springframework.jms.listener.DefaultMessageListenerContainer; |
| 50 |
|
| 51 |
public class LoadBalanceTest { |
| 52 |
private static final Logger LOG = LoggerFactory.getLogger(LoadBalanceTest.class); |
| 53 |
private static final String TESTING_QUEUE = "testingqueue"; |
| 54 |
private static int networkBridgePrefetch = 1000; |
| 55 |
|
| 56 |
@Test(timeout=120000) |
| 57 |
public void does_load_balance_between_consumers() throws Exception { |
| 58 |
BrokerService brokerService1 = null; |
| 59 |
BrokerService brokerService2 = null; |
| 60 |
final int total = 100; |
| 61 |
final AtomicInteger broker1Count = new AtomicInteger(0); |
| 62 |
final AtomicInteger broker2Count = new AtomicInteger(0); |
| 63 |
final CountDownLatch startProducer = new CountDownLatch(1); |
| 64 |
final CountDownLatch consumer1Started = new CountDownLatch(1); |
| 65 |
final CountDownLatch consumer2Started = new CountDownLatch(1); |
| 66 |
|
| 67 |
String broker1Uri; |
| 68 |
String broker2Uri; |
| 69 |
|
| 70 |
try { |
| 71 |
brokerService1 = new BrokerService(); |
| 72 |
brokerService1.setBrokerName("one"); |
| 73 |
brokerService1.setUseJmx(false); |
| 74 |
brokerService1.setPersistenceAdapter(new MemoryPersistenceAdapter()); |
| 75 |
broker1Uri = brokerService1.addConnector("nio://0.0.0.0:0").getPublishableConnectString(); |
| 76 |
|
| 77 |
brokerService2 = new BrokerService(); |
| 78 |
brokerService2.setBrokerName("two"); |
| 79 |
brokerService2.setUseJmx(false); |
| 80 |
brokerService2.setPersistenceAdapter(new MemoryPersistenceAdapter()); |
| 81 |
broker2Uri = brokerService2.addConnector("nio://0.0.0.0:0").getPublishableConnectString(); |
| 82 |
|
| 83 |
final NetworkConnector network1 = brokerService1.addNetworkConnector("static:("+broker2Uri+")"); |
| 84 |
network1.setName("network1"); |
| 85 |
network1.setDynamicOnly(true); |
| 86 |
network1.setNetworkTTL(3); |
| 87 |
network1.setPrefetchSize(networkBridgePrefetch); |
| 88 |
network1.setConduitSubscriptions(false); |
| 89 |
network1.setDecreaseNetworkConsumerPriority(false); |
| 90 |
network1.setDispatchAsync(false); |
| 91 |
|
| 92 |
final NetworkConnector network2 = brokerService2.addNetworkConnector("static:("+broker1Uri+")"); |
| 93 |
network2.setName("network1"); |
| 94 |
network2.setDynamicOnly(true); |
| 95 |
network2.setNetworkTTL(3); |
| 96 |
network2.setPrefetchSize(networkBridgePrefetch); |
| 97 |
network2.setConduitSubscriptions(false); |
| 98 |
network2.setDecreaseNetworkConsumerPriority(false); |
| 99 |
network2.setDispatchAsync(false); |
| 100 |
|
| 101 |
brokerService1.start(); |
| 102 |
brokerService2.start(); |
| 103 |
|
| 104 |
final ExecutorService pool = Executors.newSingleThreadExecutor(); |
| 105 |
final ActiveMQConnectionFactory connectionFactory1 = new ActiveMQConnectionFactory("vm://one"); |
| 106 |
final SingleConnectionFactory singleConnectionFactory1 = new SingleConnectionFactory(connectionFactory1); |
| 107 |
singleConnectionFactory1.setReconnectOnException(true); |
| 108 |
final DefaultMessageListenerContainer container1 = new DefaultMessageListenerContainer(); |
| 109 |
container1.setConnectionFactory(singleConnectionFactory1); |
| 110 |
container1.setMaxConcurrentConsumers(1); |
| 111 |
container1.setDestination(new ActiveMQQueue("testingqueue")); |
| 112 |
container1.setMessageListener(new MessageListener() { |
| 113 |
|
| 114 |
@Override |
| 115 |
public void onMessage(final Message message) { |
| 116 |
broker1Count.incrementAndGet(); |
| 117 |
} |
| 118 |
}); |
| 119 |
container1.afterPropertiesSet(); |
| 120 |
container1.start(); |
| 121 |
consumer1Started.countDown(); |
| 122 |
pool.submit(new Callable<Object>() { |
| 123 |
|
| 124 |
@Override |
| 125 |
public Object call() throws Exception { |
| 126 |
try { |
| 127 |
final ActiveMQConnectionFactory connectionFactory2 = new ActiveMQConnectionFactory( |
| 128 |
"vm://two"); |
| 129 |
final SingleConnectionFactory singleConnectionFactory2 = new SingleConnectionFactory( |
| 130 |
connectionFactory2); |
| 131 |
singleConnectionFactory2.setReconnectOnException(true); |
| 132 |
final DefaultMessageListenerContainer container2 = new DefaultMessageListenerContainer(); |
| 133 |
container2 |
| 134 |
.setConnectionFactory(singleConnectionFactory2); |
| 135 |
container2.setMaxConcurrentConsumers(1); |
| 136 |
container2.setDestination(new ActiveMQQueue( |
| 137 |
"testingqueue")); |
| 138 |
container2.setMessageListener(new MessageListener() { |
| 139 |
|
| 140 |
@Override |
| 141 |
public void onMessage(final Message message) { |
| 142 |
broker2Count.incrementAndGet(); |
| 143 |
} |
| 144 |
}); |
| 145 |
container2.afterPropertiesSet(); |
| 146 |
container2.start(); |
| 147 |
consumer2Started.countDown(); |
| 148 |
|
| 149 |
assertTrue("wait for start signal", startProducer.await(20, TimeUnit.SECONDS)); |
| 150 |
|
| 151 |
final CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory( |
| 152 |
singleConnectionFactory2); |
| 153 |
final JmsTemplate template = new JmsTemplate( |
| 154 |
cachingConnectionFactory); |
| 155 |
final ActiveMQQueue queue = new ActiveMQQueue( |
| 156 |
"testingqueue"); |
| 157 |
for (int i = 0; i < total; i++) { |
| 158 |
template.send(queue, new MessageCreator() { |
| 159 |
|
| 160 |
@Override |
| 161 |
public Message createMessage( |
| 162 |
final Session session) |
| 163 |
throws JMSException { |
| 164 |
final TextMessage message = session |
| 165 |
.createTextMessage(); |
| 166 |
message.setText("Hello World!"); |
| 167 |
return message; |
| 168 |
} |
| 169 |
}); |
| 170 |
} |
| 171 |
|
| 172 |
while (container2.getActiveConsumerCount() > 1) { |
| 173 |
System.out.println("active consumer count: " |
| 174 |
+ container2.getActiveConsumerCount()); |
| 175 |
System.out.println("concurrent consumer count: " |
| 176 |
+ container2.getConcurrentConsumers()); |
| 177 |
Thread.sleep(1000); |
| 178 |
} |
| 179 |
cachingConnectionFactory.destroy(); |
| 180 |
container2.destroy(); |
| 181 |
} catch (final Throwable t) { |
| 182 |
t.printStackTrace(); |
| 183 |
} |
| 184 |
return null; |
| 185 |
} |
| 186 |
}); |
| 187 |
|
| 188 |
waitForBridgeFormation(); |
| 189 |
startProducer.countDown(); |
| 190 |
consumer1Started.await(); |
| 191 |
consumer2Started.await(); |
| 192 |
|
| 193 |
pool.shutdown(); |
| 194 |
pool.awaitTermination(20, TimeUnit.SECONDS); |
| 195 |
LOG.info("broker1Count " + broker1Count.get() + ", broker2Count " + broker2Count.get()); |
| 196 |
|
| 197 |
int count = 0; |
| 198 |
|
| 199 |
while (count++ < 10 |
| 200 |
&& broker1Count.get() + broker2Count.get() != total) { |
| 201 |
LOG.info("broker1Count " + broker1Count.get() + ", broker2Count " + broker2Count.get()); |
| 202 |
Thread.sleep(1000); |
| 203 |
} |
| 204 |
container1.destroy(); |
| 205 |
} finally { |
| 206 |
try { |
| 207 |
if (brokerService1 != null) { |
| 208 |
brokerService1.stop(); |
| 209 |
brokerService1.waitUntilStopped(); |
| 210 |
} |
| 211 |
} catch (final Throwable t) { |
| 212 |
t.printStackTrace(); |
| 213 |
} |
| 214 |
try { |
| 215 |
if (brokerService2 != null) { |
| 216 |
brokerService2.stop(); |
| 217 |
brokerService2.waitUntilStopped(); |
| 218 |
} |
| 219 |
} catch (final Throwable t) { |
| 220 |
t.printStackTrace(); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
if (broker1Count.get() < 25 || broker2Count.get() < 25) { |
| 225 |
fail("Each broker should have gotten at least 25 messages but instead broker1 got " |
| 226 |
+ broker1Count.get() |
| 227 |
+ " and broker2 got " |
| 228 |
+ broker2Count.get()); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
@Test |
| 233 |
@Ignore("Does not work") |
| 234 |
public void does_xml_multicast_load_balance_between_consumers() throws Exception { |
| 235 |
final int total = 100; |
| 236 |
final AtomicInteger broker1Count = new AtomicInteger(0); |
| 237 |
final AtomicInteger broker2Count = new AtomicInteger(0); |
| 238 |
final ExecutorService pool = Executors.newSingleThreadExecutor(); |
| 239 |
final CountDownLatch startProducer = new CountDownLatch(1); |
| 240 |
final String xmlConfig = getClass().getPackage().getName().replace('.','/') + "/loadbalancetest.xml"; |
| 241 |
System.setProperty("lbt.networkBridgePrefetch", String.valueOf(networkBridgePrefetch)); |
| 242 |
System.setProperty("lbt.brokerName", "one"); |
| 243 |
final ActiveMQConnectionFactory connectionFactory1 = new ActiveMQConnectionFactory( |
| 244 |
"vm://one?brokerConfig=xbean:" + xmlConfig); |
| 245 |
final SingleConnectionFactory singleConnectionFactory1 = new SingleConnectionFactory( |
| 246 |
connectionFactory1); |
| 247 |
singleConnectionFactory1.setReconnectOnException(true); |
| 248 |
final DefaultMessageListenerContainer container1 = new DefaultMessageListenerContainer(); |
| 249 |
container1.setConnectionFactory(singleConnectionFactory1); |
| 250 |
container1.setMaxConcurrentConsumers(1); |
| 251 |
container1.setDestination(new ActiveMQQueue(TESTING_QUEUE)); |
| 252 |
container1.setMessageListener(new MessageListener() { |
| 253 |
|
| 254 |
@Override |
| 255 |
public void onMessage(final Message message) { |
| 256 |
broker1Count.incrementAndGet(); |
| 257 |
} |
| 258 |
}); |
| 259 |
container1.afterPropertiesSet(); |
| 260 |
container1.start(); |
| 261 |
pool.submit(new Callable<Object>() { |
| 262 |
|
| 263 |
@Override |
| 264 |
public Object call() throws Exception { |
| 265 |
System.setProperty("lbt.brokerName", "two"); |
| 266 |
final ActiveMQConnectionFactory connectionFactory2 = new ActiveMQConnectionFactory( |
| 267 |
"vm://two?brokerConfig=xbean:" + xmlConfig); |
| 268 |
final SingleConnectionFactory singleConnectionFactory2 = new SingleConnectionFactory( |
| 269 |
connectionFactory2); |
| 270 |
singleConnectionFactory2.setReconnectOnException(true); |
| 271 |
final DefaultMessageListenerContainer container2 = new DefaultMessageListenerContainer(); |
| 272 |
container2.setConnectionFactory(singleConnectionFactory2); |
| 273 |
container2.setMaxConcurrentConsumers(1); |
| 274 |
container2.setDestination(new ActiveMQQueue(TESTING_QUEUE)); |
| 275 |
container2.setMessageListener(new MessageListener() { |
| 276 |
|
| 277 |
@Override |
| 278 |
public void onMessage(final Message message) { |
| 279 |
broker2Count.incrementAndGet(); |
| 280 |
} |
| 281 |
}); |
| 282 |
container2.afterPropertiesSet(); |
| 283 |
container2.start(); |
| 284 |
|
| 285 |
|
| 286 |
assertTrue("wait for start signal", startProducer.await(20, TimeUnit.SECONDS)); |
| 287 |
|
| 288 |
final CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory( |
| 289 |
singleConnectionFactory2); |
| 290 |
final JmsTemplate template = new JmsTemplate( |
| 291 |
cachingConnectionFactory); |
| 292 |
final ActiveMQQueue queue = new ActiveMQQueue(TESTING_QUEUE); |
| 293 |
for (int i = 0; i < total; i++) { |
| 294 |
template.send(queue, new MessageCreator() { |
| 295 |
|
| 296 |
@Override |
| 297 |
public Message createMessage(final Session session) |
| 298 |
throws JMSException { |
| 299 |
final TextMessage message = session |
| 300 |
.createTextMessage(); |
| 301 |
message.setText("Hello World!"); |
| 302 |
return message; |
| 303 |
} |
| 304 |
}); |
| 305 |
} |
| 306 |
return null; |
| 307 |
} |
| 308 |
}); |
| 309 |
|
| 310 |
|
| 311 |
waitForBridgeFormation(); |
| 312 |
startProducer.countDown(); |
| 313 |
|
| 314 |
pool.shutdown(); |
| 315 |
pool.awaitTermination(10, TimeUnit.SECONDS); |
| 316 |
|
| 317 |
LOG.info("broker1Count " + broker1Count.get() + ", broker2Count " + broker2Count.get()); |
| 318 |
|
| 319 |
int count = 0; |
| 320 |
|
| 321 |
while (count++ < 10 && broker1Count.get() + broker2Count.get() != total) { |
| 322 |
LOG.info("broker1Count " + broker1Count.get() + ", broker2Count " + broker2Count.get()); |
| 323 |
Thread.sleep(1000); |
| 324 |
} |
| 325 |
if (broker1Count.get() < 25 || broker2Count.get() < 25) { |
| 326 |
fail("Each broker should have gotten at least 25 messages but instead broker1 got " |
| 327 |
+ broker1Count.get() |
| 328 |
+ " and broker2 got " |
| 329 |
+ broker2Count.get()); |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
BrokerService broker = BrokerRegistry.getInstance().lookup("one"); |
| 334 |
broker.stop(); |
| 335 |
broker = BrokerRegistry.getInstance().lookup("two"); |
| 336 |
broker.stop(); |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
private void waitForBridgeFormation() throws Exception { |
| 342 |
long done = System.currentTimeMillis() + 30000; |
| 343 |
while (done > System.currentTimeMillis()) { |
| 344 |
if (hasBridge("one") && hasBridge("two")) { |
| 345 |
return; |
| 346 |
} |
| 347 |
Thread.sleep(1000); |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
private boolean hasBridge(String name) { |
| 352 |
boolean result = false; |
| 353 |
BrokerService broker = BrokerRegistry.getInstance().lookup(name); |
| 354 |
if (broker != null && !broker.getNetworkConnectors().isEmpty()) { |
| 355 |
if (!broker.getNetworkConnectors().get(0).activeBridges().isEmpty()) { |
| 356 |
result = true; |
| 357 |
} |
| 358 |
} |
| 359 |
return result; |
| 360 |
} |
| 361 |
} |