Hi all,

I am having an issue and I'm not sure whether it's my misunderstanding of
ActiveMQ prefetch or if I'm doing something wrong.

Basically I want to slow down my consumer by prefetching 10 messages and
having no more messages incoming until I send acknowledges back. From my
reading on the ActiveMQ doc 
http://activemq.apache.org/what-is-the-prefetch-limit-for.html here , I
thought that it should be possible.

However what is happening is this:

1. I put 5000 messages in the queue from a producer.
2. I start up the consumer with consumer.prefetchSize=10
3. A function is set for the consumer messageListener which simply prints
out the message contents.
4. The consumer simply loops through every message in the queue (all 5000)
rather than stopping at 10 until I ACK, as I expected...

here is a snippet of the producer/consumer code. I have tried this with
Active MQ 5.5 and 5.3 (on Linux and on Mac) with the same results. Any
advice you may have would be much appreciated!!

******************
* PRODUCER CODE *
******************

ActiveMQConnectionFactory cf = new
ActiveMQConnectionFactory("tcp://localhost:61616");
Connection connection;
try {
        connection = cf.createConnection();
        connection.start();     
        Session session = connection.createSession(false,
Session.CLIENT_ACKNOWLEDGE);
                        
        ActiveMQQueue queue = new ActiveMQQueue("test.queue");
        MessageProducer producer = session.createProducer(queue);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        for (int i =0; i<5000; i++) {
                TextMessage message = session.createTextMessage("MyMessage: " + 
i);
                producer.send(message);
        }
} catch (JMSException e) {
        e.printStackTrace();
}

******************
* CONSUMER CODE *
******************

ActiveMQConnectionFactory cf = new
ActiveMQConnectionFactory("tcp://localhost:61616");
Connection connection;
try {
        connection = cf.createConnection();
        connection.start();
        Session session = connection.createSession(true,
Session.CLIENT_ACKNOWLEDGE);
        Queue queue = 
session.createQueue("test.queue?consumer.prefetchSize=10");
        MessageConsumer consumer = session.createConsumer(queue);
                        
        consumer.setMessageListener(new MessageListener() {
                public void onMessage(Message message) {
                        TextMessage textMessage = (TextMessage)message;
                                try {
                                        System.out.println("Consumer recieved: 
" + textMessage.getText());
                                } catch (JMSException e) {
                                        e.printStackTrace();
                                }
        } });
} catch (JMSException e1) {
        e1.printStackTrace();
}

--
View this message in context: 
http://activemq.2283324.n4.nabble.com/Prefetch-problem-misuderstanding-tp4222551p4222551.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.

Reply via email to