6

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

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

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

4

1 回答 1

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可能会丢失消息。请在此处查看问题 229,因此,不要在进程将消息从 DLQ(死信队列)移回输入队列时终止该进程。在后台,ServiceStack正在从 Redis 中弹出消息。

于 2013-05-27T01:02:43.770 回答