javaazureazureservicebusazure-servicebus-topicsazure-servicebus-subscriptions

Unable to change topic's subscription state programmatically in Java - Azure Service Bus


I need to change the state of topic's subscriptions from java code. Below are the dependencies I added in pom.xml. Spring boot version is 2.5.0 and JDK version is 17. Reason of adding this dependencies is to perform different operations on topics/queue/subscription programmatically. Operations like Create Topic, Create Queue, Create Subscriptions in topic, Delete Topics/Queue/Subscriptions, Create multiple topics with multiple subscriptions, Create subscriptions with filters, Enable/Disable topics and Subscriptions.

**<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-core</artifactId>
    <version>1.52.0</version>
</dependency>

<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-xml</artifactId>
    <version>1.1.0</version>
</dependency>

<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-core</artifactId>
    <version>1.52.0</version>
</dependency>

<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-core-amqp</artifactId>
    <version>2.9.9</version>
</dependency>

<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-core-http-nety</artifactId>
    <version>1.15.4</version>
</dependency>

<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-core</artifactId>
    <version>1.52.0</version>
</dependency>

<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-identity</artifactId>
    <version>1.13.3</version>
</dependency>

<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-messaging-servicebus</artifactId>
    <version>7.17.4</version>
</dependency>

<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-storage-blob</artifactId>
    <version>12.22.3</version>
</dependency>

<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-storage-common</artifactId>
    <version>12.21.2</version>
</dependency>

<dependency>
    <groupId>com.azure.spring</groupId>
    <artifactId>spring-cloud-azure-starter-servicebus-jms</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
</dependency>

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-servicebus</artifactId>
    <version>2.0.0-PREVIEW-5</version>
</dependency>

<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-resourcemanager-servicebus</artifactId>
    <version>2.24.0</version>
</dependency>

<dependency>
    <groupId>com.azure.resourcemanager</groupId>
    <artifactId>azure-resourcemanager</artifactId>
    <version>2.40.0</version>
</dependency>**

Below is the code and even though i get 200 OK response, status of subscription is not changing from ACTIVE/DISABLED. Please help.

import com.azure.messaging.servicebus.administration.ServiceBusAdministrationClient;
import com.azure.messaging.servicebus.administration.ServiceBusAdministrationClientBuilder;

public class SubscriptionManager {
    public static void main(String[] args) {
        String connectionString = "<service-bus-connection-string>";
        String topicName = "<topic-name>";
        String subscriptionName = "<subscription-name>";

        // Create the Service Bus Administration Client
        ServiceBusAdministrationClient adminClient = new ServiceBusAdministrationClientBuilder()
            .connectionString(connectionString)
            .buildClient();

        // Call method to update subscription status
        updateSubscriptionStatus(adminClient, topicName, subscriptionName, true); // true to enable, false to disable
    }

    public static void updateSubscriptionStatus(ServiceBusAdministrationClient client, String topicName, String subscriptionName, boolean enable) {
        if (enable) {
            client.getSubscription(topicName, subscriptionName)
                .setStatus(com.azure.messaging.servicebus.administration.models.EntityStatus.ACTIVE);
        } else {
            client.getSubscription(topicName, subscriptionName)
                .setStatus(com.azure.messaging.servicebus.administration.models.EntityStatus.DISABLED);
        }
    }
}

Solution

  • I tried the below Spring Boot code to Activate/Deactivate the Azure Service Bus Topic's subscription and successfully did it using POST requests.

    Code :

    AzureServiceBusService.java :

    package com.example.demoServiceBusDeactivate.service;
    
    import com.azure.messaging.servicebus.administration.ServiceBusAdministrationClient;
    import com.azure.messaging.servicebus.administration.ServiceBusAdministrationClientBuilder;
    import com.azure.messaging.servicebus.administration.models.EntityStatus;
    import com.azure.messaging.servicebus.administration.models.SubscriptionProperties;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Service;
    
    @Service
    public class AzureServiceBusService {
    
        @Value("${azure.servicebus.connectionString}")
        private String connectionString;
        private static final String TOPIC_NAME = "kamtopic";
        private static final String SUBSCRIPTION_NAME = "kamsub";
    
        public void deactivateSubscription() {
            try {
                ServiceBusAdministrationClient adminClient = new ServiceBusAdministrationClientBuilder()
                        .connectionString(connectionString)
                        .buildClient();
                SubscriptionProperties subscription = adminClient.getSubscription(TOPIC_NAME, SUBSCRIPTION_NAME);
                subscription.setStatus(EntityStatus.DISABLED);
                adminClient.updateSubscription(subscription);
                System.out.println("Subscription '" + SUBSCRIPTION_NAME + "' is now deactivated.");
            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            }
        }
    
        public void activateSubscription() {
            try {
                ServiceBusAdministrationClient adminClient = new ServiceBusAdministrationClientBuilder()
                        .connectionString(connectionString)
                        .buildClient();
                SubscriptionProperties subscription = adminClient.getSubscription(TOPIC_NAME, SUBSCRIPTION_NAME);
                subscription.setStatus(EntityStatus.ACTIVE);
                adminClient.updateSubscription(subscription);
                System.out.println("Subscription '" + SUBSCRIPTION_NAME + "' is now activated.");
            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            }
        }
    }
    

    ServiceBusController.java :

    package com.example.demoServiceBusDeactivate.controller;
    
    import com.example.demoServiceBusDeactivate.service.AzureServiceBusService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class ServiceBusController {
    
        @Autowired
        private AzureServiceBusService azureServiceBusService;
    
        @PostMapping("/deactivate-subscription")
        public ResponseEntity<String> deactivateSubscription() {
            try {
                azureServiceBusService.deactivateSubscription();
                return ResponseEntity.ok("Deactivation initiated.");
            } catch (Exception e) {
                e.printStackTrace();
                return ResponseEntity.status(500).body("An error occurred: " + e.getMessage());
            }
        }
    
        @PostMapping("/activate-subscription")
        public ResponseEntity<String> activateSubscription() {
            try {
                azureServiceBusService.activateSubscription();
                return ResponseEntity.ok("Activation initiated.");
            } catch (Exception e) {
                e.printStackTrace();
                return ResponseEntity.status(500).body("An error occurred: " + e.getMessage());
            }
        }
    }
    

    application.properties :

    azure.servicebus.connectionString=<ServiceBusTopicConneString>
    

    pom.xml :

    <dependency>  
        <groupId>com.azure</groupId>  
        <artifactId>azure-messaging-servicebus</artifactId>  
        <version>7.16.0</version> 
    </dependency>
    

    Output :

    I send the post request with below URL and Deactived the Azure Service Bus topic's Subscription as shown below.

    http://localhost:8080/deactivate-subscription
    

    enter image description here

    I saw that the subscription is deactivated in the Azure Service Bus topic, as shown below.

    enter image description here

    I send the post request with below URL and Actived the Azure Service Bus topic's Subscription as shown below.

    http://localhost:8080/activate-subscription
    

    enter image description here

    I saw that the subscription is activated in the Azure Service Bus topic, as shown below.

    enter image description here

    Terminal Output :

    enter image description here