用delphi xe 开发rabbitmq应用(四)

本文介绍如何从死信队列中读取消息,并解析其中的x-death属性,包括x-first-death-exchange、x-first-death-queue等关键字段,以便了解消息为何成为死信。

读取死信队列

死信队列中的消息由系统自动增加了一些标识属性,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);
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值