Change the default setting of Spring Boot JMS Library which uses ReceivedAndDelete for Azure Service Bus to PeekLock

When you’ve followed the documentation https://docs.microsoft.com/azure/service-bus-messaging/message-transfers-locks-settlement or you mastered the free learn module https://docs.microsoft.com/learn/modules/cloud-scale-messaging-with-jms-service-bus/, you might want to go further and change for instance the default behavior for the received messages. That’s how it works:

To use the PeekLock mode with JMS, you can set the session mode to CLIENT_ACKNOWLEDGE instead of the default AUTO_ACKNOWLEDGE, which will acknowledge a message manually by the client-side. Find the documentation here.

So if you are using JmsTemplate to consume messages, you can call the method of JmsTemplate#setSessionAcknowledgeMode to set the mode and then use the execute method with a SessionCallback to consume messages within the scope of the session, and also acknowledge messages explicitly.

And if using a listener, things get a bit complex. You can implement your own MessageListener , where you consume and acknowledge messages in the method of onMessage. Then define your own bean of org.springframework.jms.listener.DefaultMessageListenerContainer to accept this message listener and also set the session ack mode.

Published by Sandra Ahlgrimm

Cloud Advocate @ Azure with a passion for Java and containers

Leave a comment