如何使用Redis MQ从Dead Letter Queue重新发送?

时间:2012-12-11 16:59:01

标签: redis servicestack

刚刚花了我几个小时看Redis和Redis MQ。

慢慢地掌握Redis,想知道如何重新发送死信队列中的消息?

此外,配置选项在哪里确定消息在进入死信队列之前被重试的次数?

1 个答案:

答案 0 :(得分:4)

目前,无法在 ServiceStack 中的死信队列中自动重新发送消息。但是,您可以相对轻松地手动执行此操作:

使用以下命令从死信队列重新加载消息:

  public class AppHost {

      public override Configure(){

           // create the hostMq ...
           var hostMq = new RedisMqHost( clients, retryCount = 2 );

           // with retryCount = 2, 3 total attempts are made. 1st + 2 retries

           // before starting hostMq
           RecoverDLQMessages<TheMessage>(hostMq);

           // add handlers
           hostMq.RegisterHandler<TheMessage>( m => 
                     this.ServiceController.ExecuteMessage( m ) );

           // start hostMq
           hostMq.Start();
      }
  }

最终使用以下内容来恢复(重新排队)消息:

    private void RecoverDLQMessages<T>( RedisMqHost hostMq )
    {
        var client = hostMq.CreateMessageQueueClient();

        var errorQueue = QueueNames<T>.Dlq;
        log.Info( "Recovering Dead Messages from: {0}", errorQueue );
        var recovered = 0;

        byte[] msgBytes;
        while( (msgBytes = client.Get( errorQueue, TimeSpan.FromSeconds(1) )) != null )
        {
            var msg = msgBytes.ToMessage<T>();
            msg.RetryAttempts = 0;
            client.Publish( msg );
            recovered++;
        }

        log.Info( "Recovered {0} from {1}", recovered, errorQueue );
    }

注意

在撰写本文时, ServiceStack 可能会丢失消息。 Please See Issue 229 Here,所以,当它将消息从DLQ(死信队列)移回输入队列时,不要终止进程。在幕后, ServiceStack 是来自Redis的POPing消息。