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);
{
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的方式取得.