final ActiveMQConnectionFactory connectionFactory1 = new ActiveMQConnectionFactory(
"vm://one");
final SingleConnectionFactory singleConnectionFactory1 = new SingleConnectionFactory(
connectionFactory1);
singleConnectionFactory1.setReconnectOnException(true);
final DefaultMessageListenerContainer container1 = new DefaultMessageListenerContainer();
container1.setConnectionFactory(singleConnectionFactory1);
container1.setMaxConcurrentConsumers(1);
container1.setDestination(new ActiveMQQueue("testingqueue"));
container1.setMessageListener(new MessageListener() {
public void onMessage(final Message message) {
broker1Count.incrementAndGet();
}
});
container1.afterPropertiesSet();
container1.start();
pool.submit(new Callable<Object>() {
public Object call() throws Exception {
try {
final ActiveMQConnectionFactory connectionFactory2 = new ActiveMQConnectionFactory(
"vm://two");
final SingleConnectionFactory singleConnectionFactory2 = new SingleConnectionFactory(
connectionFactory2);
singleConnectionFactory2.setReconnectOnException(true);
final DefaultMessageListenerContainer container2 = new DefaultMessageListenerContainer();
container2
.setConnectionFactory(singleConnectionFactory2);
container2.setMaxConcurrentConsumers(1);
container2.setDestination(new ActiveMQQueue(
"testingqueue"));
container2.setMessageListener(new MessageListener() {
public void onMessage(final Message message) {
broker2Count.incrementAndGet();
}
});
container2.afterPropertiesSet();
container2.start();
assertTrue("wait for start signal", startProducer.await(20, TimeUnit.SECONDS));
final CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(
singleConnectionFactory2);
final JmsTemplate template = new JmsTemplate(
cachingConnectionFactory);
final ActiveMQQueue queue = new ActiveMQQueue(
"testingqueue");
for (int i = 0; i < total; i++) {
template.send(queue, new MessageCreator() {
public Message createMessage(
final Session session)
throws JMSException {
final TextMessage message = session
.createTextMessage();
message.setText("Hello World!");
return message;
}
});
}
// give spring time to scale back again
while (container2.getActiveConsumerCount() > 1) {
System.out.println("active consumer count: "
+ container2.getActiveConsumerCount());
System.out.println("concurrent consumer count: "
+ container2.getConcurrentConsumers());
Thread.sleep(1000);
}
cachingConnectionFactory.destroy();
container2.destroy();
} catch (final Throwable t) {
t.printStackTrace();
}
return null;
}