1

I need to provision a Service Bus queue from code but I can't seem to find any details on how to do it. The Azure Service bus library has a unit test that is creating the queue (link) but the maven library I've referenced doesn't have any of those classes (QueueDescription or ManagementClientAsync).

Has anyone tried creating a queue dynamically from java?

Maven:

<dependency> 
  <groupId>com.microsoft.azure</groupId> 
  <artifactId>azure-servicebus</artifactId> 
  <version>1.2.5</version>
</dependency>
0

2 Answers 2

3

I've referenced doesn't have any of those classes (QueueDescription or ManagementClientAsync).

However this class seem not to be part of azure-core or azure-servicebus librairy and I can seem to find which lib to add to the project to have access to those class.

You mentioned QueueDescription or ManagementClientAsync seems only available in the 2.0.0-PREVIEW version. Please have a try to use the following dependency.

<!-- https://mvnrepository.com/artifact/com.microsoft.azure/azure-servicebus -->
<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-servicebus</artifactId>
    <version>2.0.0-PREVIEW-5</version>
</dependency>

Demo code:

String connectionString = "Endpoint=sb://xxxx.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=xxxxxx";
ConnectionStringBuilder connectionStringBuilder = new ConnectionStringBuilder(connectionString);
ManagementClient client = new ManagementClient(connectionStringBuilder);
if(!client.queueExists("queueName"))
{
    QueueDescription queue = client.createQueue("queueName");
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is now up to 2.0.0-PREVIEW-7
Use ServiceBusAdministrationClient if you want to go ahead with the new servicebus library com.azure:azure-messaging-servicebus:7.11.0
0

Please note that the above mentioned library com.microsoft.azure:azure-servicebus is deprecated and it is encouraged to use the newer com.azure:azure-messaging-servicebus library as of December 2020.

As per the new library, the process to create a servicebus queue is as follows:

ServiceBusAdministrationClient sbAdminClient =  new ServiceBusAdministrationClientBuilder()
        .connectionString(servicebusConnString).buildClient();
    String queueName = "my-queue";
    if(!sbAdminClient.getQueueExists(queueName))
    {
      CreateQueueOptions queueOptions =
          new CreateQueueOptions().setMaxSizeInMegabytes(1024)
              .setMaxDeliveryCount(10)
              .setDefaultMessageTimeToLive(Duration.ofDays(1))
              .setLockDuration(Duration.ofSeconds(10))
              .setDeadLetteringOnMessageExpiration(true)
              .setPartitioningEnabled(true);
      sbAdminClient.createQueue(queueName,queueOptions);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.