Wednesday 19 July 2017

Get started in Queue, Topic and Subscription in Microsoft Azure

In this post we are going to see how to start in Microsoft Azure for Queue, Topic and subscriptions

Go to the Azure Portal and create a Service Bus Namespace and get a connection string, Next create a queue and Topic

using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static string connectionString = "Endpoint=sb://raj.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=sbxireNSHksjqaiorlkms";
        static string QueuePath = "test_queue";
        static string Topicpath = "test_topic";

        static void Main(string[] args)
        {
            // For Queue
            var _queueClient = QueueClient.CreateFromConnectionString(connectionString,QueuePath);

            // sender side for queue
            for (int i = 0; i < 10; i++)
            {
                var message = new BrokeredMessage("Message " + i);
                _queueClient.Send(message);
                Console.WriteLine(message);
            }

            // receiver side for queue
            _queueClient.OnMessage((mes) => processMessage(mes));
            _queueClient.Close();


            // sender side For Topic
            string username = "rajesh";
            var manager = NamespaceManager.CreateFromConnectionString(connectionString);
            if(!manager.TopicExists(Topicpath))
            {
                manager.CreateTopic(Topicpath);
            }

           
            var subscription = new SubscriptionDescription(Topicpath, username)
            {
                AutoDeleteOnIdle = TimeSpan.FromMinutes(5)
            };
            manager.CreateSubscription(subscription);


            var factory = MessagingFactory.CreateFromConnectionString(connectionString);
            var topicClient = factory.CreateTopicClient(Topicpath);

            // receiver side for topic that is subscription
            var subClient = factory.CreateSubscriptionClient(Topicpath, username);
            subClient.OnMessage(msg => processMessage(msg));

            var helloMessage = new BrokeredMessage("Has entered Room");
            helloMessage.Label = username;
            topicClient.Send(helloMessage);

            while (true)
            {
                string readText = Console.ReadLine();
                if (readText.Equals("exit")) break;

                var readTextMessage = new BrokeredMessage(readText);
                readTextMessage.Label = username;
                topicClient.Send(readTextMessage);
            }

            var leftMessage = new BrokeredMessage("Left the room");
            leftMessage.Label = username;
            topicClient.Send(leftMessage);

            factory.Close();

        }

        private static void processMessage(BrokeredMessage mes)
        {
            var label = mes.Label;
            var text = mes.GetBody<string>();
            Console.WriteLine("Received " + text);
        }
    }
}



From this post you can learn how to get started in Queue, Topic and Subscription in Microsoft Azure 

No comments:

Post a Comment