在天蓝色的辅助角色中使用QueueClient.OnMessage [英] Using QueueClient.OnMessage in an azure worker role

查看:69
本文介绍了在天蓝色的辅助角色中使用QueueClient.OnMessage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Azure工作人员角色,负责检查4个服务总线队列.当前,我只是手动检查队列的循环方法.

I have an Azure worker role that is responsible for checking 4 service bus queues. Currently, I just the looping method to manually check the queues.

while(true)
{
    //loop through my queues to check for messages
}

Azure SDK 2.0附带了侦听消息而不是轮询消息的功能.但是我看到的每个示例都使用带有Console.ReadKey()的控制台应用程序.是否有办法让工作角色也坐下来等待消息?

With the Azure SDK 2.0 came the ability to listen for messages rather than polling for them. But Every example I've seen uses a console app with Console.ReadKey(). Is there a way to have the worker role sit and wait on messages too?

我尝试过:

public override void Run()
{
    _queueProcessors.ForEach(x => x.OnMessage(Process);
}

其中_queueProcessors是QueueClients的列表,而Process是处理消息的私有方法.但是,辅助角色将注册它们,然后重新启动.

where _queueProcessors is a list of QueueClients and Process is a private method that handles the messages. However, the worker role would register them and then restart.

所以有人知道如何让队列客户端坐下来等待消息吗?

So anyone know how to make a queue client sit and wait on a message?

推荐答案

以下是此示例代码:

using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging;
using Microsoft.WindowsAzure.ServiceRuntime;
using System.Diagnostics;
using System.Net;
using System.Threading;

namespace WorkerRoleWithSBQueue1
{
    public class WorkerRole : RoleEntryPoint
    {
        // The name of your queue
        const string QueueName = "demoapp";
        ManualResetEvent CompletedEvent = new ManualResetEvent(false);

    // QueueClient is thread-safe. Recommended that you cache 
    // rather than recreating it on every request
    QueueClient Client;

    public override void Run()
    {
        OnMessageOptions options = new OnMessageOptions();
        options.AutoComplete = true; // Indicates if the message-pump should call complete on messages after the callback has completed processing.
        options.MaxConcurrentCalls = 1; // Indicates the maximum number of concurrent calls to the callback the pump should initiate 
        options.ExceptionReceived += LogErrors; // Allows users to get notified of any errors encountered by the message pump

        Trace.WriteLine("Starting processing of messages");
        // Start receiveing messages
        Client.OnMessage((receivedMessage) => // Initiates the message pump and callback is invoked for each message that is recieved, calling close on the client will stop the pump.
            {
                try
                {
                    // Process the message
                    Trace.WriteLine("Processing Service Bus message: " + receivedMessage.SequenceNumber.ToString());
                }
                catch
                {
                    // Handle any message processing specific exceptions here
                }
            }, options);

        CompletedEvent.WaitOne();
    }

    private void LogErrors(object sender, ExceptionReceivedEventArgs e)
    {
        if (e.Exception != null)
        {
            Trace.WriteLine("Error: " + e.Exception.Message);
        }
    }

    public override bool OnStart()
    {
        // Set the maximum number of concurrent connections 
        ServicePointManager.DefaultConnectionLimit = 12;

        // Create the queue if it does not exist already
        Trace.WriteLine("Creating Queue");
        string connectionString = "*** provide your connection string here***";
        var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
        if (!namespaceManager.QueueExists(QueueName))
        {
            namespaceManager.CreateQueue(QueueName);
        }

        // Initialize the connection to Service Bus Queue
        Client = QueueClient.CreateFromConnectionString(connectionString, QueueName);

        Trace.WriteLine("Sending messages...");
        // populate some messages
        for (int ctr = 0; ctr < 10; ctr++)
        {
            Client.Send(new BrokeredMessage());
        }

        return base.OnStart();
    }

    public override void OnStop()
    {
        // Close the connection to Service Bus Queue
        Client.Close();
        CompletedEvent.Set(); // complete the Run function
        base.OnStop();
    }
}

}

这篇关于在天蓝色的辅助角色中使用QueueClient.OnMessage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆