• 两代码的区别


    1,

      var cf = new ConnectionFactory { Uri = Uri, RequestedHeartbeat = 30 };

    while (innerTaskRunning)
                   {
                     using (var conn = cf.CreateConnection())
                       {
                           while (innerTaskRunning)
                           {
                               using (var ch = conn.CreateModel())
                               {
                                   try
                                   {
                                       ch.ExchangeDeclare(ExchangeName, exchangeType, true);
                                       ch.QueueDeclare(QueueName, true, false, false, Args);
                                   }
                                   catch (Exception exBind)
                                   {
                                       WriteLog(logFunc, "绑定队列出错!q=" + QueueName, exBind);
                                       return false; //异常跳出
                                   }


                                   var consumer = new QueueingBasicConsumer(ch);
                                   var consumerTag = ch.BasicConsume(QueueName, true, consumer);

                                   BasicDeliverEventArgs e;
                                   using (new MqWatch("BasicConsume", Uri, ExchangeName, QueueName))
                                   {
                                       e = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
                                   }
                                   var msg = Encoding.UTF8.GetString(e.Body); //这儿是消息内容
                                   e = null;
                                   try
                                   {

                                       consumeFunc(msg);
                                   }
                                   catch (Exception exConsume)
                                   {
                                       WriteLog(logFunc, "消费出错,msg=" + msg, exConsume);
                                       //add log
                                   }
                               }

                           } //end while
                       }
                   }

     

     

      var cf = new ConnectionFactory { Uri = Uri, RequestedHeartbeat = 30 };

    while (innerTaskRunning)
                   {
                    


                       using (var conn = cf.CreateConnection())
                       {
                              using (var ch = conn.CreateModel())
                               {
                                   try
                                   {
                                       ch.ExchangeDeclare(ExchangeName, exchangeType, true);
                                       ch.QueueDeclare(QueueName, true, false, false, Args);
                                   }
                                   catch (Exception exBind)
                                   {
                                       WriteLog(logFunc, "绑定队列出错!q=" + QueueName, exBind);
                                       return false; //异常跳出
                                   }


                                   var consumer = new QueueingBasicConsumer(ch);
                                   var consumerTag = ch.BasicConsume(QueueName, true, consumer);

                             while (innerTaskRunning)
                           {

                                    BasicDeliverEventArgs e;
                                   using (new MqWatch("BasicConsume", Uri, ExchangeName, QueueName))
                                   {
                                       e = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
                                   }
                                   var msg = Encoding.UTF8.GetString(e.Body); //这儿是消息内容
                                   e = null;
                                   try
                                   {

                                       consumeFunc(msg);
                                   }
                                   catch (Exception exConsume)
                                   {
                                       WriteLog(logFunc, "消费出错,msg=" + msg, exConsume);
                                       //add log
                                   }
                               }


                           } //end while
                       }
                   }

     

     

    第二段代码内存泄漏的风险。 一直无法释放  BasicDeliverEventArgs  。

    但是第二段代码写起来却是错的。

     

    应用使用BasicGet ACK的方式取得.

  • 相关阅读:
    jmeter并发定时器
    jmeter配置元件作用域
    tsung使用教程简介
    tsung部署详细教程
    Oracle数据库常用函数使用示例
    常用测试学习网站汇总
    PL&SQL编程基础简介及实践
    测试管理流程制度
    pip 提速方法
    eclipse 环境配置记录
  • 原文地址:https://www.cnblogs.com/zbw911/p/5319025.html
Copyright © 2020-2023  润新知