2

我正在使用 azure queue storage 发送电子邮件。电子邮件存储在队列存储中,队列一次发送 20 封电子邮件。

//Checks for messages inn the queue
foreach (CloudQueueMessage msgin sendEmailQueue.GetMessages(20, TimeSpan.FromSeconds(50)))
   {
        ProcessQueueMessage(msg);

   }

我遇到的问题是,当一封电子邮件被添加到队列中时,SMTP 详细信息不正确(即密码错误),该消息会因为发送失败而留在队列中,并阻止队列中的其他消息发送。

private void ProcessQueueMessage(CloudQueueMessage msg)
{
  try
  {
    //We try to send an email
    SendEmail(emailRowInMessageTable, htmlMessageBodyRef, textMessageBodyRef);

  } catch (SmtpException e)
  {
    string err = e.Message;

    //When an error occurs we check to see if the message failed to send certain no. of      
      times
    if (msg.DequeueCount > 10)
    {
      //We delete the message from queue
      sendEmailQueue.DeleteMessage(msg);

      return;
    } else
    {
      //delete from top of queue
      sendEmailQueue.DeleteMessage(msg);

      //insert into end of queue
      sendEmailQueue.AddMessage(msg);

      return;
    }
  }
 }

我尝试的解决方案是在出现错误时从队列中删除消息,并将其添加回队列的末尾,从而发送正确的电子邮件。但是删除并将消息添加回队列会重置其 dequeue 属性,这并不理想,因为我使用 dequeue 属性来确保消息不会永远在队列中。

在这种情况下,最好的解决方案是什么?

4

2 回答 2

1

您实际上不必删除消息并添加它。一旦消息的visibility timeout期限(在您的情况下为 50 秒)到期,它将自动出现在队列中。这样,您的DequeueCount逻辑也可以在同一消息出列并再次入队时起作用。

请注意,Windows Azure 队列是尽力而为的 FIFO ......所以它并不总是必要的,并且将根据添加时间来挑选消息。你可以做几件事:

  • 减少重试次数(目前你有 10 次,可以减少到 5 次)或
  • 检查实际异常。如果该过程由于不正确的凭据而失败,它将在下一次和下一次之后失败。重试该消息毫无意义。您可以将该消息移动到有害队列中,以便稍后检查该消息。
于 2014-01-09T17:06:41.013 回答
0

解决方案是为不同的 SMTP 服务器使用不同的队列,并使用多个线程从一个工作角色运行它。

在这里使用了框架:http : //www.31a2ba2a-b718-11dc-8314-0800200c9a66.com/2010/12/running-multiple-threads-on-windows.html 在单个工作者角色中运行多个线程。

于 2014-01-15T15:03:33.067 回答