同一个工作人员是否可以收听针对不同主题的2个不同订阅

时间:2015-02-11 17:09:50

标签: azure azure-worker-roles azure-cloud-services

我有两个不同的主题,一个应用程序正在向其发送消息。

我可以只拥有一个订阅这两个主题的辅助角色吗?

1 个答案:

答案 0 :(得分:1)

试试这个:

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();
    }
}

为简洁起见,我已跳过OnStart方法。