S Clark
S Clark

Reputation: 33

ActiveMQ & Java - delays when between publish & consume

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.

  1. Is this to be expected when running multiple consumers from 1 queue, or is that irrelevant?
  2. Is there anything I can do to mitigate the delay?

Thanks in advance for any useful advice.

Upvotes: 0

Views: 737

Answers (1)

Tim Bish
Tim Bish

Reputation: 18401

There are many reasons for short delays like this when dealing with network consumers and a central broker. The CPU scheduling of either the broker or the consumer along with work done managing the network interface could lead to small hiccups. You might also be seeing GC pauses as the JVM on the consumer or the broker cleans up which can delay dispatch or consumption. And of course inter-thread singaling can sometime have short delays depending on various things running alongside so these short pauses are really something I'd invest a ton of time in unless you get to the point where that is the last issue you need to solve out of all the other likely things you aren't dealing with yet.

Upvotes: 4

Related Questions