Skip to main content

Azure Service Bus Notes

Last Updated: June 11, 2022

Gradle packages

implementation 'com.azure.spring:azure-spring-boot-starter:4.0.0'
implementation 'com.azure.spring:azure-spring-boot-starter-servicebus-jms:4.0.0'

dependencyManagement {
imports {
mavenBom "com.azure.spring:azure-spring-boot-bom:4.0.0"
}
}

Using Standard Tier with Spring Boot JMS

The Azure documentation suggests that you need the Premium tier or above in order to use the Spring Boot JMS Listener integration.

Transactions on the default container factory are enabled by default.

To disable transactions on your JMS listener, add an override to the jmsListenerContainerFactory.

@Bean
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(
ConnectionFactory connectionFactory,
DefaultJmsListenerContainerFactoryConfigurer configurer) {
DefaultJmsListenerContainerFactory listenerFactory =
new DefaultJmsListenerContainerFactory();
configurer.configure(listenerFactory, connectionFactory);
listenerFactory.setTransactionManager(null);
listenerFactory.setSessionTransacted(false);
return listenerFactory;
}

Converting to Java POJO

@JmsListener(
destination = "${queue.<queueName>}",
containerFactory = "jmsListenerContainerFactory"
)
public void receiveEvent(Message incomingMessage) throws JMSException {
var messageBytes = incomingMessage.getBody(byte[].class);
var timeStamp = incomingMessage.getJMSTimestamp();
var date = OffsetDateTime.ofInstant(Instant.ofEpochMilli(timeStamp),
TimeZone.getDefault().toZoneId());
var messageJson = new String(messageBytes);
var incomingEvent = new Gson().fromJson(messageJson, IncomingEvent.class);
}