I am trying to get all messages from the queue in synchronous mode using Spring JMSTemplate.receive(String) method.
The problem is that I get always only one message. Here is the code:
@Transactional
public List<Message> receiveAllFromQueue(String destination) {
List<Message> messages = new ArrayList<Message>();
Message message;
while ((message = queueJmsTemplate.receive(destination)) != null) {
messages.add(message);
}
return messages;
}
If I remove @Transactional annotation I get all messages, but all is done out of transaction so if later during processing these messages there is an exception the messages will be lost.
Here is a definition of my JMSTemplate bean.
<bean id="queueJmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="pubSubDomain" value="false" />
<property name="receiveTimeout" value="1" />
<property name="sessionTransacted" value="true" />
</bean>
What I want to achieve is to have one transaction and within this transaction I want to get all pending messages.