final CountDownLatch sampleTimeDone = new CountDownLatch(1);
final AtomicInteger producedMessages = new AtomicInteger(0);
final AtomicInteger receivedMessages = new AtomicInteger(0);
final Callable producer = new Callable() {
public Object call() throws JMSException, InterruptedException {
Connection connection = factory.createConnection();
connections.add(connection);
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
BytesMessage message = session.createBytesMessage();
message.writeBytes(new byte[1024]);
connection.start();
connectionsEstablished.release();
while (!sampleTimeDone.await(0, TimeUnit.MILLISECONDS)) {
producer.send(message);
producedMessages.incrementAndGet();
}
connection.close();
workerDone.release();
return null;
}
};
final Callable consumer = new Callable() {
public Object call() throws JMSException, InterruptedException {
Connection connection = factory.createConnection();
connections.add(connection);
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(destination);
consumer.setMessageListener(new MessageListener() {
public void onMessage(Message msg) {
receivedMessages.incrementAndGet();
}
});
connection.start();
connectionsEstablished.release();
sampleTimeDone.await();
connection.close();
workerDone.release();
return null;
}
};
final Throwable workerError[] = new Throwable[1];
for (int i = 0; i < PRODUCER_COUNT; i++) {
new Thread("Producer:" + i) {
public void run() {
try {
producer.call();
} catch (Throwable e) {
e.printStackTrace();
workerError[0] = e;
}
}
}.start();
}
for (int i = 0; i < CONSUMER_COUNT; i++) {
new Thread("Consumer:" + i) {
public void run() {
try {
consumer.call();
} catch (Throwable e) {
e.printStackTrace();
workerError[0] = e;
}
}