繁体   English   中英

如何在ActiveMQ中与一个使用者一起使用N条消息

[英]How to consume N number of messages with one consumer in ActiveMQ

以下是我的消费者:

 public static void main(String[] args) throws JMSException {
        // Getting JMS connection from the server
        ConnectionFactory connectionFactory
            = new ActiveMQConnectionFactory(url);
        Connection connection = connectionFactory.createConnection();
        connection.start();

        // Creating session for seding messages
        Session session = connection.createSession(false,
            Session.AUTO_ACKNOWLEDGE);

        // Getting the queue 'TESTQUEUE'
        Destination destination = session.createQueue(subject);

        // MessageConsumer is used for receiving (consuming) messages
        MessageConsumer consumer = session.createConsumer(destination);

        // Here we receive the message.
        // By default this call is blocking, which means it will wait
        // for a message to arrive on the queue.
        Message message = consumer.receive();
        System.out.println(message);


     // There are many types of Message and TextMessage
        // is just one of them. Producer sent us a TextMessage
        // so we must cast to it to get access to its .getText()
        // method.
        if(message instanceof ObjectMessage){
            ObjectMessage objectMessage  = (ObjectMessage)message;
            System.out.println(" Received Message : '"+objectMessage.getObject()+" '");
        }

        connection.close();
    }

队列中有10条消息。
马上,每个消费者消耗1条消息。 我希望每个消费者消耗10条消息。
我应该为此做些什么更改?

队列的性质是您只有一个生产者和一个消费者。 您应该为此使用主题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM