DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. How to Create a Service Bus With Windows Azure

How to Create a Service Bus With Windows Azure

Windows Azure Service Bus is a brokered, scalable, multi-featured messaging queuing system. It's a reliable message queuing and durable publish/subscribe system.

Jitendra Bafna user avatar by
Jitendra Bafna
CORE ·
Mar. 07, 17 · Tutorial
Like (1)
Save
Tweet
Share
28.56K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, we will walk through how to create a service bus the with Windows Azure portal.

Queue

  • The queue provides one-directional commmunication.

  • The queue processes the message in FIFO (first-in-first-out) order.

  • Messages in the queue can be consumed by one receiver.

  • Sender and receiver do not have to receive and send messages at the same time.

Topic

  • Like queues, topics also provide one-directional communication.

  • Topics can have subscriptions. One topic can have multiple subscriptions.

  • Subscriptions can optionally use a filter to only receive messages that match specific criteria.

How to Create a Service Bus With Windows Azure

Windows Azure Service Bus is a brokered, scalable, multi-featured messaging queueing system. It is a reliable message queuing and durable publish/subscribe system.

1. Create Service Bus Namespace

Log into the Azure Portal. In the left navigation pane, click New > Enterprise Integration > Service Bus.

Image title

In the Create Namespace dialog, provide the namespace name and the system will immediately check for namespaces that are availaible.

Select the Pricing Tier (basic, premium, and standard) and Subscription in which you need to create the service bus.

Select the existing Resource Group where your service bus will live or create a new one. Select the Location where you need to host your Service Bus and click Create.

Image title

2. Management Credentials

Click the newly created namespace from the list of service bus namespaces. Then, click Shared access policies > RootManageSharedAccessKey.

Image title

After clicking RootManageSharedAccessKey, you can see primary and secondary key and primary and secondary connection string. You can copy all these details for your future use.

Image title

3. Create Queue With Windows Azure

First, you need to ensure that the service bus namespace is already created. In the left pane of the portal, select the Service Bus in which you need to create a queue.

Select Queue and then click Add Queue.Image title

In the Create Queue Dialog, enter a queue name, select the max size and other properties depending on your requirements, and click Create.

Image title

4. Create Queue Using C# Code

// Store ConnectionString in web.config or app.config and copy connection string
// from service bus namespace
string connectionString =
 CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");

var namespaceManager =
 NamespaceManager.CreateFromConnectionString(connectionString);

if (!namespaceManager.QueueExists("testqueue")) {
 namespaceManager.CreateQueue("testqueue");
}

5. Send Message to Queue (C# Code)

First, you need to install the nuget package WindowsAzure.ServiceBus. Right-click on your application in Visual Studio and select Manage NuGet Packages. Look for the Windows Azure Service Bus nuget package and install it.

using System;
using Microsoft.ServiceBus.Messaging;

namespace ServiceBusQueueSend {
 class Program {
  static void Main(string[] args) {
   var connectionString = "<Service Bus Namespace Primary or Secondary Connection String>";
   var queueName = "<Your queue name>";

   var client = QueueClient.CreateFromConnectionString(connectionString, queueName);
   var message = new BrokeredMessage("Test Message");

   client.Send(message);
  }
 }
}

Receiving message from queue:

using System;
using Microsoft.ServiceBus.Messaging;

namespace ServiceBusQueueReceive {
 class Program {
  static void Main(string[] args) {
   var connectionString = "<Service Bus Namespace Primary or Secondary Connection String>";
   var queueName = "QueueName";

   var client = QueueClient.CreateFromConnectionString(connectionString, queueName);

   client.OnMessage(message => {
    Console.WriteLine(String.Format("Message body: {0}", message.GetBody < String > ()));
    Console.WriteLine(String.Format("Message id: {0}", message.MessageId));
   });

   Console.ReadLine();
  }
 }
}

6. Create Topic With Windows Azure

First, you need to ensure that the service bus namespace is already created. In the left pane of the portal, look for the service bus in which you need to create a topic.

Select Topic and then click Add Topic.

Image title

In Create Topic Dialog, enter the topic name, select the max size and other properties depending on your requirements, and click Create.

Image title

Create topic using C#:

TopicDescription td = new TopicDescription("jittopic");
td.MaxSizeInMegabytes = 5120;
td.DefaultMessageTimeToLive = new TimeSpan(0, 1, 0);

// Create a new Topic with custom settings.
// Store ConnectionString in web.config or app.config and copy connection string
// from service bus namespace
string connectionString =
 CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");

var namespaceManager =
 NamespaceManager.CreateFromConnectionString(connectionString);

if (!namespaceManager.TopicExists("jittopic")) {
 namespaceManager.CreateTopic(td);
}

Add subscription to topic:

string connectionString =
 CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");

var namespaceManager =
 NamespaceManager.CreateFromConnectionString(connectionString);

if (!namespaceManager.SubscriptionExists("jittopic", "testsub")) {
 namespaceManager.CreateSubscription("jittopic", "testsub");
}

Add subscription with filters:

SqlFilter highIncomeFilter =
 new SqlFilter("income > 30000");

namespaceManager.CreateSubscription("jittopic",
 "HighIncome",
 highIncomeFilter);

SqlFilter lowIncomeFilter =
 new SqlFilter("income <= 30000");

namespaceManager.CreateSubscription("jittopic",
 "LowIncome",
 lowIncomeFilter);

Send message to topic:

using System;
using Microsoft.ServiceBus.Messaging;

namespace ServiceBusTpicSend {
 class Program {
  static void Main(string[] args) {
   var connectionString = "<Service Bus Namespace Primary or Secondary Connection String>";
   var topicName = "jittopic";

   var client = TopicClient.CreateFromConnectionString(connectionString, queueName);
   var message = new BrokeredMessage("Test Message");

   client.Send(message);
  }
 }
}

Receiving message from topic:

string connectionString =
 CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");

SubscriptionClient Client =
 SubscriptionClient.CreateFromConnectionString(connectionString, "jittopic", "HighIncome");

// Configure the callback options.
OnMessageOptions options = new OnMessageOptions();
options.AutoComplete = false;
options.AutoRenewTimeout = TimeSpan.FromMinutes(1);

Client.OnMessage((message) => {
 try {
  // Process message from subscription.
  Console.WriteLine("\n*High Income");
  Console.WriteLine("Body: " + message.GetBody < string > ());
  Console.WriteLine("MessageID: " + message.MessageId);
  Console.WriteLine("Message Number: " +
   message.Properties["MessageNumber"]);

  message.Complete();
 } catch (Exception) {
  message.Abandon();
 }
}, options);

Now, you know how to create a service bus namespace, queue, and topic.

azure

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How We Solved an OOM Issue in TiDB with GOMEMLIMIT
  • A Gentle Introduction to Kubernetes
  • Stop Using Spring Profiles Per Environment
  • What Are the Different Types of API Testing?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: