title | description | ms.date | author | ms.author | ms.reviewer | ms.topic | ms.custom | appliesto | ||
---|---|---|---|---|---|---|---|---|---|---|
Spring Cloud Azure support for Spring Messaging Azure Storage Queue | Spring Cloud Azure support for Spring Messaging Azure Storage Queue provides integration with Azure Storage Queue. | 04/07/2023 | KarlErickson | karler | seal | reference | devx-track-java, devx-track-extended-java |
|
This article describes how you can use Spring Cloud Azure and Spring Messaging Azure Storage Queue. The Spring Framework provides extensive support for integrating with messaging systems.
Azure Queue Storage is a service for storing large numbers of messages. You access messages from anywhere in the world via authenticated calls using HTTP or HTTPS. A queue message can be up to 64 KB in size. A queue can contain millions of messages, up to the total capacity limit of a storage account. Queues are commonly used to create a backlog of work to process asynchronously. The Spring Messaging for Azure Queue Storage project applies core Spring concepts to the development of service bus-based messaging solutions. It provides a template as a high-level abstraction for sending and receiving messages. These libraries promote the use of dependency injection and declarative configuration.
<dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-cloud-azure-starter</artifactId> </dependency> <dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-messaging-azure-storage-queue</artifactId> </dependency>
The library provides the following configuration options for StorageQueueTemplate
:
[!div class="mx-tdBreakAll"]
Property Type Description spring.cloud.azure.message-converter.isolated-object-mapper boolean Whether an isolated ObjectMapper bean is used for Storage Queue message converter. Enabled by default. spring.cloud.azure.storage.queue.enabled boolean Whether an Azure Storage Queue is enabled. spring.cloud.azure.storage.queue.connection-string String Storage Queue Namespace connection string value. spring.cloud.azure.storage.queue.accountName String Storage Queue account name. spring.cloud.azure.storage.queue.accountKey String Storage Queue account key.
There are two ways to configure Storage Queue message converter:
Configure the following property to have the default Storage Queue message converter use a
ObjectMapper
bean, which can be your customObjectMapper
bean or one managed by Spring Boot:spring: cloud: azure: message-converter: isolated-object-mapper: false
Define the Storage Queue message converter bean directly:
@BeanAzureMessageConverter<QueueMessageItem, QueueMessageItem> storageQueueMessageConverter() { JsonMapperjsonMapper = JsonMapper.builder().addModule(newJavaTimeModule()).build(); returnnewServiceBusMessageConverter(jsonMapper); }
Use the following steps to send and receive messages:
Fill in the credential configuration options using one of the following approaches:
For credentials as
DefaultAzureCredential
, configure the following properties in your application.yml file:spring: cloud: azure: storage: queue: account-name: ${AZURE_STORAGE_QUEUE_ACCOUNT_NAME}
For credentials as connection string, configure the following properties in your application.yml file:
spring: cloud: azure: storage: queue: connection-string: ${AZURE_STORAGE_QUEUE_CONNECTION_STRING}
For credentials as managed identities, configure the following properties in your application.yml file:
spring: cloud: azure: credential: managed-identity-enabled: trueclient-id: ${AZURE_CLIENT_ID}profile: tenant-id: <tenant>storage: queue: account-name: ${AZURE_STORAGE_QUEUE_ACCOUNT_NAME}
Note
The following values are allowed for tenant-id
: common
, organizations
, consumers
, or the tenant ID. For more information about these values, see the Used the wrong endpoint (personal and organization accounts) section of Error AADSTS50020 - User account from identity provider does not exist in tenant. For information on converting your single-tenant app, see Convert single-tenant app to multitenant on Microsoft Entra ID.
For credentials as service principal, configure the following properties in your application.yml file:
spring: cloud: azure: credential: client-id: ${AZURE_CLIENT_ID}client-secret: ${AZURE_CLIENT_SECRET}profile: tenant-id: <tenant>storage: queue: account-name: ${AZURE_STORAGE_QUEUE_ACCOUNT_NAME}
Note
The following values are allowed for tenant-id
: common
, organizations
, consumers
, or the tenant ID. For more information about these values, see the Used the wrong endpoint (personal and organization accounts) section of Error AADSTS50020 - User account from identity provider does not exist in tenant. For information on converting your single-tenant app, see Convert single-tenant app to multitenant on Microsoft Entra ID.
StorageQueueTemplate
is autoconfigured. You can autowire it directly into your own beans to send or receive messages, as shown in the following example:@ComponentpublicclassMyBean { privatefinalStorageQueueTemplatestorageQueueTemplate; publicMyBean(StorageQueueTemplatestorageQueueTemplate) { this.storageQueueTemplate = storageQueueTemplate; } publicvoidsomeMethod() { this.serviceBusTemplate.sendAsync('STORAGE_QUEUE_NAME', MessageBuilder.withPayload("Hello world").build()).subscribe(); } publicvoidprocessMessage() { Message<?> message = storageQueueTemplate.receiveAsync('STORAGE_QUEUE_NAME', Duration.ofSeconds(30)).block(); // ... } }
For more information, see the azure-spring-boot-samples repository on GitHub.