3
votes

I am trying to use to Azure Service Bus to broadcast a message from a Web Role to all the instances of a single worker role. This is the code I use to receive messages:

// Create the topic if it does not exist already
            string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
        var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);

        // Configure Topic Settings
        TopicDescription td = new TopicDescription("CommandTopic");
        td.MaxSizeInMegabytes = 5120;
        td.DefaultMessageTimeToLive = new TimeSpan(0, 0, 1);

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

        Random rand = new Random();
        double randNum = rand.Next();

        if (!namespaceManager.SubscriptionExists("CommandTopic", "CommandSubscription"+randNum))
        {
            namespaceManager.CreateSubscription("CommandTopic", "CommandSubscription" + randNum);
        }

        Client = SubscriptionClient.CreateFromConnectionString(connectionString, "CommandTopic", "CommandSubscription" + randNum, ReceiveMode.ReceiveAndDelete);
        Trace.WriteLine("SUBSCRIPTION: COMMANDSUBSCRIPTION"+randNum);

In order to create a separate subscription for each worker role instance (so that all instances receive the message in the topic) I had to use a random number. Is there a way of using some Id of the instance instead of the random number. There is Instance.Id however it is too long to be used as a parameter for the subscription name. Is there a shorter version without using substring? Also, is creating a separate subscription for each instance the proper approach? Previously all instances subscribed to the same subscription and so only 1 instance was getting the message and deleting it from the subscription.

2

2 Answers

0
votes

Try adding a bogus InternalEndpoint for the worker role to your configuration. This ensures the list of instances of a role gets populated.

0
votes

please find the following link, I hope it can help you make the intercommunication between your roles:

http://msdn.microsoft.com/en-us/library/windowsazure/hh180158.aspx

and also please have a look on the following link, I think it has exactly what you are looking for: http://windowsazurecat.com/2011/08/how-to-simplify-scale-inter-role-communication-using-windows-azure-service-bus/

I think what you are talking about would be like the Scenario 4 in the following link where a role can communicate to several other roles. I am not sure if what you asked for is possible, but try working with the Windows Azure Worker Role with Service Bus Queue, I think this might help you a lot and might also be a better solution than the topic and subscription in this case.