### Create Azure Service Bus Topic and Subscription with Rule Options Source: https://github.com/showpune-migration/azure-service-bus/blob/main/Azure-Service-Bus.md Provides Spring Bean definitions to get or create an Azure Service Bus topic and a subscription with a CorrelationRuleFilter based on a route key. Requires importing `ResourceNotFoundException` from `com.azure.core.exception`. ```java @Bean public TopicProperties topicProperties(ServiceBusAdministrationClient adminClient, String topicName) { try { return adminClient.getTopic(topicName); } catch (ResourceNotFoundException e) { return adminClient.createTopic(topicName); } } @Bean @DependsOn("topicProperties") public SubscriptionProperties subscription( ServiceBusAdministrationClient adminClient, String topicName, String subscriptionName, String routeKey) { try { return adminClient.getSubscription(topicName, subscriptionName); } catch (ResourceNotFoundException e) { CreateSubscriptionOptions subOptions = new CreateSubscriptionOptions(); CorrelationRuleFilter filter = new CorrelationRuleFilter().setLabel(routeKey); CreateRuleOptions ruleOptions = new CreateRuleOptions().setFilter(filter); return adminClient.createSubscription(topicName, subscriptionName, "RouteKey", subOptions, ruleOptions); } } ``` -------------------------------- ### Create Queue in Azure Service Bus Source: https://github.com/showpune-migration/azure-service-bus/blob/main/Azure-Service-Bus.md Provides a Spring Bean definition to get or create an Azure Service Bus queue. Requires importing `ResourceNotFoundException` from `com.azure.core.exception`. ```java @Bean public QueueProperties queue(ServiceBusAdministrationClient adminClient, String queueName) { try { return adminClient.getQueue(queueName); } catch (ResourceNotFoundException e) { return adminClient.createQueue(queueName); } } ``` -------------------------------- ### Send Azure Service Bus Message without Routing Key - Java Source: https://github.com/showpune-migration/azure-service-bus/blob/main/Azure-Service-Bus.md Constructs and sends a simple message to an Azure Service Bus queue or topic using ServiceBusTemplate. This example does not set the ServiceBusMessageHeaders.SUBJECT header, suitable for queues or topics without routing requirements. ```Java Message message = MessageBuilder.withPayload(strMessage).build(); serviceBusTemplate.send("queueName", message); ``` -------------------------------- ### Listener with Payload and Headers Source: https://github.com/showpune-migration/azure-service-bus/blob/main/Azure-Service-Bus.md Demonstrates a message listener method that receives the payload and message headers as a Map. ```java public void listener(T msg, @Headers Map headers) { // Handle payload and headers } ``` -------------------------------- ### Listener with Payload, Message, and Context Source: https://github.com/showpune-migration/azure-service-bus/blob/main/Azure-Service-Bus.md Demonstrates a message listener method that receives the payload, the Spring `Message`, and the Service Bus context, enabling manual message completion or abandonment. ```java public void listener(T payload, Message message, @Header(ServiceBusMessageHeaders.RECEIVED_MESSAGE_CONTEXT) ServiceBusReceivedMessageContext context) { try { // Handle payload, message, and context context.complete(); } catch ( ... ) { context.abandon(); } } ``` -------------------------------- ### @ServiceBusListener on Topic and Subscription Source: https://github.com/showpune-migration/azure-service-bus/blob/main/Azure-Service-Bus.md Shows how to use the `@ServiceBusListener` annotation to configure a listener for a specific topic and subscription. ```java @ServiceBusListener(destination = "demoTopic", group = "demoSubscription") public void listener(T message) { // Handle message } ``` -------------------------------- ### Initialize ServiceBusAdministrationClient Bean - Java Source: https://github.com/showpune-migration/azure-service-bus/blob/main/Azure-Service-Bus.md Defines a Spring Bean for ServiceBusAdministrationClient, which is used for managing Azure Service Bus resources programmatically (e.g., creating topics, subscriptions, queues). It is initialized using Managed Identity credentials provided by the spring-cloud-azure-starter. ```Java @Bean public ServiceBusAdministrationClient adminClient(AzureServiceBusProperties properties, TokenCredential credential) { return new ServiceBusAdministrationClientBuilder() .credential(properties.getFullyQualifiedNamespace(), credential) .buildClient(); } ``` -------------------------------- ### Add Azure Service Bus Core Dependencies - XML Source: https://github.com/showpune-migration/azure-service-bus/blob/main/Azure-Service-Bus.md Adds the core Azure Spring Boot starter and the Azure Service Bus messaging dependencies to the project. These dependencies provide auto-configuration and necessary classes for interacting with Azure Service Bus. ```XML com.azure.spring spring-cloud-azure-starter com.azure.spring spring-messaging-azure-servicebus ``` -------------------------------- ### @ServiceBusListener on Queue Source: https://github.com/showpune-migration/azure-service-bus/blob/main/Azure-Service-Bus.md Shows how to use the `@ServiceBusListener` annotation to configure a listener for a specific queue. ```java @ServiceBusListener(destination = "queueName") public void listener(T message) { // Handle message } ``` -------------------------------- ### Listener with Payload Source: https://github.com/showpune-migration/azure-service-bus/blob/main/Azure-Service-Bus.md Demonstrates a simple message listener method that receives only the message payload. ```java public void listener(T payload) { // Handle payload } ``` -------------------------------- ### Listener with Message Source: https://github.com/showpune-migration/azure-service-bus/blob/main/Azure-Service-Bus.md Demonstrates a message listener method that receives the full Spring `Message` object, allowing access to both payload and headers. ```java public void listener(Message message) { T body = message.getPayload(); // Handle message } ``` -------------------------------- ### Configure Azure Service Bus with Managed Identity - Properties Source: https://github.com/showpune-migration/azure-service-bus/blob/main/Azure-Service-Bus.md Configures the Spring Boot application to use Azure Managed Identity for connecting to Azure Service Bus. Sets the entity type (queue or topic) and the Service Bus namespace. Requires the AZURE_CLIENT_ID and SERVICE_BUS_NAMESPACE environment variables or properties. ```Properties spring.cloud.azure.credential.managed-identity-enabled=true spring.cloud.azure.credential.client-id=${AZURE_CLIENT_ID} spring.cloud.azure.servicebus.entity-type=queue spring.cloud.azure.servicebus.namespace=${SERVICE_BUS_NAMESPACE} ``` -------------------------------- ### Send Azure Service Bus Message with Routing Key - Java Source: https://github.com/showpune-migration/azure-service-bus/blob/main/Azure-Service-Bus.md Constructs and sends a message to an Azure Service Bus topic using ServiceBusTemplate. Sets the ServiceBusMessageHeaders.SUBJECT header with a routing key, which is used by subscriptions for filtering messages. ```Java Message message = MessageBuilder.withPayload(strMessage) .setHeader(ServiceBusMessageHeaders.SUBJECT, "routing.key").build(); serviceBusTemplate.send("topic", message); ``` -------------------------------- ### Add Azure Service Bus Managed Dependency (BOM) - XML Source: https://github.com/showpune-migration/azure-service-bus/blob/main/Azure-Service-Bus.md Adds the Azure Spring Cloud Azure dependencies Bill of Material (BOM) to the dependencyManagement section of the pom.xml file. This helps manage versions for related Azure dependencies. Adjust the version based on your Spring Boot version (5.22.0 for Spring Boot 3.x, 4.20.0 for Spring Boot 2.x). ```XML com.azure.spring spring-cloud-azure-dependencies 5.22.0 import pom ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.