0

示例代码在这里

public override void Run()
{
   while (true)
   {
    IAsyncResult result = CUDClient.BeginReceive(TimeSpan.FromSeconds(10),  OnMessageReceive, CUDClient);
    Thread.Sleep(10000);
   }
}

我已经测试了这个 Azure 工作者角色。我在服务总线队列中保留了 100 条消息。它将实体更新作为一种操作(实体框架)。处理所有队列需要 15 分钟,看起来需要更长的时间。有什么改进的建议吗?

提前致谢

4

4 回答 4

1

实际上,根据我的经验,Service Bus 的速度非常快。你有什么问题是“Thread.Sleep(10000)”;

每条消息休眠 10 秒。对于 100 条消息 100*10 = 10000 秒 = 16.67 分钟所以这是延迟的问题......

解决方案:

不要使用 Thread.Sleep(10000); (不适合BeginReceive,只适合Receive)

public override void Run() //This should not be a Thread...If its a thread then your thread will terminate after receiving your first message
{
    IAsyncResult result = CUDClient.BeginReceive(**TimeSpan.MaxValue**,  OnMessageReceive, CUDClient);
}

//Function OnMessageReceive
{
 //Process the Message
 **IAsyncResult result = CUDClient.BeginReceive(TimeSpan.MaxValue,  OnMessageReceive, CUDClient);**
}

使用 TimeSpan.MaxValue 您与 SB 的连接将被保留很长时间。所以没有频繁的空消息(更少的成本)......

于 2013-01-24T14:48:39.020 回答
1

尝试使用XecMe Parallel 任务来处理消息读取。

XecMe@xecme.codeplex.com

于 2013-02-17T20:30:13.150 回答
0

我试过这个。

while (true)
        {
           //read all topic messages in sequential way....           
           IAsyncResult result = CUDClient.BeginReceive(OnMessageReceive, CUDClient); 
           Thread.Sleep(1000);
        }  
public static void OnMessageReceive(IAsyncResult result)
    {
        SubscriptionClient queueClient = (SubscriptionClient)result.AsyncState;
        IBusinessLogicProvider Obj;
        try
        {
            //Receive the message with the EndReceive call
            BrokeredMessage receivedmsg = queueClient.EndReceive(result);
                //receivedmsg = CUDClient.Receive();
                if (receivedmsg != null)
                {
                    switch (receivedmsg.ContentType)
                    {
                        case "Project":
                            Obj = new ProjectsBL();
                            Obj.HandleMessage(receivedmsg);
                            receivedmsg.BeginComplete(OnMessageComplete, receivedmsg);
                            break;
                     }
                 }
          }
    }

它在 1 分钟 (00:01:02) 内处理了所有 100 条消息。比上一个好很多。

于 2013-01-28T08:37:41.777 回答
0

试试这个...

  //Somefunction
    IAsyncResult result = CUDClient.BeginReceive(OnMessageReceive, CUDClient); 

    while (true)
        Thread.Sleep(1000); //In case you are using thread

    //Somefunction End

public static void OnMessageReceive(IAsyncResult result)
    {
        CUDClient.BeginReceive(OnMessageReceive, CUDClient);
        SubscriptionClient queueClient = (SubscriptionClient)result.AsyncState;
        IBusinessLogicProvider Obj;
        try
        {
            //Receive the message with the EndReceive call
            BrokeredMessage receivedmsg = queueClient.EndReceive(result);
                //receivedmsg = CUDClient.Receive();
                if (receivedmsg != null)
                {
                    switch (receivedmsg.ContentType)
                    {
                        case "Project":
                            Obj = new ProjectsBL();
                            Obj.HandleMessage(receivedmsg);
                            receivedmsg.BeginComplete(OnMessageComplete, receivedmsg);
                            break;
                     }
                 }
          }
    }
于 2013-01-29T08:55:20.893 回答