为什么我的消息消费者没有收到所有其他消息?

时间:2014-09-18 08:47:50

标签: java glassfish jms activemq message-queue

我已经编写了一些代码,使用在Glassfish 4服务器上运行的ActiveMQ将TestMessages推送到队列中 如果我发送连续的消息" A"," B"," C"," D"," E" ," F" 我只收到" A"," C"," E"

有谁知道为什么?

消息制作人

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());
    }
}

消息使用者

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();
            }
        }
    }
}

提前致谢 标记

1 个答案:

答案 0 :(得分:0)

这里显而易见的答案是,您在该目的地上运行了多个消费者。每当队列有多个消费者时,消息就会在它们之间进行负载平衡。您可以通过JMX或使用Web控制台连接到代理,并检查队列上的订阅以查看有多少订阅。