OiO.lk Blog java Unable to change topic's subscription state programmatically in Java – Azure Service Bus
java

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



You need to sign in to view this answers

Exit mobile version