I have one server, on which I have one publisher which sends messages to a single queue. Reading from this queue I have 5 consumers, each on its own JVM. The idea is that the publisher's messages should be consumed as soon as possible by whichever consumer(s) is free. Sometimes all 5 will be free and then ActiveMQ presumably chooses one to receive/dequeue the message (?).
All messages are non-persistent. I'm using ActiveMQ pretty much out the box with just 1 queue and zero tinkering to any config files. I'm also not using transactions.
The publisher logs the time in millseconds right after it's returned:
public void sendMessage(String text) {
TextMessage message = null;
try {
message = session.createTextMessage(text);
producer.send(message);
System.out.println("JUST FINISHED SENDING MESSAGE " + System.currentTimeMillis());
} catch (JMSException e) {
e.printStackTrace();
}
}
Each consumer (running its own JVM) is listening on that queue for bursts of 2 minutes at time:
Message message = consumer.receive(120000);
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
text = textMessage.getText();
System.out.println("MESSAGE RECEIVED " + System.currentTimeMillis());
}
Usually consumers will log "MESSAGE RECEIVED" at exactly the same time in milliseconds as the "JUST FINISHED SENDING MESSAGE", which is perfect. But sometimes there is an inexplicable delay of around 15 milliseconds between the publish and the consume, even when all consumers are sitting free. Given all processes are on the same server, and that latency is absolutely critical, I'm frustrated that there's sometimes this delay.
- Is this to be expected when running multiple consumers from 1 queue, or is that irrelevant?
- Is there anything I can do to mitigate the delay?
Thanks in advance for any useful advice.