简体   繁体   中英

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

Following is my consumer :

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

There are 10 messages in the queue.
Rightnow, 1 message is consumed by each consumer. I want 10 message to be consumed by each consumer.
What changes should I do for that?

The nature of queue is that you have one producer and one consumer. You should use topic for this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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