I am new to Azure Service Bus and I am able to create queues and topic via Azure Portal. I could also able to create subscriptions for topic in there. We created a POC and it worked well and we could able to send and receive messages in that.
Now we need to create subscriptions for the topic via code than azure portal. Programming language is Java and JDK version is 17. Ours is spring boot application with spring boot parent version of 2.5.0. Please help us for selecting correct dependency for azure service bus client and it will be helpful if we get some code snippets for creating subscriptions for topic in Java.
The library you would want to use for this is com.azure.messaging.servicebus.administration
. Here's a sample code (taken from the same link) to create topics and subscriptions programmatically:
HttpLogOptions logOptions = new HttpLogOptions()
.setLogLevel(HttpLogDetailLevel.HEADERS);
// DefaultAzureCredential creates a credential based on the environment it is executed in.
TokenCredential tokenCredential = new DefaultAzureCredentialBuilder().build();
// 'fullyQualifiedNamespace' will look similar to "{your-namespace}.servicebus.windows.net"
ServiceBusAdministrationClient client = new ServiceBusAdministrationClientBuilder()
.credential(fullyQualifiedNamespace, tokenCredential)
.httpLogOptions(logOptions)
.buildClient();
String topicName = "my-new-topic";
TopicProperties topic = client.createTopic(topicName);
String subscriptionName = "high-importance-subscription";
String ruleName = "important-emails-filter";
CreateSubscriptionOptions subscriptionOptions = new CreateSubscriptionOptions()
.setMaxDeliveryCount(15)
.setLockDuration(Duration.ofMinutes(2));
CorrelationRuleFilter ruleFilter = new CorrelationRuleFilter()
.setCorrelationId("emails");
ruleFilter.getProperties().put("importance", "high");
CreateRuleOptions createRuleOptions = new CreateRuleOptions()
.setFilter(ruleFilter);
SubscriptionProperties subscription = client.createSubscription(topicName, subscriptionName, ruleName,
subscriptionOptions, createRuleOptions);
System.out.printf("Subscription created. Name: %s. Topic name: %s. Lock Duration: %s.%n",
subscription.getSubscriptionName(), subscription.getTopicName(), subscription.getLockDuration());