I am trying to get all messages from a queue in synchronous mode using the Spring method JMSTemplate.receive (String).
The problem is that I always get 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 delete the @Transactional annotation, I get all the messages, but everything is done from the transaction, so if an exception occurs later in the processing of these messages, the messages will be lost.
Here is the 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>
I want to reach a single transaction and inside this transaction I want to receive all pending messages.
source share