Use JMS in Spring to access Azure Service Bus
This tutorial demonstrates how to use Spring Boot Starter for Azure Service Bus JMS to send messages to and receive messages from Service Bus queues
and topics
.
Azure provides an asynchronous messaging platform called Azure Service Bus ("Service Bus") that is based on the Advanced Message Queueing Protocol 1.0 ("AMQP 1.0") standard. You can use Service Bus across the range of supported Azure platforms.
The Spring Boot Starter for Azure Service Bus JMS provides Spring JMS integration with Service Bus.
The following video describes how to integrate Spring JMS applications with Azure Service Bus using JMS 2.0.
Prerequisites
An Azure subscription - create one for free.
Java Development Kit (JDK) version 8 or higher.
Apache Maven, version 3.2 or higher.
cURL or a similar HTTP utility to test functionality.
A queue or topic for Azure Service Bus. If you don't have one, see Use Azure portal to create a Service Bus namespace and a queue or Use the Azure portal to create a Service Bus topic and subscriptions to the topic.
A Spring Boot application. If you don't have one, create a Maven project with the Spring Initializr. Be sure to select Maven Project and, under Dependencies, add the Spring Web dependency, then select Java version 8 or higher.
Important
Spring Boot version 2.5 or higher is required to complete the steps in this tutorial.
Send and receive messages from Azure Service Bus
With a queue or topic for Azure Service Bus, you can send and receive messages using Spring Cloud Azure Service Bus JMS.
To install the Spring Cloud Azure Service Bus JMS Starter module, add the following dependencies to your pom.xml file:
The Spring Cloud Azure Bill of Materials (BOM):
<dependencyManagement> <dependencies> <dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-cloud-azure-dependencies</artifactId> <version>4.5.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
The Spring Cloud Azure Service Bus JMS Starter artifact:
<dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-cloud-azure-starter-servicebus-jms</artifactId> </dependency>
Code the application
Use the following steps to configure your application to use a Service Bus queue or topic to send and receive messages.
Configure the Service Bus credentials by adding the following properties to your application.properties file.
spring.jms.servicebus.connection-string=<ServiceBusNamespaceConnectionString> spring.jms.servicebus.pricing-tier=<ServiceBusPricingTier>
The following table describes the fields in the configuration:
Field Description spring.jms.servicebus.connection-string
Specify the connection string you obtained in your Service Bus namespace from the Azure portal. spring.jms.servicebus.pricing-tier
Specify the pricing tier of your service bus. Supported values are premium, standard, and basic. Premium uses Java Message Service (JMS) 2.0, while standard and basic use JMS 1.0 to interact with Azure Service Bus. Add
@EnableJms
to enable support for JMS listener annotated endpoints. UseJmsTemplate
to send messages and@JmsListener
to receive messages, as shown in the following example:import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.jms.annotation.EnableJms; import org.springframework.boot.CommandLineRunner; import org.springframework.jms.annotation.JmsListener; import org.springframework.jms.core.JmsTemplate; @SpringBootApplication @EnableJms public class ServiceBusJMSQueueApplication implements CommandLineRunner { private static final Logger LOGGER = LoggerFactory.getLogger(ServiceBusJMSQueueApplication.class); private static final String QUEUE_NAME = "<QueueName>"; @Autowired private JmsTemplate jmsTemplate; public static void main(String[] args) { SpringApplication.run(ServiceBusJMSQueueApplication.class, args); } @Override public void run(String... args) { LOGGER.info("Sending message"); jmsTemplate.convertAndSend(QUEUE_NAME, "Hello Word"); } @JmsListener(destination = QUEUE_NAME, containerFactory = "jmsListenerContainerFactory") public void receiveMessage(String message) { LOGGER.info("Message received: {}", message); } }
Replace
<QueueName>
with your own queue name configured in your Service Bus namespace.Tip
In this tutorial, there are no authentication operations in the configurations or the code. However, connecting to Azure services requires authentication. To complete the authentication, you need to use Azure Identity. Spring Cloud Azure uses
DefaultAzureCredential
, which the Azure Identity library provides to help you get credentials without any code changes.DefaultAzureCredential
supports multiple authentication methods and determines which method to use at runtime. This approach enables your app to use different authentication methods in different environments (such as local and production environments) without implementing environment-specific code. For more information, see the Default Azure credential section of Authenticate Azure-hosted Java applications.To complete the authentication in local development environments, you can use Azure CLI, Visual Studio Code, PowerShell or other methods. For more information, see Azure authentication in Java development environments. To complete the authentication in Azure hosting environments, we recommend using managed identity. For more information, see What are managed identities for Azure resources?
Start the application. You should see
Sending message
andHello Word
posted to your application log, as shown in the following example output:Sending message Message received: Hello Word
Deploy to Azure Spring Apps
Now that you have the Spring Boot application running locally, it's time to move it to production. Azure Spring Apps makes it easy to deploy Spring Boot applications to Azure without any code changes. The service manages the infrastructure of Spring applications so developers can focus on their code. Azure Spring Apps provides lifecycle management using comprehensive monitoring and diagnostics, configuration management, service discovery, CI/CD integration, blue-green deployments, and more. To deploy your application to Azure Spring Apps, see Deploy your first application to Azure Spring Apps.
Next steps
Feedback
Submit and view feedback for