版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/YAOQINGGG/article/details/80106679
消费者可以通过设置prefetchSize进行批量取消息,有默认值。可以设置:
1、 tcp://localhost:61616?jms.prefetchPolicy.all=50
2、 tcp://localhost:61616?jms.prefetchPolicy.queuePrefetch=1
3、 queue = new ActiveMQQueue(“TEST.QUEUE?consumer.prefetchSize=10”);
该数如果设置为0,那消费者不能用push方式读取消息,此时消费者将读取不到消息。
在broker端,有下面的方法来判断需不需要给客户端推送消息。
/**
* Used to determine if the broker can dispatch to the consumer.
*
* @return true if the subscription is full
*/
@Override
public boolean isFull() {
return getPrefetchSize() == 0 ? prefetchExtension.get() == 0 : dispatched.size() - prefetchExtension.get() >= info.getPrefetchSize();
}
看下queue里面的doActualDispatch方法会调用该方法进行判断,如果不满,才会发送给消费者
if (!fullConsumers.contains(s)) {
if (!s.isFull()) {
if (dispatchSelector.canSelect(s, node) && assignMessageGroup(s, (QueueMessageReference)node) && !((QueueMessageReference) node).isAcked() ) {
// Dispatch it.
s.add(node);
LOG.trace("assigned {} to consumer {}", node.getMessageId(), s.getConsumerInfo().getConsumerId());
iterator.remove();
target = s;
break;
}
} else {
// no further dispatch of list to a full consumer to
// avoid out of order message receipt
fullConsumers.add(s);
LOG.trace("Subscription full {}", s);
}
}