Nishu Tayal
Nishu Tayal

Reputation: 20830

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?

Upvotes: 0

Views: 528

Answers (1)

alexey28
alexey28

Reputation: 5220

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

Upvotes: 1

Related Questions