我正在使用azure队列存储来发送电子邮件。电子邮件存储在队列存储中,队列一次发送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属性来确保消息永远不在队列中。

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

有帮助吗?

解决方案 2

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

在此处使用框架: http://www.31a2ba2a -b718-11dc-8314-0800200c9a66.com/2010/12/running-multiple-threads-on-windows.html 在单个工人角色内运行多个线程。

其他提示

您不必真的删除消息并添加它。一旦消息 visibility timeout 期限(在您的情况下为50秒)已过期,它将自动出现在队列中。这样,你的 DequeueCount 逻辑也将工作,因为相同的消息是出列和再次排队。

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

  • 减少重试次数(目前您有10次,您可以将其减少到5次)或
  • 检查实际异常。如果由于凭据不正确而导致进程失败,则下次和之后的下一次都会失败。重试那条消息毫无意义。您可以将该消息移动到有害队列中,以便稍后可以检查该消息。
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top