用delphi xe 开发rabbitmq应用(四)

读取死信队列

用delphi xe 开发rabbitmq应用(四)

死信队列中的消息由系统自动增加了一些标识属性,x-death(是一个Array),x-first-death-exchange(queue/reason)。

死信队列中的消息如果不是由于异常(publisher代码控制)而进入,由于超时而进入,那其中的消息仍然可以正常读取(消费),只要代码中做特别处理。


   try
       //解码Header,如果从死信队列接收,其中还有 (x-death) Array
       with aMsg.Header.PropertyList.ApplicationHeaders do
       begin
           if IsFieldExist('x-death') then
           begin
                vDeath := Field['x-death'].AsArray;

                if vDeath.Items[0].kind = 'F' then
                begin
                   vTable := vDeath.Items[0].CastAsTable;
                   SiMain.LogInt64('TAsyncHello.DecodeMessage->count', vTable.FieldByName('count').Value.AsLongLongInt.Value);
                   SiMain.LogString('TAsyncHello.DecodeMessage->reason', vTable.StringByName('reason'));
                   SiMain.LogString('TAsyncHello.DecodeMessage->queue', vTable.StringByName('queue'));
                   SiMain.LogString('TAsyncHello.DecodeMessage->exchange', vTable.StringByName('exchange'));
                   //SiMain.LogString('x-first-death-exchange', vTable.StringByName('x-first-death-exchange'));

                end;
           end;
           if IsFieldExist('x-first-death-exchange') then
              SiMain.LogString('x-first-death-exchange', StringByName('x-first-death-exchange'));

           if IsFieldExist('x-first-death-queue') then
              SiMain.LogString('x-first-death-queue', StringByName('x-first-death-queue'));

           if IsFieldExist('x-first-death-reason') then
              SiMain.LogString('x-first-death-reason', StringByName('x-first-death-reason'));
       end;
   Except on E: Exception do
       begin
           siMain.LogException('解码消息头错误:' + E.Message);
           MainForm.MemoMessages.Lines.Add('解码消息头错误:' + E.Message);
       end;
   end;

   vMsgID := aMsg.Header.PropertyList.MessageID.Value;
   vAppID := aMsg.Header.PropertyList.AppID.Value;
   SiMain.LogString('AppID', aMsg.Header.PropertyList.AppID.Value);

   SiMain.LogInteger('TAsyncHello.DecodeMessage-->DeliveryMode', aMsg.Header.PropertyList.DeliveryMode.Value);
   SiMain.LogInteger('TAsyncHello.DecodeMessage-->Priority', aMsg.Header.PropertyList.Priority.Value);
   SiMain.LogString('TAsyncHello.DecodeMessage-->ContentType', aMsg.Header.PropertyList.ContentType.Value);
   //SiMain.LogString('Expiration', aMsg.Header.PropertyList.Expiration.Value);
   SiMain.LogString('TAsyncHello.DecodeMessage-->ContentEncoding', aMsg.Header.PropertyList.ContentEncoding.Value);