//get proxy queues for statistics lookups
Connection proxyConnection = factory.createConnection();
proxyConnection.start();
Session proxySession = proxyConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
final QueueViewMBean proxyQueue1 = getProxyToQueueViewMBean(proxySession.createQueue(QUEUE_1_NAME));
final QueueViewMBean proxyQueue2 = getProxyToQueueViewMBean(proxySession.createQueue(QUEUE_2_NAME));
// LOAD THE QUEUE
Connection producerConnection = factory.createConnection();
producerConnection.start();
Session session = producerConnection.createSession(TRANSACTED, Session.AUTO_ACKNOWLEDGE);
Destination queue = session.createQueue(QUEUE_1_NAME);
MessageProducer producer = session.createProducer(queue);
List<TextMessage> senderList = new ArrayList<TextMessage>();
for (int i = 0; i < MESSAGE_COUNT; i++) {
TextMessage msg = session.createTextMessage(i + " " + formatter.format(new Date()));
senderList.add(msg);
producer.send(msg);
if(TRANSACTED) session.commit();
if(DEBUG && i%100 == 0){
int index = (i/100)+1;
System.out.print(index-((index/10)*10));
}
}
//get access to the Queue info
if(DEBUG){
System.out.println("");
System.out.println("Queue1 Size = "+proxyQueue1.getQueueSize());
System.out.println("Queue1 Memory % Used = "+proxyQueue1.getMemoryPercentUsage());
System.out.println("Queue1 Memory Available = "+proxyQueue1.getMemoryLimit());
}
// FLUSH THE QUEUE
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
Connection[] consumerConnections1 = new Connection[NUM_CONSUMERS];
List<Message> consumerList1 = new ArrayList<Message>();
Connection[] consumerConnections2 = new Connection[NUM_CONSUMERS];
Connection[] producerConnections2 = new Connection[NUM_CONSUMERS];
List<Message> consumerList2 = new ArrayList<Message>();
for(int ix=0; ix<NUM_CONSUMERS; ix++){
producerConnections2[ix] = factory.createConnection();
producerConnections2[ix].start();
consumerConnections1[ix] = getConsumerConnection(factory);
Session consumerSession = consumerConnections1[ix].createSession(TRANSACTED, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = consumerSession.createConsumer(session.createQueue(QUEUE_1_NAME));
consumer.setMessageListener(new SessionAwareMessageListener(producerConnections2[ix], consumerSession, QUEUE_2_NAME, latch1, consumerList1));
}
latch1.await(200000, TimeUnit.MILLISECONDS);
if(DEBUG){
System.out.println("");
System.out.println("Queue2 Size = "+proxyQueue2.getQueueSize());
System.out.println("Queue2 Memory % Used = "+proxyQueue2.getMemoryPercentUsage());
System.out.println("Queue2 Memory Available = "+proxyQueue2.getMemoryLimit());
}
for(int ix=0; ix<NUM_CONSUMERS; ix++){
consumerConnections2[ix] = getConsumerConnection(factory);
Session consumerSession = consumerConnections2[ix].createSession(TRANSACTED, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = consumerSession.createConsumer(session.createQueue(QUEUE_2_NAME));
consumer.setMessageListener(new SessionAwareMessageListener(consumerSession, latch2, consumerList2));
}
boolean success = Wait.waitFor(new Wait.Condition() {
@Override
public boolean isSatisified() throws Exception {
boolean done = latch2.await(10, TimeUnit.SECONDS);
if(DEBUG){
System.out.println("");
System.out.println("Queue1 Size = "+proxyQueue1.getQueueSize());
System.out.println("Queue1 Memory % Used = "+proxyQueue1.getMemoryPercentUsage());
System.out.println("Queue2 Size = "+proxyQueue2.getQueueSize());
System.out.println("Queue2 Memory % Used = "+proxyQueue2.getMemoryPercentUsage());
System.out.println("Queue2 Memory Available = "+proxyQueue2.getMemoryLimit());
}
return done;
}
}, 300 * 1000);
if (!success) {
dumpAllThreads("blocked waiting on 2");
}
assertTrue("got all expected messages on 2", success);
producerConnection.close();
for(int ix=0; ix<NUM_CONSUMERS; ix++){
consumerConnections1[ix].close();
consumerConnections2[ix].close();
producerConnections2[ix].close();
}
//let the consumer statistics on queue2 have time to update
Thread.sleep(500);
if(DEBUG){
System.out.println("");
System.out.println("Queue1 Size = "+proxyQueue1.getQueueSize());
System.out.println("Queue1 Memory % Used = "+proxyQueue1.getMemoryPercentUsage());
System.out.println("Queue2 Size = "+proxyQueue2.getQueueSize());
System.out.println("Queue2 Memory % Used = "+proxyQueue2.getMemoryPercentUsage());
}
Wait.waitFor(new Wait.Condition() {
@Override
public boolean isSatisified() throws Exception {
return 0 == proxyQueue1.getQueueSize();
}});
assertEquals("Queue1 has gone negative,",0, proxyQueue1.getQueueSize());
Wait.waitFor(new Wait.Condition() {
@Override
public boolean isSatisified() throws Exception {
return 0 == proxyQueue2.getQueueSize();
}});
assertEquals("Queue2 has gone negative,",0, proxyQueue2.getQueueSize());
proxyConnection.close();
}