How to create a service bus subscription in Java

Ryan Winterbourne 20 Reputation points
2024-09-04T16:07:43.7833333+00:00

I am having two issues using the Azure Service Bus Java API. I am unable to create a new subscription or list existing subscriptions. Below is the code I am using...

ServiceBusAdministrationClient serviceBusAdminClient = new ServiceBusAdministrationClientBuilder()
                    .connectionString(connectionString)
                    .httpLogOptions(logOptions)
                    .buildClient();

serviceBusAdminClient.listTopics().forEach(topic -> {
             log.debug("Topic Name List: '" + topic.getName() + "'");
});

serviceBusAdminClient.listSubscriptions(topicName).forEach(sub -> {
            log.debug("Subscription Name : '" + sub.getSubscriptionName() + "'");
});


The client is created fine and I am able to list the topics. However, whenever I list the subscriptions, it always returns nothing. I have verified that I am using the correct topic name and that subscriptions actually exist for that topic.

Secondly, I am trying to create a new subscription for a given topic using code just like the Azure examples...

SubscriptionProperties properties =
         serviceBusAdminClient.createSubscription(topicName, subscriptionName, ruleName, options, createRuleOptions);


I have tried doing it the simple way by just specifying the topic and subscription name and letting the rules be defaults, I have tried various combos of settings but I always seem to get the same error...

<Error><Code>400</Code><Detail>The specified resource description is invalid. TrackingId:a2ea230d-90d2-4e5e-9db8-dc6129515fc6_G4, SystemTracker:ncs-ext-dev-sbn:Topic:ncs-cache-dev-sbt, Timestamp:2024-09-04T15:35:03</Detail></Error>"

    at com.azure.messaging.servicebus.administration.AdministrationModelConverter.mapException(AdministrationModelConverter.java:509)

    at com.azure.messaging.servicebus.administration.ServiceBusAdministrationClient.executeAndThrowException(ServiceBusAdministrationClient.java:1764)

    at com.azure.messaging.servicebus.administration.ServiceBusAdministrationClient.createSubscriptionWithResponse(ServiceBusAdministrationClient.java:461)

    at com.azure.messaging.servicebus.administration.ServiceBusAdministrationClient.createSubscription(ServiceBusAdministrationClient.java:396)
Azure Service Bus
Azure Service Bus
An Azure service that provides cloud messaging as a service and hybrid integration.
636 questions
Azure
Azure
A cloud computing platform and infrastructure for building, deploying and managing applications and services through a worldwide network of Microsoft-managed datacenters.
1,022 questions
{count} votes

Accepted answer
  1. JananiRamesh-MSFT 27,921 Reputation points
    2024-09-07T05:31:38.1666667+00:00

    Ryan Winterbourne Thank you for your patience while we worked through this issue. I appreciate you sharing the GitHub post (https://github.com/Azure/azure-sdk-for-java/issues/39853); it was indeed helpful, others experiencing the same thing can easily reference this.

    Since the Microsoft Q&A community has a policy that the question author cannot accept their own answer, they can only accept answers by others, I'll repost your solution in case you'd like to Accept the answer.

    Issue:

    Two main issues while using the Azure Service Bus Java API:

    1. Listing Subscriptions: You were able to list topics successfully but could not list subscriptions for a given topic. Despite verifying that the topic name was correct and that subscriptions existed, the listSubscriptions method always returned an empty result.
    2. Creating a Subscription: When attempting to create a new subscription, encountered an error:
      400 The specified resource description is invalid.

    Resolution: The problem was related to mismatched Azure dependency versions. By adding dependency management to the pom.xml and removing the version from the azure-messaging-servicebus dependency, you were able to resolve the issue

    Feedback: better validation and more informative error messages would have been helpful. I will pass this feedback along to the Azure SDK team to improve the developer experience.

    If you have any further questions or run into any other issues, please don’t hesitate to reach out.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. JananiRamesh-MSFT 27,921 Reputation points
    2024-09-06T09:14:51.79+00:00

    @Ryan Winterbourne Thanks for reaching out. I tried to repro inhouse and I am able to create/retrieve subscriptions under topic successfully.

    public class ListSubscriptions {
        
    public static void main(String[] args) {
    
            // Connection string to your Service Bus namespace
            String connectionString = "Endpoint=sb://xxxxxxxxxxxx.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=1xLA0ht5Q+tRNw/YxxxxxxxxxQK4b+ASbIaCPP8=";
            String topicName = "topic2";
            String subscriptionName = "subscription3";
    
            // Create an administration client
            ServiceBusAdministrationClient adminClient = new ServiceBusAdministrationClientBuilder()
                .connectionString(connectionString)
                .buildClient();
    
                 // Create a subscription under the topic
            adminClient.createSubscription(topicName, subscriptionName, new CreateSubscriptionOptions());
            System.out.printf("Subscription '%s' created under topic '%s'%n", subscriptionName, topicName);
    
            // List all subscriptions in the topic
            adminClient.listSubscriptions(topicName).forEach(subscription -> {
                System.out.printf("Subscription Name: %s%n", subscription.getSubscriptionName());
            });
    
        }
    }
    
    

    as mentioned earlier please try to list subscriptions for a topic using rest api call first to confirm if you're looking at the correct namespace/topic https://learn.microsoft.com/en-us/rest/api/servicebus/controlplane/subscriptions/list-by-topic?view=rest-servicebus-controlplane-2021-11-01&tabs=HTTP

    To create a subscription https://learn.microsoft.com/en-us/rest/api/servicebus/controlplane/subscriptions/create-or-update?view=rest-servicebus-controlplane-2021-11-01&tabs=HTTP

    please try creating subscription using the above given code and let me know incase of further queries, I would be happy to assist you.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.