Skip to content

Latest commit

 

History

History
169 lines (130 loc) · 8.06 KB

spring-messaging-storage-queue-support.md

File metadata and controls

169 lines (130 loc) · 8.06 KB
titledescriptionms.dateauthorms.authorms.reviewerms.topicms.customappliesto
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
✅ Version 4.20.0
✅ Version 5.22.0

Spring Cloud Azure support for Spring Messaging Azure Storage Queue

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.

Spring Messaging Azure Storage Queue

Key concepts

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 setup

<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>

Configuration

The library provides the following configuration options for StorageQueueTemplate:

[!div class="mx-tdBreakAll"]

PropertyTypeDescription
spring.cloud.azure.message-converter.isolated-object-mapperbooleanWhether an isolated ObjectMapper bean is used for Storage Queue message converter. Enabled by default.
spring.cloud.azure.storage.queue.enabledbooleanWhether an Azure Storage Queue is enabled.
spring.cloud.azure.storage.queue.connection-stringStringStorage Queue Namespace connection string value.
spring.cloud.azure.storage.queue.accountNameStringStorage Queue account name.
spring.cloud.azure.storage.queue.accountKeyStringStorage Queue account key.

Basic usage

Custom Storage Queue message converter

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 custom ObjectMapper 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); }

Send and receive messages to Azure Storage Queue

Use the following steps to send and receive messages:

  1. 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.

  1. 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(); // ... } }

Samples

For more information, see the azure-spring-boot-samples repository on GitHub.

close