Develop with Azure Service Bus (1): Manage Service Bus

Microsoft Azure Service Bus is a reliable information delivery service. The purpose of this service is to make communication easier and implement the decoupling between modules.

On the Azure service bus introduction, I will not repeat here, please refer to the official description: Azure service bus.

To achieve the full operation of this article, you need to have the following prerequisites:

  1. An active Azure subscription
  2. Java Runtime Environment 
  3. Java IDE, Eclipse (Optional)

This article will introduce how to use Azure portal / Java SDK to manage Azure Service Bus.

1. Create a Namespace

To begin using Service Bus queues in Azure, you must first create a namespace with a name that is unique across Azure. A namespace provides a scoping container for addressing Service Bus resources within your application.

1.1 Create in the Azure Portal

In the portal:

① Click the Add button to search for the ‘Service Bus’ keyword

② After finding the service bus, click the ‘Create’ button

③ Fill in the necessary information. Pricing tier option: Basic, Standard and Advanced. The Base can only use the queue service, the Standard and the Advanced can use the theme and subscription services additionally. Performance increases with the pricing tier becomes high.

1.2 Create by code

Here we use Azure Service Bus Management Library to manage.  On Azure Certification section please refer to my previous article: How to convert a VM from managed disks to unmanaged disks.

Sample code as below:

 ServiceBusNamespace serviceBusNamespace = azure.serviceBusNamespaces()
 .define("demo4servicebus")
 .withRegion(Region.ASIA_EAST)
 .withNewResourceGroup("demo")
 .withSku(NamespaceSku.STANDARD)
 .create();

2. Obtain the default shared access policy and connection string

2.1 Obtain in the Azure Portal

Refer to the red markup above, click the “Shared access policies” -> select the default policy -> copy the connection string.

2.2 Obtain by code

ServiceBusNamespace serviceBusNamespace = azure.serviceBusNamespaces().getByResourceGroup("demo", "demo4servicebus");
List<NamespaceAuthorizationRule> list = serviceBusNamespace.authorizationRules().list();
for (NamespaceAuthorizationRule item : list) {
 System.out.println(item.name() + " -> " + item.getKeys().primaryConnectionString());
}

3. Create queues

Here I will demonstrate how to create a queue, the same as the creation of topics and subscriptions.

3.1 Create in the Azure Portal

Refer to the red markup above, add a queue named ‘testqueue’ and set the relevant parameters.

3.2 Create by code

serviceBusNamespace.queues().define("testqueue")
 .withSizeInMB(1024)
 .withPartitioning()
 .withDefaultMessageTTL(Period.minutes(10))
 .withMessageLockDurationInSeconds(60)
 .create();

4. Summary

This article mainly introduces how to achieve the Azure Service Bus management through Azure Portal/ Java SDK . If you have any questions, please contact us via aka.ms/devchat.

Leave a Reply

Your email address will not be published. Required fields are marked *