Reputation: 21
I'm fiddling around with a JMS Queue using ActiveMQ and I wanted to work out a Queue Browser. I've looked up some examples on the web but I'm having quite a bit of trouble getting it to work.
Here is the relevant code:
QueueBrowser browser = template.execute(new SessionCallback<QueueBrowser>() {
public QueueBrowser doInJms(Session session) throws JMSException {
return session.createBrowser(queue);
}
}, true);
Enumeration messages = browser.getEnumeration();
int num = 0;
while(messages.hasMoreElements()) {
messages.nextElement();
num +=1;
}
logger.info("Num = " + num);
I've injected in the template and queue, and using some log statements I have verified that they are the objects I want. I can even retrieve the queue from the QueueBrowser once it is instantiated. I've populated the queue with about 100 messages and am reading them off using a custom JmsMessageListener that has about a 1 sec delay between reads.
However, the issue is that the code hangs around the while(messages.hasMoreElements()
line. From debugging, it never even goes inside the loop. If I have that line of code in a logger to check the value the hanging also occurs.
If I merely use the enumeration to nextElement()
without looping, then I'm returned a null value. I cannot determine whether this means the queue or enumeration is empty. If so, shouldn't messages hasMoreElements()
return a proper boolean value?
Could the issue be that I'm reading messages with a JmsMessageListener? Am I using the template wrong? I can't really determine where something is going wrong because it appears that 90% of it is working, its just the last 10% that seems to be acting oddly.
EDIT:
Managed to get it working. My session was being closed before I ever called the browser to do anything. All I needed to do was move the browser code into the doInJms block as such:
template.execute(new SessionCallback<QueueBrowser>() {
public QueueBrowser doInJms(Session session) throws JMSException {
QueueBrowser browser = session.createBrowser(queue);
Enumeration messages = browser.getEnumeration();
int num = 0;
logger.info("" + messages.hasMoreElements());
while(messages.hasMoreElements()) {
messages.nextElement();
num +=1;
}
logger.info("Num = " + num);
return null;
}
}, true);
Upvotes: 1
Views: 3245
Reputation: 21
The solution is in the edit above, however I wanted to post this for ease of access. I merely needed to move the browser code inside the doInJms block. An additional solution is to use template.browse and create a new BrowserCallback to override. In the end the solution is similar.
template.execute(new SessionCallback<QueueBrowser>() {
public QueueBrowser doInJms(Session session) throws JMSException {
QueueBrowser browser = session.createBrowser(queue);
Enumeration messages = browser.getEnumeration();
int num = 0;
logger.info("" + messages.hasMoreElements());
while(messages.hasMoreElements()) {
messages.nextElement();
num +=1;
}
logger.info("Num = " + num);
return null;
}
}, true);
Upvotes: 1