0

i have an outOfMemoryException while reading messages from a queue with 2 M of messages. and i am trying to find a way to read messgages by 1000 for example . here is my code

List<TextMessage> messages = jmsTemplate.browse(JndiQueues.BACKOUT, (session,browser) -> {
        Enumeration<?> browserEnumeration = browser.getEnumeration().;
        List<TextMessage> messageList = new ArrayList<TextMessage>();
        while (browserEnumeration.hasMoreElements()) {
            messageList.add((TextMessage) browserEnumeration.nextElement());
        }
        return messageList;
    });

thanks

Ares
  • 71
  • 1
  • 10

1 Answers1

0

Perform the browse on a different thread and pass the results subset to the main thread via a BlockingQueue<List<TextMessage>>. e.g. a LinkedBlockingQueue with a small capacity.

The browsing thread will block when the queue is full. When the main thread removes an entry from the queue, the browser can add a new one.

Probably makes sense to have a capacity at least 2 so that the next list is available immediately.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • Hello Gary ,my question was ,if there is in JmsTemplate a method to get Messages by a certain number (1000 ) for example . because i dont know if browse method loads the messages on memory that causes a memory leak . or is it because of adding messages to the list that loads two millions of messages and causes the outofMemory exception – Ares Sep 14 '20 at 08:05
  • It has nothing to do with the template; the template simply provides you with a browser to use, it doesn't do the browsing; it's because YOUR code is reading all the messages into memory; use the technique I described to avoid this. – Gary Russell Sep 14 '20 at 13:09