我会回复自己。看起来 JMSTemplate 不支持它。暂时解决它的唯一方法是扩展 JMSTemplate 并添加使用部分 JMSTemplate 的新方法。不幸的是,有些方法是私有的,所以它们需要被复制......
public class CustomQueueJmsTemplate extends JmsTemplateDelegate {
public List<Message> receiveAll(String destinationName) {
return receiveAll(destinationName, null);
}
public List<Message> receiveAll(final String destinationName, final String messageSelector) {
return execute(new SessionCallback<List<Message>>() {
@Override
public List<Message> doInJms(Session session) throws JMSException {
Destination destination = resolveDestinationName(session, destinationName);
return doReceiveAll(session, destination, messageSelector);
}
}, true);
}
private List<Message> doReceiveAll(Session session, Destination destination, String messageSelector)
throws JMSException
{
return doReceiveAll(session, createConsumer(session, destination, messageSelector));
}
private List<Message> doReceiveAll(Session session, MessageConsumer consumer) throws JMSException {
try {
// Use transaction timeout (if available).
long timeout = getReceiveTimeout();
JmsResourceHolder resourceHolder = (JmsResourceHolder) TransactionSynchronizationManager
.getResource(getConnectionFactory());
if (resourceHolder != null && resourceHolder.hasTimeout()) {
timeout = resourceHolder.getTimeToLiveInMillis();
}
// START OF MODIFIED CODE
List<Message> messages = new ArrayList<>();
Message message;
while ((message = doReceive(consumer, timeout)) != null) {
messages.add(message);
}
// END OF MODIFIED CODE
if (session.getTransacted()) {
// Commit necessary - but avoid commit call within a JTA transaction.
if (isSessionLocallyTransacted(session)) {
// Transacted session created by this template -> commit.
JmsUtils.commitIfNecessary(session);
}
} else if (isClientAcknowledge(session)) {
// Manually acknowledge message, if any.
for (Message retrievedMessages : messages) {
retrievedMessages.acknowledge();
}
}
return messages;
}
finally {
JmsUtils.closeMessageConsumer(consumer);
}
}
private Message doReceive(MessageConsumer consumer, long timeout) throws JMSException {
if (timeout == RECEIVE_TIMEOUT_NO_WAIT) {
return consumer.receiveNoWait();
} else if (timeout > 0) {
return consumer.receive(timeout);
} else {
return consumer.receive();
}
}
}