Grimalkyne
Grimalkyne

Reputation: 21

Why is my message consumer not receiving every other message?

I've written some code to push TestMessages onto a queue using ActiveMQ running on a Glassfish 4 server If I send as consecutive messages "A", "B", "C", "D", "E", "F" I only receive "A", "C", "E"

Does anyone have any idea why?

Message producer

public void sendMessage(String msg) {
    try {
        if (session == null) {
            session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
            queue = session.createQueue(QUEUE_NAME);
        }
        messageProducer = session.createProducer(queue);
        Message message = createMessage(msg);
        messageProducer.send(message);
    } catch (NamingException nex) {
        System.out.println("Messager - naming exception" + nex.getLocalizedMessage());
    } catch (JMSException jex) {
        System.out.println("Messager - JMS exception" + jex.getLocalizedMessage());
    }
}

Message Consumer

public class Listener extends Thread implements MessageListener {           
    private void run() {
        try {
            connectionFactory = new ActiveMQConnectionFactory(AMQ_USER, AMQ_PASS, url);
            connection = connectionFactory.createConnection();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            queue = session.createQueue(QUEUE_NAME);
            consumer = session.createConsumer(queue);
            consumer.setMessageListener(this);
            connection.start();
        } catch (NamingException nex) {
            log.error("Error in run()", nex);
        } catch (JMSException jex) {
            log.error("Error in run()", jex);
        }
    }

    @Override
    public void onMessage(Message message) {
        if (message instanceof TextMessage) {
            try {
                TextMessage textMessage = (TextMessage) message;
                if (textMessage != null) {
                    System.out.println(textMessage.getText());
                }
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }
}

Thanks in advance Mark

Upvotes: 2

Views: 2670

Answers (1)

Tim Bish
Tim Bish

Reputation: 18356

The obvious answer here is that you have more than one consumer running on that destination. Whenever there are more than one consumer for a Queue the messages are load balanced between them. You can connect to the broker via JMX or using the Web Console and check the subscriptions on the Queue to see how many there are.

Upvotes: 1

Related Questions