java no wait_即使队列在activemq中不为空,JMS实现中的receiveNoWait也返回null

我正在尝试在我的项目中实现JMS . 我使用活动mq作为提供程序,并使用持久队列 . 以下是从活动mq中检索元素的代码

conn = GlobalConfiguration.getJMSConnectionFactory().createConnection();

conn.start();

session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

MessageConsumer consumer = session.createConsumer(queue);

ObjectMessage obj = (ObjectMessage) consumer.receiveNoWait();

此代码有时返回数据,有时它返回null,即使我在活动的mq管理控制台中可以看到待处理消息的数量为非零 . 我阅读了很多文章,很少有人提到JMS api并没有强制要求你每次都获得元素,你必须相应地编写代码 . 因为在我的场景中,我依赖于队列,一旦队列返回null,我就杀了进程,所以我用以下方式修改了代码

我没有调用receiveNoWait,而是在通过队列浏览器检查队列中是否存在元素之后开始使用receive . 以下是修改后的代码

public static T retrieveObjectFromQueue(Queue queue, Class clazz) {

synchronized (queue) {

if(!queueHasMoreElements(queue))

return null;

Connection conn = null;

Session session = null;

try {

conn = GlobalConfiguration.getJMSConnectionFactory().createConnection();

conn.start();

session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

MessageConsumer consumer = session.createConsumer(queue);

ObjectMessage obj = (ObjectMessage) consumer.receive();

return clazz.cast(obj.getObject());

} catch(Exception e) {

throw new RuntimeException(e);

}finally {

closeSessionAndConnection(session, conn);

}

}

public static boolean queueHasMoreElements(Queue queue) {

Connection conn = null;

Session session = null;

try {

conn = GlobalConfiguration.getJMSConnectionFactory().createConnection();

conn.start();

session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

QueueBrowser browser = session.createBrowser(queue);

Enumeration enumeration = browser.getEnumeration();

return enumeration.hasMoreElements();

} catch(Exception e) {

throw new RuntimeException(e);

}finally {

closeSessionAndConnection(session, conn);

}

现在我的代码在处理了大约20-30个元素后卡住了,再次,我可以在管理控制台中看到待处理的元素 . 当我尝试使用调试模式时,我意识到,在检索了20-30个元素之后,我的代码卡在了consumer.receive(),这在情况队列为空时是预期的,但是当我检查我的管理控制台时,它显示了很多队列中的元素 .

我使用jdbc(mysql)作为activemq的持久存储 . 我正在使用的配置xml在activemq配置示例中给出(activemq / examples / conf / activemq-jdbc-performance.xml)

我使用tcp:// localhost:61616?jms.prefetchPolicy.queuePrefetch = 1作为activemq url .

请让我知道我做错了什么 . 我正在使用java8和apache activeMq 5.13.1

谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值