在jboss管理控制台页面中,我可以查看队列中当前的项目数。 但是我从queueBrowser.getEnumeration()获取空枚举。
以下是我浏览队列的代码:
public class JMSQueueBrowser {
private final Log log = LogFactory.getLog(getClass());
private QueueConnectionFactory connectionFactory;
private Queue queue;
private QueueBrowser qBrowser;
private QueueSession qSession;
private QueueConnection qConn;
public JMSQueueBrowser() {
initialize();
}
private void initialize() {
try {
InitialContext initialContext = new InitialContext();
connectionFactory = (QueueConnectionFactory)initialContext.lookup("java:comp/env/jms/MyQCF");
queue = (Queue)initialContext.lookup("queue/sampleQueue");
qConn = (QueueConnection) connectionFactory.createConnection();
qConn.start();
qSession = qConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
qBrowser = qSession.createBrowser(queue);
initialContext.close();
} catch (NamingException e) {
log.error(e.getMessage());
} catch (JMSException e) {
log.error(e.getMessage());
}
}
public void browseQueue() {
try {
log.info("---------Queue Name: "+queue.getQueueName()+"-----------");
log.info("---------Queue Has Elements: "+qBrowser.getEnumeration().hasMoreElements()+"-----------");
} catch (JMSException e) {
log.error(e.getMessage());
}
}}
日志始终与以下内容相同:
INFO JMSQueueBrowser - ---------Queue Name: sampleQueue-----------
INFO JMSQueueBrowser - ---------Queue Has Elements: false----------
用于JMS队列的库是jbossall-client.jar。
任何答案都将不胜感激。提前谢谢。