Daniel Goncalves
Daniel Goncalves

Reputation: 308

Is it possible for the same worker to listen to 2 different subscriptions for different topics

I have two different topics that one application is sending messages to.

Can I have just one worker role that subscribes to both topics?

Upvotes: 0

Views: 36

Answers (1)

ctv
ctv

Reputation: 1061

Try this:

public class WorkerRole : RoleEntryPoint
{
    ManualResetEvent CompletedEvent = new ManualResetEvent(false);
    SubscriptionClient Client1 = SubscriptionClient.CreateFromConnectionString("your conn str", "TestTopic1", "HighMessages");
    SubscriptionClient Client2 = SubscriptionClient.CreateFromConnectionString("your conn str", "TestTopic2", "HighMessages");

    public override void Run()
    {

        Client1.OnMessage((receivedMessage1) =>
        {
            var messageFromTopic1 = receivedMessage1.GetBody<string>();
            //Do stuff
        });

        Client2.OnMessage((receivedMessage2) =>
        {
            var messageFromTopic2 = receivedMessage2.GetBody<string>();
            //Do stuff
        });

        CompletedEvent.WaitOne();
    }

    public override void OnStop()
    {
        //Also close your clients here (Client1.Close(), ...)
        CompletedEvent.Set();
        base.OnStop();
    }
}

I've skipped the OnStart method for brevity.

Upvotes: 1

Related Questions